将文件保存到服务器时没有收到corruption
消息。
但是将文件输出到浏览器时,我收到"We found a problem with some content.... "
消息。
虽然显示了数据和图表。
代码如下:
public function plotChartAndExport($x_data, $y_data, $row_count)
{
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Asia/Kathmandu');
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
for($i=1; $i<=$row_count; $i++)
{
$objWorksheet->setCellValue('A'.$i, $x_data[$i-1]);
$objWorksheet->setCellValue('B'.$i, $y_data[$i-1]);
}
// Set the Labels for each data series we want to plot
$dataSeriesLabels = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1),
);
// Set the X-Axis Labels
$xAxisTickValues = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$'.$row_count, NULL, ($row_count-1)),
);
// Set the Data values for each data series we want to plot
$dataSeriesValues = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$'.$row_count, NULL, ($row_count-1)),
);
// Build the dataseries
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED, // plotGrouping
range(0, count($dataSeriesValues)-1), // plotOrder
$dataSeriesLabels, // plotLabel
$xAxisTickValues, // plotCategory
$dataSeriesValues // plotValues
);
// Set additional dataseries parameters
// Make it a vertical bar rather than a horizontal column graph
$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
// Set the series in the plot area
$plotArea = new PHPExcel_Chart_PlotArea(NULL, array($series));
// Set the chart legend
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
$title = new PHPExcel_Chart_Title('Test Bar Chart');
$yAxisLabel = new PHPExcel_Chart_Title('Hours');
// Create the chart
$chart = new PHPExcel_Chart(
'chart1', // name
$title, // title
$legend, // legend
$plotArea, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
NULL, // xAxisLabel
$yAxisLabel // yAxisLabel
);
// Set the position where the chart should appear in the worksheet
$chart->setTopLeftPosition('A'.($row_count+2));
$chart->setBottomRightPosition('H20');
// Add the chart to the worksheet
$objWorksheet->addChart($chart);
// // Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/force-download');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Test Report.xlsx"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Cache-Control: max-age=0');
// Save Excel 2007 file
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('php://output');
}
一段时间以来,我一直在尝试解决问题,一切似乎都很好。
为什么在浏览器中输出时导致文件损坏?
答案 0 :(得分:1)
在第二张工作表中创建图表的解决方案,替换代码中的线条:
...
// Set the position where the chart should appear in the worksheet
$chart->setTopLeftPosition('A1');
$chart->setBottomRightPosition('H20');
// Add the chart to the worksheet
$objPHPExcel->createSheet(1);
$objPHPExcel->setActiveSheetIndex(1)->setTitle('chart', false);
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel->setActiveSheetIndex(1)->addChart($chart);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(1);
...
如果仍然存在问题,请在发送标头之前检查是否将任何输出发送到浏览器。
edit:问题在标题内。至少,首先尝试保存文件,然后阅读:
// Save Excel 2007 file
$lsFileName = "Test Report.xlsx";
$lsSavePath = "some/folder/with/permission/to/save/". $lsFileName;
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save($lsSavePath);
$lnFilesize = filesize($lsSavePath);
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/force-download');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment; filename=" . utf8_decode($lsFileName));
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Cache-Control: max-age=0');
header("Content-Length:" . $lnFilesize);
readfile($lsSavePath);
// $objWriter->save('php://output');