工作表名称中的PHPexcel图表空间不起作用

时间:2013-12-02 12:14:00

标签: charts phpexcel

当我在工作表名称中给出一个空格时,图表开始产生错误。

它有效

$heading       = 'AB'; 

问题:无法解决原因?

 $heading       = 'A B'; //i want to give a space in sheet name how?

完整代码:

$workbook = new PHPExcel();

$heading        = 'AB';
$workbook->getActiveSheet()->setTitle($heading);        
// Set document properties

$sheet = $workbook->getActiveSheet();
$sheet->fromArray(array(
  array(  '', 2010, 2011),
  array('Q1',   12,   15),
  array('Q2',   56,   73),
  array('Q3',   52,   61),
  array('Q4',   30,   32),
));

$labels = array(
  new PHPExcel_Chart_DataSeriesValues('String', $heading.'!$B$1', null, 1),
  new PHPExcel_Chart_DataSeriesValues('String', $heading.'!$C$1', null, 1),
);
$categories = array(
  new PHPExcel_Chart_DataSeriesValues('String', $heading.'!$A$2:$A$5', null, 4),
  new PHPExcel_Chart_DataSeriesValues('String', $heading.'!$A$2:$A$5', null, 4),
);
$values = array(
  new PHPExcel_Chart_DataSeriesValues('Number', $heading.'!$B$2:$B$5', null, 4),
  new PHPExcel_Chart_DataSeriesValues('Number', $heading.'!$C$2:$C$5', null, 4),
);
$series = new PHPExcel_Chart_DataSeries(
  PHPExcel_Chart_DataSeries::TYPE_BARCHART,       // plotType
  PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED,  // plotGrouping
  array(0, 1),                                    // plotOrder
  $labels,                                        // 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);
$chart = new PHPExcel_Chart(
  'chart1',                                       // name
  null,                                           // title
  $legend,                                        // legend
  $plotarea,                                      // plotArea
  true,                                           // plotVisibleOnly
  0,                                              // displayBlanksAs
  null,                                           // xAxisLabel
  null                                            // yAxisLabel
);

$chart->setTopLeftPosition('A7');
$chart->setBottomRightPosition('H20');
$sheet->addChart($chart);
$writer = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
$writer->setIncludeCharts(TRUE);

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

1 个答案:

答案 0 :(得分:3)

如果您的工作表名称包含空格等字符,那么在单元格范围内引用它时,您需要将其包装在单引号中:例如。

new PHPExcel_Chart_DataSeriesValues('String', "'".$heading."'".'!$B$1', null, 1),

因此它将显示为:

'A B'!$B$1

否则它可能会非常令人困惑,因为你可能意味着工作表{{1}中$B$1的当前工作表中的列A的交叉(是的,空格字符实际上可以是MS Excel中的运算符!) }