记录一笔关于PHPEXCEL导出大数据超时和内存溢出的问题

通过查阅资料可以找到PHPEXCEL本身已经有通过缓存来处理大数据的导出了。但是昨晚一直没有成功,这可捉急了。最后想来想去就替换了phpExcel的版本了。最后就成功了。话不多说,代码附上

<?php
    set_time_limit(0);
    header("Content-Type:text/html;charset=utf-8");
    define('EOL', '<br />');
    define('CURSCRIPT', 'shell');
    define('WEB',dirname(__FILE__).DIRECTORY_SEPARATOR);
    define ( 'ROOT_PATH', dirname (WEB) . DIRECTORY_SEPARATOR . 'application' . DIRECTORY_SEPARATOR );
    define ( 'FRAMEWORK_PATH', substr ( ROOT_PATH, 0, - 12 ) . 'jcphp' . DIRECTORY_SEPARATOR );
    define ( 'WXSERVER_PATH', dirname (WEB) . DIRECTORY_SEPARATOR . 'wwwroot' . DIRECTORY_SEPARATOR.'wxserver'.DIRECTORY_SEPARATOR);
    require FRAMEWORK_PATH . './class/class_core.php';
    class_core::instance (false);
    $rootexcel ='Classes/PHPExcel.php';
    require_once($rootexcel);
    $column_number = 14;
    $objPHPExcel = new PHPExcel();// 创建一个处理对象实例
     $objActSheet = $objPHPExcel->getActiveSheet();
    ini_set("memory_limit", "1024M"); // 设置php可使用内存
    $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
    if (!PHPExcel_Settings::setCacheStorageMethod($cacheMethod)) {
        die($cacheMethod . " 缓存方法不可用" . EOL);
    }
    echo date('H:i:s'), " 当前使用的缓存方法是: ", $cacheMethod, " 方式", EOL;
    echo date('H:i:s'), " 开始设置文档属性", EOL;
   $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
            ->setLastModifiedBy("Maarten Balliauw")
            ->setTitle("Office 2007 XLSX Test Document")
            ->setSubject("Office 2007 XLSX Test Document")
            ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
            ->setKeywords("office 2007 openxml php")
            ->setCategory("Test result file");


    echo date('H:i:s'), " 开始添加单元格标题", EOL;
    $objPHPExcel->setActiveSheetIndex(0);
    $title = '学员数据';
    //设置表格标题
    $objActSheet->setCellValue('A1', $title);
    //设置字体大小
    $objActSheet->getStyle('A1')->getFont()->setName('宋体');
    $objActSheet->getStyle('A1')->getFont()->setSize(40);
    $objActSheet->getStyle('A1')->getFont()->setBold(true);
     //合并标题单元格
    $objActSheet->getStyle('B2')->getAlignment()->applyFromArray(
     array(
         'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
         'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
         'rotation' => 0,
         'wrap' => true
         ) );
    $styleArray1 = array(
      'font' => array(
        'bold' => true,
        'size'=>12,
        'color'=>array(
         'argb' => '00000000',
         ),
        ),
      'alignment' => array(
        'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        ),
      );
    $styleArray2 = array(
      'font' => array(
        'bold' => true,
        'size'=>12,
        'color'=>array(
          'argb' => '00000000',
          ),
        ),
      'alignment' => array(
        'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        ),
      );
    $arr=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N');
    // 将A1单元格设置为加粗,居中
    $objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray1);
    $objActSheet->mergeCells( 'A1:'.chr(ord('A')+$column_number-1).'1');//合并标题
    $objActSheet->setCellValue('A2', '编号');
    $objActSheet->setCellValue('B2', '学员姓名');
    $objActSheet->setCellValue('C2', '性别');
    $objActSheet->setCellValue('D2', '身份证号');
    $objActSheet->setCellValue('E2', '手机号');
    $objActSheet->setCellValue('F2', '学员状态');
    $objActSheet->setCellValue('G2', '教练');
    $objActSheet->setCellValue('H2', '训练车辆');
    $objActSheet->setCellValue('I2', '联系地址');
    $objActSheet->setCellValue('J2', '报名日期');
    $objActSheet->setCellValue('K2', '招生人');
    $objActSheet->setCellValue('L2', '过科一日期');
    $objActSheet->setCellValue('M2', '过科二日期');
    $objActSheet->setCellValue('N2', '过科三日期');
    foreach ($arr as  $v) {            
       $objPHPExcel->getActiveSheet()->getStyle($v.'2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);// 居中
       $objPHPExcel->getActiveSheet()->getStyle($v.'2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);           
       $objPHPExcel->getActiveSheet()->getColumnDimension($v)->setWidth(50);// 单元格宽度
       $objPHPExcel->getActiveSheet()->getStyle($v.'2')->applyFromArray($styleArray2);
   }
   $styleArray = array(
    'borders' => array(
        'allborders' => array(
                    //'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的
                    'style' => PHPExcel_Style_Border::BORDER_THIN,//细边框
                    //'color' => array('argb' => 'FFFF0000'),
                    ),
        ),
    );
    $i=3;
    $xueyuanModel = model('kehu');
    $where = " mdid=28 ";
    $data = $xueyuanModel->get(array('*'),$where,0,10000);
       if($data['count']){
        $scoreModel = model('score');
        foreach ($data['data'] as $k => $rk) {
            $sex = $rk['UserSex']==1?'男':'女';
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $rk['Id']);
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $rk['UserName']);
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $sex);
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, "`".$rk['sfz']);
            $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $rk['UserMobile']);
            $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, xyztdesc($rk['xyzt']));
            $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, showtype2('jc_user','TrueName','Id',$rk['jlid']));
            $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, showtype2('jc_car','chepai','id',$rk['carID']));
            $objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $rk['UserAddr']);
            $objPHPExcel->getActiveSheet()->setCellValue('J' . $i, date('Y-md',strtotime($rk['bmtime'])));
            $objPHPExcel->getActiveSheet()->setCellValue('K' . $i, showtype2('jc_user','TrueName','Id',$rk['addid']));
            //科目一,科目二,科目三通过时间
            $suba = array('L'=>1,'M'=>2,'N'=>3); 
            foreach($suba as $sk=>$sv){
                $scorerow = $scoreModel->get(array('testtime')," uId='".$rk['Id']."' AND score=1 AND subtype=".$sv."");
                $objPHPExcel->getActiveSheet()->setCellValue($sk . $i, $scorerow['testtime']);
            }            
            foreach ($arr as $va) {
               $objPHPExcel->getActiveSheet()->getStyle($va.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle($va.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
           }
           $objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(20);
           $i++;
       }
   }
  $j=$i-1;
  $objPHPExcel->getActiveSheet()->getStyle('A1:H'.$j)->applyFromArray($styleArray); 
   #######################################################################################################

    $objPHPExcel->getActiveSheet()->setTitle('客户信息');
    echo date('H:i:s'), " 设置格式为Excel2007版格式", EOL;
    $callStartTime = microtime(true);

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
    $callEndTime = microtime(true);
    $callTime = $callEndTime - $callStartTime;
    echo date('H:i:s'), " 设置生成的文件为: ", str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)), EOL;
    echo date('H:i:s'), ' 写入Workbook中耗时 ', sprintf('%.4f', $callTime), " 秒", EOL;
    echo date('H:i:s'), ' 当前内存使用情况: ', (memory_get_usage(true) / 1024 / 1024), " MB", EOL;
    echo date('H:i:s'), " 内存使用峰值: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;
    echo date('H:i:s'), " 完成写入文件", EOL;
    echo date('H:i:s'), ' 文件被创建在: ', getcwd(), '目录', EOL;

 

转载于:https://www.cnblogs.com/YangJieCheng/p/7339473.html

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值