【问题标题】:PHPexcel chart space in sheet name is not working工作表名称中的 PHPexcel 图表空间不起作用
【发布时间】:2013-12-02 12:13:59
【问题描述】:

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

有效

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

【问题讨论】:

    标签: charts phpexcel


    【解决方案1】:

    如果您的工作表名称包含空格等字符,则在单元格区域或类似区域中引用它时需要将其用单引号括起来:例如

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

    所以它会读作:

    'A B'!$B$1
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多