输出到浏览器时 phpexcel 图表文件损坏

phpexcel chart file corrupt when outputted to browser

我在将文件保存到服务器时没有收到 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', NULL, 1),   
    );
    //  Set the X-Axis Labels

    $xAxisTickValues = array(
        new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A:$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:$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');
}

我已经尝试解决这个问题一段时间了,一切似乎都还好。

为什么在浏览器中输出时会导致文件损坏?

在一秒钟内创建图表的解决方案sheet,替换代码中的行:

...
    //  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);
...

如果仍然存在问题,请检查在发送 header 之前是否向浏览器发送了任何输出。

编辑:问题出在 header 之内。至少,先尝试保存文件,然后阅读它:

 // 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');