加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

使用PHPExcel将大文件导出到Excel

发布时间:2020-12-13 22:53:48 所属栏目:PHP教程 来源:网络整理
导读:我试图使用 PHPExcel库在 PHP(Laravel4)中导出大约40,000行Mysql数据. 以下是我的代码: ($patList是结果列的数组) set_time_limit ( 3000 );$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;$cacheSettings = array( 'memoryCacheS
我试图使用 PHPExcel库在 PHP(Laravel4)中导出大约40,000行Mysql数据.
以下是我的代码:

($patList是结果列的数组)

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不会生成.

解决方法

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

是不明智的….我甚至不知道使用值-1是否有效;但如果确实如此,则意味着你将所有内容存储在内存中,而不是在php:// temp中

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

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

会告诉缓存流使用8MB的内存,然后如果需要存储额外的数据来使用php:// temp

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读