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

How to rename the phpexcel sheet containing a chart in it?

我尝试了 phpexcel 的 github 页面中 this 示例的细微变化。 但我注意到,当我重命名生成的 excel sheet 时,我在 sheet 中没有很好地获取图表,如图所示。不确定我哪里错了。

这是我的代码:

<?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', null, 1),
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$H', null, 1)
);
//  Set the X-Axis Labels
$xAxisTickValues = array(
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$F:$F', null, 3),
);
//  Set the Data values for each data series we want to plot
$dataSeriesValues = array(
    new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$G:$G', null, 3),
    new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$H:$H', 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;
?>

类似

的条目
'Worksheet!$G'

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

'Performance!$G'

编辑

如果一个范围内的工作表名称包含任何"special"个字符,例如空格,那么MS Excel规则是工作表名称必须用单引号引起来,所以:

'Worksheet 1!$G

意味着你要么需要用双引号括起你的字符串(转义 $ 标志以防止 PHP 试图将 $G</code> 解释为变量) </p> <pre><code>"'Worksheet 1'!$G$1"

或转义单引号

'\'Worksheet 1\'!$G'

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