Php 我如何解决;“允许的内存大小已耗尽”;错误?

Php 我如何解决;“允许的内存大小已耗尽”;错误?,php,excel,phpspreadsheet,Php,Excel,Phpspreadsheet,我正在使用PHPSReadSheet,我想用4张纸修改一个xlsx文件。我只想在2张图纸中插入数据,但我想将所有4张图纸复制到新的xlsx文件中。当我这样做时,我会出现以下错误: 致命错误:允许的内存大小536870912字节已用尽(已尝试) 分配20480字节) 这是因为其中一张太重了,但那张很重的纸我只想复制它,不想修改它。我已尝试加载以下内容: ini_set('memory_limit', -1); 但它对我来说不起作用,因为它超出了定义的运行时(超过120秒) 我也试过这样做: $i

我正在使用PHPSReadSheet,我想用4张纸修改一个xlsx文件。我只想在2张图纸中插入数据,但我想将所有4张图纸复制到新的xlsx文件中。当我这样做时,我会出现以下错误:

致命错误:允许的内存大小536870912字节已用尽(已尝试) 分配20480字节)

这是因为其中一张太重了,但那张很重的纸我只想复制它,不想修改它。我已尝试加载以下内容:

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);
但这只会将指定的图纸复制到新文件中

编辑: 我尝试使用以下代码复制不需要编辑的工作表:

$spreadsheet1 =\PhpOffice\PhpSpreadsheet\IOFactory::load("./sampleData/example1.xls");
$clonedWorksheet = clone $spreadsheet1->getSheetByName('Data Sheet #2 ');
$clonedWorksheet->setTitle('Test');
$spreadsheet->addSheet($clonedWorksheet);
但现在我得到了另一个错误:

致命错误:未捕获错误:调用上的成员函数getCell() 无效 C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\phpspreadsheet\Calculation\Calculation.php:2785 堆栈跟踪:#0 C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\phpspreadsheet\Cell\Cell.php(262): PhpOffice\PhpSpreadsheet\Calculation\Calculation->calculateCellValue(对象(PhpOffice\PhpSpreadsheet\Cell\Cell), 正确)#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(对象(PhpOffice\PhpSpreadsheet\Shared\XMLWriter), 对象(PhpOffice\PhpSpreadsheet\Worksheet\Worksheet),'M7',数组)#3 C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\phpspreadsheet\Writer\Xlsx\Worksheet.php(76): 北京市政府办公室 C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\phpspreadsheet\Calculation\Calculation.php 在线2785


我认为这是因为克隆人无法复制公式。这是原因吗?复制带有公式的工作表是另一种解决方案吗?

我终于找到了解决方案。问题出在编写器和公式上,因此我们将这一行添加到代码中,现在它运行良好:

$writer->setPreCalculateFormulas(false);
因此,现在编写器的所有代码都是:

$writer = new Xlsx($spreadsheet);
$writer->setPreCalculateFormulas(false);
$writer->save('test.xlsx');

您是否尝试过一次打开一张工作表,复制需要复制的两张工作表,然后一次一张地访问其他工作表well@RiggsFolly我试图复制这两张工作表,现在我遇到了以下错误:*致命错误:未捕获错误:在null上调用成员函数getCell()**这不起作用。调用
writeToAllSheets()
时发生允许的内存问题。我正在尝试将我的多页工作簿转换为多页PDF。对我来说,只有当我将限制设置为(-1)时,内存问题才会消失,但之后作业似乎无休止地运行。这为我解决了这个问题。