php xls导出文件到本地_使用PHPExcel将大文件导出到excel

bd96500e110b49cbb3cd949968f18be7.png

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

Below is my code:

($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');

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);

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

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');

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值