加快导出phpExcel的时间

时间:2016-08-16 15:20:41

标签: php phpexcel export-to-excel

我需要一个帮助,我有很多报告有10到30列和20000或更多行,mi驱动程序是mongodb并且结果查询非常快,但是当创建带有库PHPExcel的文档表时太慢或者大约十分钟或多一点时间,我的代码只有一个bucle:

    $row++;
    $contUser = 0;
    foreach ($data as $index => $objUser) {
        $backgroundColor = $colorOdd;
        if ($contUser % 2 == 0) {
            $backgroundColor = $colorPair;
        }
        $field = $objUser->getFields();
        $column = 0;
        //case 'N°':
        $sheet->setCellValueByColumnAndRow($column, $row, $contUser + 1);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));

        $column++;
        //case matricula:
        $sheet->setCellValueByColumnAndRow($column, $row, $objUser->getChrUsername());
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        //case matricula:
        $sheet->setCellValueByColumnAndRow($column, $row, $field['appaterno']['value']);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        //case matricula:
        $sheet->setCellValueByColumnAndRow($column, $row, $field['apmaterno']['value']);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        //case matricula:
        $sheet->setCellValueByColumnAndRow($column, $row, $objUser->getChrFirstname());
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));

        //$field['dni']['value']
        $column++;
        $type = PHPExcel_Cell_DataType::TYPE_STRING;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($field['dni']['value'], $type);
        //$sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        //$field['sociedad']['value']
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($field['sociedad']['value'], $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($field['gerenciacentral']['value'], $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($field['division']['value'], $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($field['area']['value'], $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($field['servicio']['value'], $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($field['funcion']['value'], $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($field['fecha']['value'], $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($field['divpersonal']['value'], $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($field['subpersonal']['value'], $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($field['superior']['value'], $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($field['matsuperior']['value'], $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($field['estado']['value'], $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($field['iniestado']['value'], $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($field['finestado']['value'], $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($objUser->getChrEmail(), $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit(empty($field['agencia']['value'])?'':$field['agencia']['value'], $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));

        $date = '';
        $time = '';
        if(!empty($this->lastAccessQuiz) && $this->lastAccessQuiz>0){
            $date = date("Y-m-d", $this->lastAccessQuiz);
            $time = date("H:i:s", $this->lastAccessQuiz);
        }
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($date, $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $column++;
        $sheet->getCellByColumnAndRow($column, $row)->setValueExplicit($time, $type);
        $sheet->getColumnDimensionByColumn($column)->setAutoSize(true);
        $sheet->getStyleByColumnAndRow($column, $row)->applyFromArray($styleArray);
        $sheet->getStyleByColumnAndRow($column, $row)->getFill()->applyFromArray(array('type' => \PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('rgb' => $backgroundColor)));
        $row++;
        $contUser++;
    }

1 个答案:

答案 0 :(得分:0)

首先,不要为您编写的每个单元格设置列样式(例如setAutoSizing为true)....编写所有数据,然后对该列应用自动调整大小。

与细胞样式相似......它们都是相同的,所以不要将它们应用于每个单独的细胞,但是在完成单个细胞的编写后应用于整个范围。 如果它是行的背景颜色,则应用于行范围,但不是每个单独的单元格。