【问题标题】:CodeIgniter + PHPExcel: Error when try to create a chartCodeIgniter + PHPExcel:尝试创建图表时出错
【发布时间】:2013-06-07 03:27:28
【问题描述】:

我必须开发一个程序,该程序包括创建包含不同单元格的新 Excel 工作表。下一步是生成一个垂直条形图来显示从数据单元格中检索到的各种数据。

我正在使用 PHPExcel 提供的“33chartcreate-column”示例来了解该库如何与 codeigniter 框架一起使用。但是当我尝试通过调用 setIncludeCharts 方法来创建图表时,我得到了错误: “致命错误:在 *\application\third_party\PHPExcel\Calculation.php 第 3327 行中的非对象上调用成员函数 cellExists()”

$this->load->library('excel');
//activate worksheet number 1
$this->excel->setActiveSheetIndex(0);
//name the worksheet
$this->excel->getActiveSheet()->setTitle('Nombre de la hoja');

$this->excel->getActiveSheet()->fromArray(
array(
    array('',   2010,   2011,   2012),
    array('Q1',   12,   15,     21),
    array('Q2',   56,   73,     86),
    array('Q3',   52,   61,     69),
    array('Q4',   30,   32,     0),
    )
);

$dataseriesLabels = array(
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1), //    2010
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1), //    2011
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1), //    2012
);

/*Set the X-Axis Labels
Datatype
Cell reference for data
Format Code
Number of datapoints in series
Data values
Data Marker*/
$xAxisTickValues = array(
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', NULL, 4), //    Q1 to Q4
);

/*Set the Data values for each data series we want to plot
Datatype
Cell reference for data
Format Code
Number of datapoints in series
Data values
Data Marker*/
$dataSeriesValues = array(
    new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', NULL, 4),
    new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', NULL, 4),
    new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$5', NULL, 4),
);

//Build the dataseries
$series = new PHPExcel_Chart_DataSeries(
    PHPExcel_Chart_DataSeries::TYPE_BARCHART,        // plotType
    PHPExcel_Chart_DataSeries::GROUPING_STANDARD,    // plotGrouping
    range(0, count($dataSeriesValues)-1),            // plotOrder
    $dataseriesLabels,                               // plotLabel
    $xAxisTickValues,                                // plotCategory
    $dataSeriesValues                                // plotValues
);

/*Set additional dataseries parameters
Make it a vertical column rather than a horizontal bar 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('Test Column Chart');
$yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');

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

$this->excel->getActiveSheet()->addChart($chart);
// Save Excel 2007 file
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
<b>$objWriter->setIncludeCharts(TRUE);</b> //line 1327
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

【问题讨论】:

  • 查看用于定义图表的调用比查看用于保存文件的代码更重要... setIncludeCharts(TRUE) 只是告诉 PHPExcel 包含您的图表'已经定义了。
  • @MarkBaker 谢谢,我已经更新了代码,添加了定义图表的调用。

标签: php codeigniter charts phpexcel


【解决方案1】:

我发现了我的错误:

$this->excel->getActiveSheet()->setTitle('Nombre de la hoja');

在调用setActiveSheetIndex 方法后,我无法调用setTitle 方法。

【讨论】:

  • 如果要将图表添加到工作表中,似乎无法设置工作表标题。在 1.7.9 中生成了一个致命错误,在 1.8.2 中输出了电子表格,但图表中填充了无效值,因此代码中似乎存在某种从未解决的内部冲突。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
  • 2015-01-15
  • 2016-07-12
相关资源
最近更新 更多