php 如何使用PHP修改现有的excel文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2144638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:13:56  来源:igfitidea点击:

How to modify existing excel file using PHP?

phpexcelpear

提问by Prabu

I have a excel i need to add some more sheets into the excel using PHP, i have used PEAR, there i tried only can write excel and read a file, not able to read and modify the file, guys can you help me in this?

我有一个 excel,我需要使用 PHP 在 excel 中添加更多工作表,我使用过 PEAR,在那里我尝试只能编写 excel 和读取文件,无法读取和修改文件,伙计们,你能帮我吗? ?

Thanks in advance

提前致谢

Prabu

普拉布

回答by Gerard Banasig

You will need 2 pear packages

你需要2个梨包

  1. PHP-ExcelReader package
  2. Spreadsheet_Excel_Writer package
  1. PHP-ExcelReader 包
  2. Spreadsheet_Excel_Writer 包

What you need to do is read first the excel file use PHP-ExcelReader packageIt reads the binary format of XLS files directly and can return values and formats from any cell. http://code.google.com/p/php-excel-reader/

您需要做的是先读取excel文件,使用PHP-ExcelReader包直接读取XLS文件的二进制格式,可以从任何单元格返回值和格式。 http://code.google.com/p/php-excel-reader/

read the excel file

读取excel文件

$data = new Spreadsheet_Excel_Reader("test.xls");

show the data of the file

显示文件的数据

$data->dump($row_numbers=false,$col_letters=false,$sheet=0,$table_class='excel')


Once you have stored the data in a variable save the data in another file this time you will use the The Spreadsheet_Excel_Writer packagehttps://github.com/pear/Spreadsheet_Excel_Writer

将数据存储在变量中后,这次将数据保存在另一个文件中,您将使用Spreadsheet_Excel_Writer 包https://github.com/pear/Spreadsheet_Excel_Writer

 <?php
require_once 'Spreadsheet/Excel/Writer.php';
$workbook = new Spreadsheet_Excel_Writer('test.xls');
$worksheet =& $workbook->addWorksheet('My first worksheet');
if (PEAR::isError($worksheet)) {
    die($worksheet->getMessage());
}
$workbook->close();
?>