我正在尝试使用 Zend Framework 和 PHPExcel 编写 Excel2007 文件。我知道我可以增加 PHP 内存限制.. 但没有其他办法吗?
到目前为止,这是我的代码,它适用于 5000 行和 77 列,但我需要 12000 行 :-) 内存设置为 128MB
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_sqlite;
PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$phpExcel = new PHPExcel();
// set headers
$select = $db->select();
$select->from('IMPORT')->limit(1);
$data = $select->query()->fetchAll();
$columns = array_keys($data[0]);
$phpExcel->setActiveSheetIndex(0);
$colCNT = 1;
foreach ($columns as $column) {
$letter = Nc_Utils::getNameFromNumber($colCNT);
$phpExcel->getActiveSheet()->SetCellValue($letter . '1', $column);
$colCNT++;
}
unset($select);
unset($data);
$sql = 'SELECT * FROM IMPORT LIMIT 7000';
$stmt = $db->query($sql);
$rowCNT = 2;
while($rows = $stmt->fetch()){
$phpExcel->getActiveSheet()->fromArray($rows, null, 'A'.$rowCNT);
$rowCNT++;
}
unset($rowCNT);
unset($select);
unset($db);
// MEMORY USAGE = 4 MB!
$phpExcelWrite = new PHPExcel_Writer_Excel2007($phpExcel);
$phpExcelWrite->setUseDiskCaching(true);
$phpExcelWrite->save(str_replace('.php', '.xls', __FILE__));
PHP 致命错误:第 102 行 /Applications/MAMP/htdocs/phpexcel/library/PHPExcel/Shared/XMLWriter.php 中允许的内存大小为 134217728 字节已用尽(尝试分配 13878925 字节)