如何解决使用phpSpreadSheet读写Excel文件后,图形消失
我正在尝试使用来自mysql服务器并使用phpSpreadSheet的数据填充Excel文件。然后,我需要创建一个图形,以便用户可以看到已经制作的图形。 我可以填充excel文件没有问题,但是该图没有显示。 因此,我决定通过在一个空文件中添加标题和图形来简化问题,读取该文件,将数据写入该文件,然后将其导出给用户(使用下载按钮)。但是即使在这种情况下,打开图形后该图形也不存在。 然后,我使用了一个非常简单的代码,但它仍然没有显示。因此,我认为我无法解决的问题在所有情况下都是相同的。我做错了什么,所以图表消失了? 这是我在创建文件并尝试创建图形时首先使用的代码:
$pdo = new PDO("mysql:host=localhost;dbname=XXXXXXX","root","");
$nameSens = $_POST['nameSens'];
$query = "SELECT hours,values from $nameSens";
$stmt = $pdo->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll();
$spreadsheet = $file->getActiveSheet();
$spreadsheet->setTitle($nameSens);
$file->getActiveSheet()->getColumnDimension('A:F')->setAutoSize(true);
$file->getActiveSheet()->getStyle('A1:B1')->getFont()->getColor()->setARGB('FFFFFFFF');
$file->getActiveSheet()->getStyle('A1:B1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('FF3333CC');
$file->getActiveSheet()->getStyle('F1')->getFont()->getColor()->setARGB('FFFFFFFF');
$file->getActiveSheet()->getStyle('F1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('FF3333CC');
$spreadsheet->getStyle('A1:F1')->getAlignment()->setHorizontal('center');
$spreadsheet->getStyle('A:F')->getAlignment()->setHorizontal('center');
$spreadsheet->setCellValue('A1','day');
$spreadsheet->setCellValue('B1','hour');
$spreadsheet->setCellValue('F1','Values');
$count = 2;
foreach($result as $row) {
$day = substr($row["hours"],-9);
$hour = substr($row["hours"],11,);
$spreadsheet->setCellValue('A' . $count,$day);
$spreadsheet->getStyle('A' . $count)->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_YYYYMMDD);
$spreadsheet->setCellValue('B' . $count,$hour);
$spreadsheet->getStyle('B' . $count)->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_TIME6);
$dayA = $file->getActiveSheet()->getCell('A' . $count)->getValue();
$spreadsheet->setCellValue('C' . $count,$dayA);
$spreadsheet->getStyle('C' . $count)->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
$hourB = $file->getActiveSheet()->getCell('B' . $count)->getValue();
$spreadsheet->setCellValue('D' . $count,$hourB);
$spreadsheet->getStyle('D' . $count)->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
$dateExcel = $file->getActiveSheet()->getCell('C' . $count)->getValue() + $file->getActiveSheet()->getCell('D' . $count)->getValue();
$dateExcel = \DateTime::createFromFormat('Y-m-d H:i',substr($row["hours"],-3))->getTimestamp();
$dateExcel = ((($dateExcel/60)/60)/24)+25569.08;
$spreadsheet->setCellValue('E' . $count,$dateExcel);
$spreadsheet->getStyle('E' . $count)->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
$spreadsheet->setCellValue('F' . $count,$row["values"]);
$count++;
}
$dataSeriesLabels = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING,'Worksheet!$F$1',null,1),];
$xAxisTickValues = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER,'Worksheet!$E$2:$E$6',5),];
$dataSeriesValues = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER,'Worksheet!$F$2:$F$6',];
// Build the dataseries
$series = new DataSeries(
DataSeries::TYPE_LINECHART,// plotType
DataSeries::GROUPING_STACKED,// plotGrouping
range(0,count($dataSeriesValues) - 1),// plotOrder
$dataSeriesLabels,// plotLabel
$xAxisTickValues,// plotCategory
$dataSeriesValues // plotValues
);
$series->setPlotDirection(DataSeries::DIRECTION_COL);
// Set the series in the plot area
$layout1 = new Layout();
$layout1->setShowVal(true);
$plotArea = new PlotArea($layout1,[$series]);
// Set the chart legend
$legend = new Legend(Legend::POSITION_TOPRIGHT,false);
$title = new Title('Data F01');
$yAxisLabel = new Title('Values (mm)');
// Create the chart
$chart = new Chart(
'chart1',// name
$title,// title
$legend,// legend
$plotArea,// plotArea
true,// plotVisibleOnly
DataSeries::EMPTY_AS_GAP,// displayBlanksAs
null,// xAxisLabel
$yAxisLabel,// yAxisLabel
);
// Set the position where the chart should appear in the worksheet
$chart->setTopLeftPosition('H1');
$chart->setBottomRightPosition('O15');
// Add the chart to the worksheet
$spreadsheet->addChart($chart);*/
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($file,'Xls');
$writer = new Xls($file);
$writer->setIncludeCharts(true);
$file_name = 'donnees-'.$nameSens.'.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$file_name.'"');
header('Cache-Control: max-age=0');
ob_end_clean();
$writer->save('php://output');
exit;
这就是我得到的(请注意,如果我使用E和F列手动制作图形,则可以正常工作):
那是我与已经包含图形的excel文件一起使用的简单代码
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('../data/data.xls');
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->getCell('C3')->setValue('data1');
$worksheet->getCell('D3')->setValue('data2');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet,'Xls');
$writer->save('write.xls');
下面是我所拥有的。顶部是输入文件(data.xls),底部是输出(write.xls)
感谢您的帮助
洛朗
解决方法
感谢Kramar,我解决了我的问题: PHPSpreadsheet generates invalid file with charts
我只是将displayBlanksAs更改为'gap'而不是0!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。