我正在为我的报告使用模板。我只是在编辑单元格以获得所需的值。
我正在使用 PHPExcel 和 Yii 框架。
public function generateSalesOrderReportExcel($id)
{
set_time_limit(0);
$templateFile = Yii::getPathOfAlias('webroot').'/path/to/file.xlsx';
$ext = strtolower(array_pop(explode(".", $templateFile)));
$writerType = ($ext == "xls") ? 'Excel5' : 'Excel2007';
Yii::import('ext.phpexcel.XPHPExcel');
XPHPExcel::init();
/* Cell caching to reduce memory usage. */
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = array('memoryCacheSize'=>'8MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$objReader = PHPExcel_IOFactory::createReader($writerType);
$objReader->setReadDataOnly(false);
/* check if the the file is readable */
if (!$objReader->canRead($templateFile))
return false;
/* load only the first sheet */
$sheets = $objReader->listWorksheetNames($templateFile);
$objReader->setLoadSheetsOnly(array($sheets[0]));
$objPHPExcel = $objReader->load($templateFile);
$objPHPExcel->setActiveSheetIndex(0);
// save the sales order report then download
$fileName = 'SalesOrderReport-'.date('Ymd').'-'.time().'.xlsx';
$filePath = Yii::getPathOfAlias('webroot').'/tmp/'.$fileName;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $writerType);
$objWriter->save($filePath);
Yii::app()->getRequest()->sendFile($fileName, @file_get_contents($filePath));
Yii::app()->end();
}
我的问题是现有模板的设计在我将其保存到新文件时未呈现。