嗨我正在使用PHPExcel库在excel文件中编写大数据
我的标题:
$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
记录
大约需要400秒来写50000条记录
有人可以告诉我如何减少执行时间。
答案 0 :(得分:1)
开始
摆脱
$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()
的作业......那么你甚至不需要设置格式掩码