【发布时间】:2022-01-25 17:07:37
【问题描述】:
首先,我在 stackoverflow 和表单中都搜索了我的问题,但我找不到确切的问题及其解决方案。 (This one is about saving HTML to existing Excel)
*** 我也看到了一些关于保存为新的 excel 文件的相关问题的答案,但我的问题是我可以保存到现有的 excel 文件中吗?如果有可能,为什么我打开它时会出错。
问题:
我有一个包含股票代码和日期的现有 excel-2010 模板文件。
我使用 PHPexcel 加载/读取此文件(读取/加载工作正常)并在一些空单元格上填写价格值,然后我尝试使用以下代码将文件保存回来。
require_once __DIR__ . '/src/Classes/PHPExcel.php';
require_once __DIR__ . '/src/Classes/PHPExcel/IOFactory.php';
// read existing excel file
$readphpexcel = PHPExcel_IOFactory::load("polimer_uzex.xlsx");
// read the worksheet named "fetched"
$fetched = $readphpexcel->getSheetByName("fetched");
// make an array from all data on the worksheet
$exceldatas = $fetched->toArray(null, true, true, true);
/// ... some cell value modifications take place here on $exceldatas
// write to excell
$fetched->fromArray($exceldatas, null, 'A1', true);
$objWriter = PHPExcel_IOFactory::createWriter($readphpexcel, 'Excel5');// Excel 2010
$objWriter->save("polimer_uzex.xlsx");
【问题讨论】: