CodeIgniter + PHPExcel:尝试创建图表时出错

时间:2013-06-07 03:27:29

标签: php codeigniter charts phpexcel

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

我正在使用PHPExcel给出的“33chartcreate-column”示例来理解libraty如何与codeigniter框架一起工作。但是当我尝试通过调用setIncludeCharts方法创建图表时,我得到错误: “致命错误:在第3327行 * \ application \ third_party \ PHPExcel \ Calculation.php中的非对象上调用成员函数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__));

1 个答案:

答案 0 :(得分:1)

我发现了我的错误:

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

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