我使用PHPExcel编辑大型XLSX文件(~230kb,20张)。我这样做的方式是:
这很慢(约6秒),虽然只有很小的变化(一个工作表中只有一些单元格)。
有没有办法只保存文件中的更改?或者如果没有,还有另一个图书馆吗?
答案 0 :(得分:0)
您可以尝试使用Spout:https://github.com/box/spout。它不应该超过一秒钟,您的代码应如下所示:
$reader = ReaderFactory::create(Type::XLSX);
$reader->open('path/to/file/to/read.xlsx');
$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile('path/to/file/to/write.xlsx'); // needs to be different than the one read for now
while ($reader->hasNextSheet()) {
$reader->nextSheet();
while ($reader->hasNextRow()) {
$row = $reader->nextRow();
// change the row here if needed
$writer->addRow($row);
}
}
$reader->close();
$writer->close();
// And at the end, you can replace the old file with the new one:
rename('path/to/file/to/read.xlsx', 'path/to/file/to/write.xlsx');
希望有所帮助!