以最短的执行时间生成大型 excel 文件

Generate large excel files with minimum excecution time

您好,我正在使用 PHPExcel 库在 excel 文件

中写入大量数据

我的header:

$objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A5', 'Sr. No.')
                     ->setCellValue('B5', 'Invoice No')
                     ->setCellValue('C5', 'Invoice Date')
                     ->setCellValue('D5', 'Zone')
                     ->setCellValue('E5', 'Customer Code')
                     ->setCellValue('F5', 'Customer Description')
                     ->setCellValue('G5', 'Item Code')
                     ->setCellValue('H5', 'Item Description')
                     ->setCellValue('I5', 'Picklist No.')
                     ->setCellValue('J5', 'Picklist Date')
                     ->setCellValue('K5', 'Start Serial No')
                     ->setCellValue('L5', 'End Serial No')
                     ->setCellValue('M5', 'Quantity')
                     ->setCellValue('N5', 'LR No.')
                     ->setCellValue('O5', 'LR Date')
                     ->setCellValue('P5', 'Transporter');

我将每个单元格设置为文本格式使用-我需要这个,因为我的记录有很大的数字字符串,我希望它们仅作为文本

$i=2;
for($s=$dd['start_serial_no'];$s<=$dd['end_serial_no'];$s++)
    {

    $objsheet->getStyle('A'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objsheet
    ->getStyle('B'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objsheet
    ->getStyle('C'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objsheet
    ->getStyle('D'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objsheet
    ->getStyle('E'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objsheet
    ->getStyle('F'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objsheet
    ->getStyle('G'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objsheet
    ->getStyle('H'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

    $objsheet
    ->getStyle('I'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objsheet
    ->getStyle('J'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objsheet
    ->getStyle('K'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objsheet
    ->getStyle('L'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objsheet
    ->getStyle('M'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objsheet
    ->getStyle('N'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objsheet
    ->getStyle('O'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

        $i++;
    }

然后我使用

为它们添加值
$i=2;    
for($s=$dd['start_serial_no'];$s<=$dd['end_serial_no'];$s++)
        {

            if($dd['record_status']=='Active')
            {

                $objPHPExcel->setActiveSheetIndex(0)
                          ->setCellValue('A'.$i, $i-5)
                          ->setCellValue('B'.$i, $dd['invoice_no'])
                          ->setCellValue('C'.$i, date("d/m/Y",strtotime($dd['invoice_date'])))
                          ->setCellValue('D'.$i, $dd['zone_name'])
                          ->setCellValue('E'.$i, $dd['cus_code'])
                          ->setCellValue('F'.$i, $dd['cus_desc'])
                          ->setCellValue('G'.$i, $dd['item_code'])
                          ->setCellValue('H'.$i, $dd['item_name'])
                          ->setCellValue('I'.$i, $dd['mrnpicklist_date'])
                          ->setCellValue('J'.$i, date("d/m/Y",strtotime($dd['mrnpicklist_date'])))
                          ->setCellValue('K'.$i, $s)
                          ->setCellValue('L'.$i, $dd['quantity'])
                          ->setCellValue('M'.$i, $dd['lr_no'])
                          ->setCellValue('N'.$i, date("d/m/Y",strtotime($dd['lr_date'])))
                          ->setCellValue('O'.$i, $dd['transporter_name']);

                $i++;
            }

    }

然后调整单元格大小

foreach(range('A','O') as $columnID) {
            $objsheet->getColumnDimension($columnID)
                ->setAutoSize(true);
        }

p.s.$dd['end_serial_no']-$dd['start_serial_no'] >50000 记录

写入 50000 条记录大约需要 400 秒

谁能告诉我如何减少执行时间。

开始

去掉

$i=2;
for($s=$dd['start_serial_no'];$s<=$dd['end_serial_no'];$s++)
    {

    $objsheet->getStyle('A'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objsheet
    ->getStyle('B'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

    ....

    $objsheet
    ->getStyle('O'.$i)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

    $i++;
}

并将其替换为一次性应用于整个单元格区域的样式设置

$objsheet->getStyle('A2:O'.($dd['end_serial_no'] - $dd['start_serial_no'] + 1))
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

但是如果您的值中有大量序列号,您最好还是使用 setCellValueExplicit() 而不是 setCellValue() 来设置这些单元格,并且不要假设将单元格格式代码设置为 TEXT 会自动强制将值存储为字符串....它不会,那是 setCellValueExplicit() 的工作......那么你甚至不需要设置格式掩码