<b>Fatal error</b>: Allowed memory size of 134217728 bytes exhausted (tried to allocate 78 bytes) in <b>/var/www/leanne/api/classes/PHPExcel/CachedObjectStorage/PHPTemp.php</b> on line <b>66</b><br />
你好,
几天前我问了这个问题,并被建议更改我的代码并使用单元缓存。虽然我更改了代码并尝试使用单元缓存,但仍然出现内存错误。我迫切希望找到解决方案。
谁能建议哪种缓存方法最适合编写 1 到 100,000 行数据的 Excel 文件?如果单元缓存不起作用,我可能需要使用另一个解决方案,该解决方案允许我以与 CSV 版本相同的方式附加到 xls 文件。
我当前的代码示例如下:
if ($count_prods > 0) {
$format = strtolower($export_data['output']);
$temp_file_location = '../temp/exports/products/';
$filename = 'data_' + $shop->ID . '_' . $export_id . '_test';
$separator = ',';
$endrow = "\n";
$fh = fopen($temp_file_location . $filename . '.csv', 'a');
/*$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array( ' memoryCacheSize ' => '8MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);*/
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_sqlite;
PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
$objPHPExcel = new PHPExcel();
$rowID = 2;
$counter = 1;
for ($i = 0; $i < $count_prods; $i += $batchlimit) {
$csv = '';
$limit = $batchlimit * $counter;
$start = $i + 1;
$productData = $productExport->getProductData($start, $limit);
if ($counter == 1) {
//get column names
if ($format == 'csv') {
$column_titles = implode(',', $productExport->product_fields);
$column_no = count($column_titles);
$csv = $column_titles . $endrow;
} else {
$objPHPExcel->getActiveSheet()->fromArray($productExport->product_fields, NULL, 'A1');
}
}
//loop through data export array
foreach ($productData as $product_id => $product_details) {
$columnID = 'A';
foreach ($product_details as $key => $value) {
if ($format == 'csv') {
$csv .= '"' . str_replace('"', '\'', $product_details[$key]) . '"' . $separator;
} else {
$objPHPExcel->getActiveSheet()->setCellValue($columnID . $rowID, $product_details[$key]);
}
$columnID++;
}
if ($format == 'csv') {
$csv = rtrim($csv, $separator);
$csv .= $endrow;
}
$rowID++;
}
if ($format == 'csv') {
fwrite($fh, $csv);
$csv = '';
}
$counter++;
}
if ($format == 'csv') {
fclose($fh);
}
//if XLS file
if ($format == 'xls') {
//$objPHPExcel = $objReader->load($temp_file_location . $filename . '.csv');
// $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
//$objWriter->save($temp_file_location . $filename . '.xls');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($temp_file_location . $filename . '.xlsx');
}