PHPExcel – 从数组中填充数据

到目前为止,我已经通过PHPExcel成功地将Mysql中的数据导出到Excel中,制作了我的表格,并进行了格式化处理,而且一切都很顺利。

然而,迄今为止,从所述表格制作图表的尝试都失败了。 不仅如此,在Googlesearch结束之后的几天,我还没有find一个简单的示例/教程来介绍如何从MySQL中填充graphics值。

这一切归结为:

Taken from https://github.com/affinitybridge/phpexcel/blob/master/Tests/33chartcreate-pie.php /** PHPExcel */ include 'PHPExcel.php'; $objPHPExcel = new PHPExcel(); $objWorksheet = $objPHPExcel->getActiveSheet(); $objWorksheet->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), ) ); 

我该如何从a)Mysql或b)excel中填充$ objWorksheet-> fromArray。

如果是SQL,我需要为我的数据库中的所有表运行这个查询。

$ sql =“SELECT Functional_AreaPassed__StatusBlocked__StatusFailed__Status FROM $ tbl_name WHERE 1;

上下文:我有几个function区域,每个function区域在通过/失败/阻塞中有不同的值。 每个function区需要1个图。 平均而言,我有每个表14个function区。

然后我将为每个function区域构build一个饼图(或条形图,我不挑剔)。

选项2)Excel

由于我已经说过在Excel中的数据,我需要填充来自以下的数据:

在这里输入图像说明

同样,每个ROW(行5(FA = EPG))的1个图将是1个图,行6(FA = VOD)将是另一个图等等)。

这将不得不重复每个工作表…

我到目前为止的尝试是在下面,这将产生每个工作表(这是很好)一个graphics,但是是空的(不好)。

 foreach (glob("*.xls") as $f) { $inputFileName = $f; echo "Found $inputFileName <br/>"; } // Read your Excel workbook $inputFileType = PHPExcel_IOFactory::identify($inputFileName); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($inputFileName); $i = 0; foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) { $arrayData[$worksheet->getTitle()] = $worksheet->toArray(); echo "Title: ".$worksheet->getTitle()."<br/>"; $wsTitle = $worksheet->getTitle(); $objWorksheet = $objPHPExcel->setActiveSheetIndexByName($wsTitle); $highestRow = $worksheet->getHighestRow(); // eg 10 $endofData = $highestRow -1; $highestColumn = $worksheet->getHighestColumn(); // eg 'F' $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); $colNumberStart = PHPExcel_Cell::columnIndexFromString("D"); $BeforeTOT = $colNumberStart - 2; $TOT = $colNumberStart - 1; echo "Highest Row is ==> $highestRow <br/>End of Data - header is $endofData <br/> Highest Col is ==> $highestColumn <br/> Start col is ==> $colNumberStart and Highest Col end is ==> $highestColumnIndex <br/>"; // Set the Labels for each data series we want to plot // Datatype // Cell reference for data // Format Code // Number of datapoints in series // Data values // Data Marker ${'dataseriesLabels' . $i} = array( new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$4', null, 1), // 2010 new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$G$4', null, 1), // 2011 new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$H$4', null, 1), // 2012 new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$I$4', null, 1), // 2012 ); displayArray(${'dataseriesLabels' . $i}); // Set the X-Axis Labels // Datatype // Cell reference for data // Format Code // Number of datapoints in series // Data values // Data Marker ${'xAxisTickValues' . $i} = array( new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$4:$H$4', null, 19), // Q1 to Q4 ); displayArray(${'xAxisTickValues' . $i}); // 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' . $i} = array( new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$G$5:$I$5', null, 4), ); displayArray(${'dataSeriesValues' . $i}); // Build the dataseries ${'series' . $i} = new PHPExcel_Chart_DataSeries( PHPExcel_Chart_DataSeries::TYPE_PIECHART, // plotType PHPExcel_Chart_DataSeries::GROUPING_PERCENT_STACKED, // plotGrouping range(0, count(${'dataseriesLabels' . $i})-1), // plotOrder ${'dataseriesLabels' . $i}, // plotLabel ${'xAxisTickValues' . $i}, // plotCategory ${'dataSeriesValues' . $i} // plotValues ); displayArray(${'series' . $i}); // Set the series in the plot area ${'plotarea' . $i} = new PHPExcel_Chart_PlotArea(null, array(${'series' . $i})); // Set the chart legend ${'legend' . $i} = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_TOPRIGHT, null, false); ${'title' . $i} = new PHPExcel_Chart_Title('Test %age-Stacked Area Chart'); ${'yAxisLabel' . $i} = new PHPExcel_Chart_Title('Value ($k)'); // Create the chart ${'chart' . $i} = new PHPExcel_Chart( 'chart1', // name ${'title' . $i}, // title ${'legend' . $i}, // legend ${'plotarea' . $i}, // plotArea true, // plotVisibleOnly 0, // displayBlanksAs null, // xAxisLabel ${'yAxisLabel' . $i} // yAxisLabel ); // Set the position where the chart should appear in the worksheet ${'chart' . $i}->setTopLeftPosition('A7'); ${'chart' . $i}->setBottomRightPosition('H20'); // Add the chart to the worksheet $objWorksheet->addChart(${'chart' . $i}); // Set the Labels for each data series we want to plot // Datatype // Cell reference for data // Format Code // Number of datapoints in series // Data values // Data Marker // Save Excel 2007 file echo date('H:i:s') , " Write to Excel2007 format" , EOL; $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->setIncludeCharts(TRUE); $objWriter->save(str_replace('.php', '.xlsx', __FILE__)); echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; // Echo memory peak usage echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL; // Echo done echo date('H:i:s') , " Done writing file" , EOL; echo 'File has been created in ' , getcwd() , EOL; $i++; } 

