使用PHPExcel将大文件导出到excel

使用PHPExcel将大文件导出到excel

本文介绍了使用PHPExcel将大文件导出到excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHPExcel库在PHP(Laravel4)中导出大约40,000行Mysql数据.下面是我的代码:

I am trying to export around 40,000 rows of Mysql data in PHP(Laravel4) using PHPExcel library.Below is my code:

($ patList是结果列的数组)

($patList is an array of result columns)

set_time_limit ( 3000 );

$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;

$cacheSettings = array( 'memoryCacheSize' => -1);

PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

$objPHPExcel = new PHPExcel();
$i = 1;
$patList = $result[0];

for ($r = 0; $r < count($patList); $r++) {

    $objPHPExcel->setActiveSheetIndex(0)

         ->setCellValue("A$i", $patList[$r][0])
         ->setCellValue("B$i", $patList[$r][1])
         ->setCellValue("C$i", $patList[$r][2])
         ->setCellValue("D$i", $patList[$r][1])
         ->setCellValue("E$i", $patList[$r][2])
         ->setCellValue("F$i", $patList[$r][1])
         ->setCellValue("G$i", $patList[$r][2])
         ->setCellValue("H$i", $patList[$r][2])
         ->setCellValue("I$i", $patList[$r][1])
         ->setCellValue("J$i", $patList[$r][2])
         ->setCellValue("K$i", $patList[$r][5]);
     $i++;
}

$objPHPExcel->getActiveSheet()->setTitle('Name of Sheet 1');

header('Content-Type: application/vnd.ms-excel');

header('Content-Disposition: attachment;filename="result.xls"');

header('Cache-Control: max-age=0');

ob_clean();

flush();

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

$objWriter->save('php://output');

如果excel中有3-4列和15,000行,则上面的代码可以正常运行.但是,如果我增加编号.行到30,000或否.到10列时,excel不会生成.

The above code runs fine if there are 3-4 columns in the excel and 15,000 rows. However, if I increase the no. of rows to 30,000 or the no. of columns to 10, the excel doesn't get generated.

推荐答案

$cacheSettings = array( 'memoryCacheSize' => -1);

不明智....我什至不知道使用-1值是否可行;但是,如果这样做的话,那将意味着您将所有内容都存储在内存中,而没有任何内容存储在php://temp

isn't sensible.... I don't even know if using a value of -1 will work; but if it does, it will mean that you're storing everything in memory and nothing in php://temp

memoryCacheSize值告诉高速缓存流,在将数据切换到php://temp之前,应在内存中存储多少数据;

The memoryCacheSize value tells the cache stream how much data should be stored in memory before switching data out to php://temp ; so

$cacheSettings = array( 'memoryCacheSize' => '8MB');

会告诉缓存流使用8MB的内存,然后是否需要存储其他数据才能使用php://temp

would tell the cache stream to use 8MB of memory, and then if additional data needed to be stored to use php://temp instead

这篇关于使用PHPExcel将大文件导出到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 18:57