我在尝试将数据附加到PHP的Excel文件时遇到了一些麻烦。我使用PHPExcel,代码如下。
问题是:当文件足够小时,它工作得很好。但随后文件开始变大,我开始得到允许的内存大小耗尽错误。所以,我不是每次都从头开始编写文件,而是只获取新数据并附加到文件中。但由于某种原因,它没有写入文件。
有没有人有想法?
function get_subscriptions($process, $filename){
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
//define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
/** Include PHPExcel */
require_once dirname(__FILE__) . '/PHPExcel.php';
require_once dirname(__FILE__) . '/PHPExcel/IOFactory.php';
// Create new PHPExcel object
//$objPHPExcel = new PHPExcel();
$objPHPExcel = PHPExcel_IOFactory::load(dirname(__FILE__) . "/".$filename);
$objPHPExcel->setActiveSheetIndex(0);
$objSheet = $objPHPExcel->getActiveSheet();
$last_line = $objSheet->getHighestRow()+1;
$last_id = $objSheet->getCellByColumnAndRow(0, $last_line)->getCalculatedValue();
// Set document properties
$objPHPExcel->getProperties()->setCreator("FHGV")
->setLastModifiedBy("FHGV")
->setTitle("Report")
->setSubject("PSS Report")
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'ID')
->setCellValue('B1', 'Date')
->setCellValue('C1', 'Name')
->setCellValue('D1', 'CPF')
->setCellValue('E1', 'RG')
->setCellValue('F1', 'ZIP')
->setCellValue('G1', 'Address')
->setCellValue('H1', 'No')
->setCellValue('I1', 'Reference')
->setCellValue('J1', 'City')
->setCellValue('K1', 'State')
->setCellValue('L1', 'Other')
->setCellValue('M1', 'Email')
->setCellValue('N1', 'Phone')
->setCellValue('O1', 'Cell Phone')
->setCellValue('P1', 'Birth Date')
->setCellValue('Q1', 'Company')
->setCellValue('R1', 'Position')
global $wpdb;
$array = $wpdb->get_results("SELECT id, date_added as 'date',
(select value from wp_iphorm_form_entry_data dados where element_id = 1 and entry_id = id) as 'name'
...
FROM wp_iphorm_form_entries subscription
WHERE subscription.post_title = \"".$process."\" AND id>".$last_id);
$row = $last_line+1; $col = 0;
foreach($array as $data){
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->id);
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->date);
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->name);
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->cpf);
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->rg);
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->zip);
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->address);
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->no);
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->reference);
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->city);
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->state);
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->others);
$objSheet->setCellValueByColumnAndRow($col++, $row, strip_tags($data->email));
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->phone);
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->cell_phone);
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->birth_date);
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->company);
$objSheet->setCellValueByColumnAndRow($col++, $row, $data->position);
$col=0;$row++;
}
for($col = 'A'; $col !== 'S'; $col++) {
$objPHPExcel->getActiveSheet()
->getColumnDimension($col)
->setAutoSize(true);
}
// Rename worksheet
$objSheet->setTitle('Subscriptions');
// Save Excel 2007 file
$callStartTime = microtime(true);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
// Renaming the file and saving it
$objWriter->save(str_replace('functions.php', $filename , __FILE__));
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
}
答案 0 :(得分:0)
在追加数据之前,您正在阅读整个电子表格。这意味着PHPExcel会加载内存中的所有数据。如果您没有太多数据但停止为更大的电子表格工作,它会很有效。 因为您的程序只有有限的可用内存,所以整个数据集不适合内存,因此程序崩溃。
或者,您可以查看如何使用Spout执行此操作。 Spout专门用于解决这些内存不足错误。您可以在此处找到文档:https://github.com/box/spout/wiki/Add-data-to-an-existing-spreadsheet