费拉斯,我有更多的调查,并得出一些答案。

一个。 如果数据在excel上,则不需要从Mysql b查询它。 这是一个可以工作的片段,100%的时间从以前的工作表中填充堆积的条形图:

  // Set the Labels for each data series we want to plot // Datatype // Cell reference for data // Format Code // Number of datapoints in series // Data values // Data Marker $dataseriesLabels1 = array( new PHPExcel_Chart_DataSeriesValues('String', 'report!$E$4', NULL, 4), // Jan to Dec new PHPExcel_Chart_DataSeriesValues('String', 'report!$F$4', NULL, 4), // Jan to Dec new PHPExcel_Chart_DataSeriesValues('String', 'report!$G$4', NULL, 4) // Jan to Dec ); // 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', 'report!$C$4:$C$20', NULL, 4) // Jan to Dec ); // 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 $dataSeriesValues1 = array( new PHPExcel_Chart_DataSeriesValues('Number', 'report!$E$5:$E$20', NULL, 16), new PHPExcel_Chart_DataSeriesValues('Number', 'report!$F$5:$F$20', NULL, 16), new PHPExcel_Chart_DataSeriesValues('Number', 'report!$G$5:$G$20', NULL, 16) ); // Build the dataseries $series1 = new PHPExcel_Chart_DataSeries( PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType PHPExcel_Chart_DataSeries::GROUPING_STACKED, // plotGrouping range(0, count($dataSeriesValues1)-1), // plotOrder $dataseriesLabels1, // plotLabel $xAxisTickValues, // plotCategory $dataSeriesValues1 // plotValues ); // Set additional dataseries parameters // Make it a vertical column rather than a horizontal bar graph $series1->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL); // Set the series in the plot area $plotarea = new PHPExcel_Chart_PlotArea(NULL, array($series1)); // Set the chart legend $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false); $title = new PHPExcel_Chart_Title('Average Weather Chart for Crete'); // Create the chart $chart = new PHPExcel_Chart( 'chart1', // name $title, // title $legend, // legend $plotarea, // plotArea true, // plotVisibleOnly 0, // displayBlanksAs NULL, // xAxisLabel NULL // yAxisLabel ); // Set the position where the chart should appear in the worksheet $chart->setTopLeftPosition('F2'); $chart->setBottomRightPosition('O16'); // Add the chart to the worksheet $objWorksheet->addChart($chart); // Instantiate a Writer to create an OfficeOpenXML Excel .xlsx file $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); $objWriter->setIncludeCharts(true); 

d。 所以,要自动创build图表,过程将是:d1。 打开w / e工作表,您需要从d2获取数据。 读取或硬编码标签的值,x和y轴d3。 创build图表元素d4。 张贴所述元素w / e是期望的。

希望这有助于其他一些糟糕的编码器在那里头疼PHPExcel +graphics

================================================== ========================

更新25/9

我试图dynamic地构build数组,但有点失败。

  Fellas, question. 

我现在试图dynamic填充这个,换句话说,构build:

  $dataseriesLabels1 = "array("; foreach ($Labels as $k => $s) { echo "Parsing $s <br/>"; $dataseriesLabels1 .= "new PHPExcel_Chart_DataSeriesValues('String', ".$s.", NULL, 4),"; // echo "We have $dataseriesLabels1 in this loop <br/>"; } $dataseriesLabels1 = rtrim($dataseriesLabels1, "," ); $dataseriesLabels1 .= ");"; echo "BUilt : $dataseriesLabels1 <br/>"; 

这一切都很好,因为它为我创造了:

  array(new PHPExcel_Chart_DataSeriesValues('String', 'Sprint!$D$4:$D$16', NULL, 4),new PHPExcel_Chart_DataSeriesValues('String', 'Sprint!$E$4:$E$16', NULL, 4),new PHPExcel_Chart_DataSeriesValues('String', 'Sprint!$F$4:$F$16', NULL, 4),new PHPExcel_Chart_DataSeriesValues('String', 'Sprint!$G$4:$G$16', NULL, 4),new PHPExcel_Chart_DataSeriesValues('String', 'Sprint!$H$4:$H$16', NULL, 4)); 

不幸的是,这被解释为一个string,而不是一个数组,因此图表生成失败:/

想法?