PHPExcel需要更长的时间才能在Excel文件中添加数据

时间:2017-05-29 07:31:43

标签: php phpexcel

我的数据库中有大约20000条记录。我正在获取该数据进程并将其添加到数组中,直到此处它工作正常但是当我向excel添加数据时需要更长的时间。我的代码在

之下
public function testexcel($result) {
    $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);
            $objPHPExcel->getActiveSheet()->setTitle('Panel');
            $alp = range('A', 'Z');
            $alp_count = 1;

            $columns = array(
                'A' => 'client_name',
                'B' => 'clientCircle',
                'C' => 'proj_name',
                'D' => 'state',
                'E' => 'city',
                'F' => 'pincode',
                'G' => 'address',
                'H' => 'branchName',
                'I' => 'panelIp',
                'J' => 'panelLocation',
                'K' => 'panelType',
                'L' => 'panelId',
                'M' => 'panelManufacturer',
                'N' => 'panelModel',
                'O' => 'panelCurrentVersion',
                'P' => 'siteId',
                'Q' => 'craCompany',
                'R' => 'mspCompany',
                'S' => 'hkCompany',
                'T' => 'installationVendor',
                'U' => 'antenna',
                'V' => 'subscription',
                'W' => 'dvr_user_id',
                'X' => 'dvr_password',
                'Y' => 'dvr_port',
            );

              $count = 1;
            foreach ($result as $k => $v) {
                foreach ($columns as $k1 => $v1) {

                    if (isset($v[$v1]))
                        $objPHPExcel->getActiveSheet()->setCellValueExplicit($k1 . $count, $v[$v1]);
                    else {
                        $objPHPExcel->getActiveSheet()->setCellValueExplicit($k1 . $count, "");
                    }
    //            }
                }

                $count++;
            }

            $date = date('Y/m/d h:i:s');
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="Panel-' . $date . '.xlsx"');
            header('Cache-Control: max-age=0');
            // If you're serving to IE 9, then the following may be needed
            header('Cache-Control: max-age=1');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            $objWriter->save('php://output');
}

请建议我如何优化代码,以便花费更少的时间在Excel中添加数据。 提前致谢

0 个答案:

没有答案