Esempio n. 1
0
 /**
  * Write chart to XML format
  *
  * @param  PHPPowerPoint             $presentation
  * @param  PHPPowerPoint_Shape_Chart $chart
  * @param  string                    $tempName
  * @return string                    String output
  * @throws Exception
  */
 public function writeSpreadsheet(PHPPowerPoint $presentation, $chart, $tempName)
 {
     // Need output?
     if (!$chart->getIncludeSpreadsheet()) {
         throw new Exception('No spreadsheet output is required for the given chart.');
     }
     // Verify PHPExcel
     if (!class_exists('PHPExcel')) {
         throw new Exception('PHPExcel has not been loaded. Include PHPExcel.php in your script, e.g. require_once \'PHPExcel.php\'.');
     }
     // Create new spreadsheet
     $workbook = new PHPExcel();
     // Set properties
     $title = $chart->getTitle()->getText();
     if (strlen($title) == 0) {
         $title = 'Chart';
     }
     $workbook->getProperties()->setCreator($presentation->getProperties()->getCreator())->setLastModifiedBy($presentation->getProperties()->getLastModifiedBy())->setTitle($title);
     // Add chart data
     $sheet = $workbook->setActiveSheetIndex(0);
     $sheet->setTitle('Sheet1');
     // Write series
     $seriesIndex = 0;
     foreach ($chart->getPlotArea()->getType()->getData() as $series) {
         // Title
         $sheet->setCellValueByColumnAndRow(1 + $seriesIndex, 1, $series->getTitle());
         // X-axis
         $axisXData = array_keys($series->getValues());
         for ($i = 0; $i < count($axisXData); $i++) {
             $sheet->setCellValueByColumnAndRow(0, $i + 2, $axisXData[$i]);
         }
         // Y-axis
         $axisYData = array_values($series->getValues());
         for ($i = 0; $i < count($axisYData); $i++) {
             $sheet->setCellValueByColumnAndRow(1 + $seriesIndex, $i + 2, $axisYData[$i]);
         }
         ++$seriesIndex;
     }
     // Save to string
     $writer = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
     $writer->save($tempName);
     // Load file in memory
     $returnValue = file_get_contents($tempName);
     @unlink($tempName);
     return $returnValue;
 }