如何重命名包含图表的phpexcel表?

时间:2015-09-04 09:33:16

标签: php phpexcel

我在phpexcel的github页面尝试了this示例的轻微变化。 但我注意到,当我重命名生成的Excel工作表时,我没有在工作表中很好地获得图表,如图所示。不知道我哪里出错了。

enter image description here

这是我的代码:

<?php
require_once 'PHPExcel.php';
$objPHPExcel = new PHPExcel();

$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->setTitle('Performance'); // Chart Fails !
$objWorksheet->fromArray(
    array(
        array('', '', '', '', '', '', 'ClassA', 'ClassB'),
        array('', '', '', '', '', 'June',   81,   90),
        array('', '', '', '', '', 'July',   92,   91),
        array('', '', '', '', '', 'August', 81,   90),
    )
);
//  Set the Labels for each data series we want to plot
$dataseriesLabels = array(
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$G$1', null, 1),
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$H$1', null, 1)
);
//  Set the X-Axis Labels
$xAxisTickValues = array(
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$F$2:$F$4', null, 3),
);
//  Set the Data values for each data series we want to plot
$dataSeriesValues = array(
    new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$G$2:$G$4', null, 3),
    new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$H$2:$H$4', null, 3),
);
//  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 horizontal bar rather than a vertical 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('Performance');
$yAxisLabel = new PHPExcel_Chart_Title('Percentage');
//  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('A2');
$chart->setBottomRightPosition('E15');
//  Add the chart to the worksheet
$objWorksheet->addChart($chart);
// Save Excel 2007 file

header('Content-Type: application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename="performance.xlsx"'); 
header('Cache-Control: max-age=0');

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

1 个答案:

答案 0 :(得分:2)

等条目
'Worksheet!$G$1'

是标准的MS Excel范围引用..... !之前的“位”是工作表名称,而$G$1是该工作表中的单元格或单元格范围....因此,如果您的工作表被称为Performance,那么它应该是

'Performance!$G$1'

修改

如果某个范围内的工作表名称包含任何“特殊”字符(如空格),则MS Excel规则是必须使用单引号引用工作表名称,因此:

'Worksheet 1!$G$1

意味着您需要将字符串换成双引号(转义$符号以防止PHP尝试将$G$1解释为变量)

"'Worksheet 1'!\$G\$1"

或逃避单引号

'\'Worksheet 1\'!$G$1'

(包含特殊字符的字符串的标准PHP字符串处理)