如何解决 "Allowed memory size exhausted" 错误?
How can I resolve "Allowed memory size exhausted" error?
我正在使用 phpspreadsheet,我想用 4 sheet 修改一个 xlsx 文件。我只想在 2 sheet 中插入数据,但我想将所有 4 sheet 复制到新的 xlsx 文件中。当我这样做时,出现此错误:
Fatal error: Allowed memory size of 536870912 bytes exhausted (tried
to allocate 20480 bytes)
这是因为其中一个sheet太重了,但是那个重sheet我只想复制,不想修改。我试过加载这个:
ini_set('memory_limit', -1);
但它对我不起作用,因为它超出了定义的运行时间(超过 120 秒)。
我也试过这个:
$inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
$sheetnames = ['Data Sheet #1','Data Sheet #3'];
/** Create a new Reader of the type defined in $inputFileType **/
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Advise the Reader of which WorkSheets we want to load **/
$reader->setLoadSheetsOnly($sheetnames);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $reader->load($inputFileName);
但这只会将指定的 sheets 复制到新文件。
编辑:
我尝试使用以下代码复制不需要编辑的 sheets:
$spreadsheet1 =\PhpOffice\PhpSpreadsheet\IOFactory::load("./sampleData/example1.xls");
$clonedWorksheet = clone $spreadsheet1->getSheetByName('Data Sheet #2 ');
$clonedWorksheet->setTitle('Test');
$spreadsheet->addSheet($clonedWorksheet);
但现在我又收到另一个错误:
Fatal error: Uncaught Error: Call to a member function getCell() on
null in
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Calculation\Calculation.php:2785
Stack trace: #0
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Cell\Cell.php(262):
PhpOffice\PhpSpreadsheet\Calculation\Calculation->calculateCellValue(Object(PhpOffice\PhpSpreadsheet\Cell\Cell),
true) #1
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(1077):
PhpOffice\PhpSpreadsheet\Cell\Cell->getCalculatedValue() #2
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(1027):
PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet->writeCell(Object(PhpOffice\PhpSpreadsheet\Shared\XMLWriter),
Object(PhpOffice\PhpSpreadsheet\Worksheet\Worksheet), 'M7', Array) #3
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(76):
PhpOffice in
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Calculation\Calculation.php
on line 2785
我认为这是因为使用克隆无法复制公式。这可能是原因吗?用公式复制 sheet 是另一种解决方案吗?
终于找到解决办法了。问题出在作者和公式上,所以将这一行添加到代码中,现在运行良好:
$writer->setPreCalculateFormulas(false);
所以现在编写器的所有代码是:
$writer = new Xlsx($spreadsheet);
$writer->setPreCalculateFormulas(false);
$writer->save('test.xlsx');
我正在使用 phpspreadsheet,我想用 4 sheet 修改一个 xlsx 文件。我只想在 2 sheet 中插入数据,但我想将所有 4 sheet 复制到新的 xlsx 文件中。当我这样做时,出现此错误:
Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes)
这是因为其中一个sheet太重了,但是那个重sheet我只想复制,不想修改。我试过加载这个:
ini_set('memory_limit', -1);
但它对我不起作用,因为它超出了定义的运行时间(超过 120 秒)。
我也试过这个:
$inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
$sheetnames = ['Data Sheet #1','Data Sheet #3'];
/** Create a new Reader of the type defined in $inputFileType **/
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Advise the Reader of which WorkSheets we want to load **/
$reader->setLoadSheetsOnly($sheetnames);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $reader->load($inputFileName);
但这只会将指定的 sheets 复制到新文件。
编辑: 我尝试使用以下代码复制不需要编辑的 sheets:
$spreadsheet1 =\PhpOffice\PhpSpreadsheet\IOFactory::load("./sampleData/example1.xls");
$clonedWorksheet = clone $spreadsheet1->getSheetByName('Data Sheet #2 ');
$clonedWorksheet->setTitle('Test');
$spreadsheet->addSheet($clonedWorksheet);
但现在我又收到另一个错误:
Fatal error: Uncaught Error: Call to a member function getCell() on null in C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Calculation\Calculation.php:2785 Stack trace: #0 C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Cell\Cell.php(262): PhpOffice\PhpSpreadsheet\Calculation\Calculation->calculateCellValue(Object(PhpOffice\PhpSpreadsheet\Cell\Cell), true) #1 C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(1077): PhpOffice\PhpSpreadsheet\Cell\Cell->getCalculatedValue() #2 C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(1027): PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet->writeCell(Object(PhpOffice\PhpSpreadsheet\Shared\XMLWriter), Object(PhpOffice\PhpSpreadsheet\Worksheet\Worksheet), 'M7', Array) #3 C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(76): PhpOffice in C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Calculation\Calculation.php on line 2785
我认为这是因为使用克隆无法复制公式。这可能是原因吗?用公式复制 sheet 是另一种解决方案吗?
终于找到解决办法了。问题出在作者和公式上,所以将这一行添加到代码中,现在运行良好:
$writer->setPreCalculateFormulas(false);
所以现在编写器的所有代码是:
$writer = new Xlsx($spreadsheet);
$writer->setPreCalculateFormulas(false);
$writer->save('test.xlsx');