我试图用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致命错误:允许的内存大小为134217728字节已用尽
(尝试分配13878925字节)在
/Applications/MAMP/htdocs/phpexcel/library/PHPExcel/Shared/XMLWriter.php
在第102行

最佳答案

我尝试使用PhpExcel,但发现了与您相同的问题。
我已经尝试了所有建议以减少内存消耗(链接Klinky在他的答案中张贴),但仅提高了25-30%。我什至考虑过要创建CSV输出而不是Excel。

现在,我正在使用PEAR的Spreadsheet_Excel_Writer,并且对于大致相同的数据量(11k行62列),它对我来说很好用,尽管生成文件需要花费一些时间。

它没有PhpExcel强大,但是正如我在您的示例中看到的那样,您没有使用它来设置单元格样式和编写函数,对吗?

例:

$workbook = new Spreadsheet_Excel_Writer();
$workbook->send($filename . '.xls');
$workbook->setVersion(8); // excel 8
// Create worksheet
$worksheet =& $workbook->addWorksheet('submissions');
$worksheet->setInputEncoding('UTF-8');
// Write some data on Sheet 1
$row = 0;
$worksheet->write($row, 0, 'Poster #ID');
$worksheet->write($row, 1, 'Surname');
$worksheet->write($row, 2, 'Firstname');
$worksheet->write($row, 3, 'Country');
$worksheet->write($row, 4, 'E-mail');
$worksheet->write($row, 5, 'All authors');
$worksheet->write($row, 6, 'Institutions');
$worksheet->write($row, 7, 'Abstract topic');
$worksheet->write($row, 8, 'Abstract title');
foreach ($records as $rec_id => $rec_data) {
    $row++;
    $worksheet->write($row, 0, 'P-' . $rec_data['id']);
    $worksheet->write($row, 1, $rec_data['submitter_surname']);
    $worksheet->write($row, 2, $rec_data['submitter_firstname']);
    $worksheet->write($row, 3, $rec_data['submitter_country']);
    $worksheet->write($row, 4, $rec_data['submitter_email']);
    $worksheet->write($row, 5, $rec_data['authors']);
    $worksheet->write($row, 6, $rec_data['institutions']);
    $worksheet->write($row, 7, $rec_data['abstract_topic']);
    $worksheet->write($row, 8, $rec_data['abstract_title']);
}
$workbook->close(); // output

关于php - PHPExcel内存问题-还有其他想法吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12251459/

10-11 14:33