我需要使用excel到pdf的图表生成报告。我尝试使用phpexcel,但无法加载图表。它只生成带表的PDF。这是我在phpexcel中的代码。
$newpdf = "report-".date('m_d_Y_H_i_s', strtotime('now')).".pdf";
$objPHPExcel1 = new PHPExcel();
$objPHPExcel1 = PHPExcel_IOFactory::load('csv/'.$filename);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setFitToWidth(true);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setFitToHeight(true);
$objPHPExcel1->getActiveSheet()->setShowGridLines(false);
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="'.$newpdf.'"');
header('Cache-Control: max-age=0'); //no cache
$objWriter1 = new PHPExcel_Writer_PDF($objPHPExcel1);
$objWriter1->save('php://output');
exit;
我还尝试使用jchartfx从HTML加载内容,但没有成功。有什么想法?
提前致谢。
答案 0 :(得分:0)
PHPExcel只能从Excel2007(OfficeOpenXML .xlsx)文件中读取图表,并且只有在您明确告知它时才会加载它们。
if (PHPExcel_IOFactory::identify('csv/'.$filename) == "Excel2007") {
$objReader = PHPExcel_IOFactory::createReader("Excel2007");
$objReader->setIncludeCharts(TRUE);
} else {
die("Can't load charts");
}
$objPHPExcel = $objReader->load('csv/'.$filename);
写入PDF文件时,必须安装相关的库(在您的情况下为tcpdf)并告诉PHPExcel您要使用它以及安装tcpdf的目录。
$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
$rendererLibrary = 'tcPDF5.9';
$rendererLibraryPath = '/php/libraries/PDF/' . $rendererLibrary;
if (!PHPExcel_Settings::setPdfRenderer(
$rendererName,
$rendererLibraryPath
)) {
die('NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
EOL .
'at the top of this script as appropriate for your directory structure'
);
}
您还需要安装jpgraph,PHPExcel需要知道它的安装位置
$rendererName = PHPExcel_Settings::CHART_RENDERER_JPGRAPH;
$rendererLibrary = 'jpgraph3.5.0b1/src';
$rendererLibraryPath = '/php/libraries/Charts/' . $rendererLibrary;
if (!PHPExcel_Settings::setChartRenderer(
$rendererName,
$rendererLibraryPath
)) {
die('NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
PHP_EOL .
'at the top of this script as appropriate for your directory structure'
);
}
最后,您需要明确告诉PDF Writer生成图表
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save($outputFileName);