我想生成一个 Excel 表,其中包含一些默认字段和可变数量的评论字段以及与评论相关的数据。我想为评论字段设置特定的宽度和换行文本。现在我正在使用这段代码
$excelObj = new \PHPExcel();
$ews = $excelObj->getSheet(0);
$ews->setTitle('SurveyDetails');
$ews->fromArray($header, ' ', 'A1'); //Write the header from array
$ews->fromArray($content_arr, ' ', 'A2'); // Write the content from array
$header_style = array(
'font' => array('bold' => true,),
'alignment' => array('horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER, 'vertical' => \PHPExcel_Style_Alignment::VERTICAL_CENTER),
);
$content_style = array(
'alignment' => array('horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER, 'vertical' => \PHPExcel_Style_Alignment::VERTICAL_CENTER),
);
$start_end_columns = $excelObj->setActiveSheetIndex(0)->calculateWorksheetDimension();
preg_match_all('!\d+!', $start_end_columns, $matches);
$numbers = implode(':', $matches[0]);
$columns = explode(":", $numbers);
$header_end_column_no = $columns[1];
$header_start_end = str_replace($header_end_column_no, 1, $start_end_columns); // A1:G1
$ews->getStyle($header_start_end)->applyFromArray($header_style);
$start_end_columns = str_replace("A1", "A2", $start_end_columns); // Start column of content changed A1 to A2
$ews->getStyle($start_end_columns)->applyFromArray($content_style);
$cols = explode(":", $start_end_columns);
$endColmn = $cols[1];
$endColmn = preg_replace('/[0-9]+/', '', $endColmn);
$activeSheetObj = $excelObj->getActiveSheet();
//Set the autosize height for all the cells
$activeSheetObj->getDefaultRowDimension()->setRowHeight(-1);
$this->log(sprintf("Before set hyper link excel projectId : %s, surveyId : %s", $projectId, $survey_id), LogLevel::INFO);
//Set the hyperlink in the path field
$i = 2;
$count = count($content_arr);
foreach ($content_arr as $content) {
$activeSheetObj->getCell('F' . $i)->getHyperlink()->setUrl($content['path']);
$i++;
}
$activeSheetObj->getStyle('G'.'2:'.'G'.$count)->getAlignment()->setWrapText(true);
$this->log(sprintf("After setting hyper link projectId : %s, surveyId : %s", $projectId, $survey_id), LogLevel::INFO);
//Set specific width for the note and comment fields
$activeSheetObj->getColumnDimension('G')->setWidth(35);
$l = 0;
if (strcmp('G', $endColmn) != 0) {
for ($col = 'J'; $col != $endColmn; $col++) {
if ($l % 3 == 0) {
$activeSheetObj
->getColumnDimension($col)
->setWidth(35);
$activeSheetObj->getStyle($col.'2:'.$col.$count)->getAlignment()->setWrapText(true);
}
$l++;
}
$activeSheetObj->getColumnDimension($endColmn)->setWidth(35);
$activeSheetObj->getStyle($endColmn.'2:'.$endColmn.$count)->getAlignment()->setWrapText(true);
}
unset($content_arr);
$this->log(sprintf("After setting width projectId : %s, surveyId : %s", $projectId, $survey_id), LogLevel::INFO);
$writer = \PHPExcel_IOFactory::createWriter($excelObj, 'Excel2007');
//Save to perticular location
$keyname = "videos/Export/" . $projectId . "/" . $fileName;
$writer->save('/home/senchu/Documents/Infrass.xlsx');
但完成 10 列和 400 行的过程大约需要 3 秒。有什么办法可以优化这段代码以提高性能。当我尝试缓存时,我没有得到太多的性能提升。
而不是迭代每一行并设置超链接
$activeSheetObj->getCell('F' . $i)->getHyperlink()->setUrl($content['path']);
有没有什么方法可以从数组中设置超链接。这样我们就可以避免这么多的迭代次数。