PHPExcel图表已被Microsoft Excel删除

时间:2016-10-10 05:34:30

标签: excel phpexcel

我正在尝试通过PHPExcel在Excel文件上制作图表。

我可以为图表的datasource制作表格。看起来像这样

enter image description here

我正在为图表编写代码..看起来像这样..

$values = new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$1:$B$11', NULL, 11);
$categories = new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$1:$A$11', NULL, 11);
$series = new PHPExcel_Chart_DataSeries(
    PHPExcel_Chart_DataSeries::TYPE_LINECHART,        // plotType
    PHPExcel_Chart_DataSeries::GROUPING_STANDARD,    // plotGrouping
    range(0, count($values)-1),                      // plotOrder
    array(),                                         // plotLabel
    array($categories),                                // plotCategory
    array($values)                                // plotValues
);
$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
$plotarea = new PHPExcel_Chart_PlotArea(NULL, array($series));
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL,false);
$title = new PHPExcel_Chart_Title('Test Column Chart');
$yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');

$chart = new PHPExcel_Chart(
    'chart1',           // name
    $title,             // title
    $legend,            // legend
    $plotarea,          // plotArea
    true,               // plotVisibleOnly
    0,                  // displayBlanksAs
    NULL,               // xAxisLabel
    $yAxisLabel         // yAxisLabel
);
$objPHPExcel->getActiveSheet()->addChart($chart);

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
$objWriter->setIncludeCharts(TRUE);

header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="aOSalesPerformance.xlsx"');
$objWriter->save('php://output');

当我尝试打开由该代码生成的文档时,这是excel给我的错误

enter image description here

为什么会出现此错误? Excel为什么说绘图形状已被删除? 任何帮助appriciated

1 个答案:

答案 0 :(得分:0)

您收到该错误的主要原因是您过度选择了绘图值并包含了标题。例如,使用这两行,范围应从A2和B2开始,因为A1和B1包含要绘制的标题和NOT值。

所以这两行:

$values = new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$1:$B$11', NULL, 11);
$categories = new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$1:$A$11', NULL, 11);

应该是:

$values = new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$11', NULL, 11);
$categories = new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$11', NULL, 11);

这将处理错误,但您的图表仍然不会显示,因为您还需要指定要显示的图表的位置。因此,还需要添加这两个额外的行:

$chart->setTopLeftPosition('A20');
$chart->setBottomRightPosition('L50'); 

这将处理您收到的错误以及缺少显示的图表。但是,我建议在创建$ series对象之外创建$ values和$ categories的数组,因为这样可以绘制多于一列的数据。 This website has a good tutorial on creating charts using PHPExcel

更正后的代码:

$values = array();
$values[] = new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$11', NULL, 11);
$categories = array();
$categories[] = new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$11', NULL, 11);
$series = new PHPExcel_Chart_DataSeries(
    PHPExcel_Chart_DataSeries::TYPE_LINECHART,        // plotType
    PHPExcel_Chart_DataSeries::GROUPING_STANDARD,    // plotGrouping
    range(0, count($values)-1),                      // plotOrder
    null,                                         // plotLabel
    $categories,                                // plotCategory
    $values                               // plotValues
);
$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
$plotarea = new PHPExcel_Chart_PlotArea(NULL, array($series));
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL,false);
$title = new PHPExcel_Chart_Title('Test Column Chart');
$k="y axis";
$yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');

$chart = new PHPExcel_Chart(
    'chart1',           // name
    $title,             // title
    $legend,            // legend
    $plotarea,          // plotArea
    true,               // plotVisibleOnly
    0,                  // displayBlanksAs
    NULL,               // xAxisLabel
    $yAxisLabel         // yAxisLabel
);

//Location where chart will be displayed
$chart->setTopLeftPosition('A20');
$chart->setBottomRightPosition('L50'); 

$objPHPExcel->getActiveSheet()->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/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="aOSalesPerformance.xlsx"');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('php://output');