sulavvr
sulavvr

Reputation: 276

PHPExcel Pie Chart

I've been stuck on this problem for a while now. I still can't seem to be able to figure out the way. I couldn't get the PHPExcel's piechart example, 33chartcreate-pie.php. I searched everywhere, but I don't seem to be able to find a good answer, so I decided to ask it myself. From the example given, I tried this.. I'm using Codeigniter and so I'm loading the library $this->load->library('excel').

$dataseriesLabel1 = array(
        new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1),
);

$xAxisTickValues1 = array(
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', NULL, 4),
);

$dataSeriesValues1 = array(
    new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', NULL, 4),
);

$series1 = new PHPExcel_Chart_DataSeries(
    PHPExcel_Chart_DataSeries::TYPE_PIECHART,
    PHPExcel_Chart_DataSeries::GROUPING_STANDARD,
    range(0, count($dataSeriesValues1)-1),
    $dataseriesLabels1,
    $xAxisTickValues1,
    $dataSeriesValues1
);

$layout1 = new PHPExcel_Chart_Layout();
$layout1->setShowVal(TRUE);
$layout1->setShowPercent(TRUE);

$plotarea1 = new PHPExcel_Chart_PlotArea($layout1, array($series1));
$legend1 = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
$title1 = new PHPExcel_Chart_Title('Test Pie Chart');

$chart1 = new PHPExcel_Chart(
    'chart1',
     $title1,
     $legend1,
     $plotarea1,
     true,
     0,
     NULL,
     NULL
);

$chart1->setTopLeftPosition('A7');
$chart1->setBottomRightPosition('H20');

$this->excel->getActiveSheet()->addChart($chart1);

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content_Disposition: attachment;filename="Report"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('php://output');

At first, I wasn't getting any output, then I found out that I was using Excel5. So, I changed the type to Excel2007, but I still don't get anything. In fact, when I export and change the extension from .xlsx to .xls, I get the following error: <b>Fatal error</b>: Call to a member function cellExists() on a non-object in <b>path\application\third_party\PHPExcel\Calculation.php</b> on line <b>3327</b><br /> I would really appreciate the help. Thank you for looking at this. I hope everything's clear.

Upvotes: 1

Views: 6213

Answers (1)

This work for me

Instead of: $this->excel
use:           $objPHPExcel (but first $objPHPExcel = new PHPExcel();)

Instead of: header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
use:           header('Content-Type: application/vnd.ms-excel');

Instead of: header('Content_Disposition: attachment;filename="Report"'); use:            header('Content-Disposition: attachment;filename="Report.xls"');

Upvotes: 2

Related Questions