使用 phpSpreadSheet 读写 excel 文件后,图形消失

After reading and writing an excel file using phpSpreadSheet, the graph disappears

我正在尝试使用来自 mysql 服务器并使用 phpSpreadSheet 的数据填充 excel 文件。然后,我需要创建一个图表,以便用户可以看到已经制作好的图表。 我可以毫无问题地填充 excel 文件,但图表没有显示。 所以我决定通过在一个空文件中添加 headers 和图形来简化问题,读取这个文件,将数据写入这个文件,并为用户导出(使用下载按钮)。但即使在这种情况下,打开后也没有图表。 然后我使用了一个非常简单的代码,但它仍然没有出现。所以我认为我无法解决的问题在所有情况下都是一样的。我做错了什么所以图表消失了? 这是我在创建文件并尝试创建图表时首次使用的代码:

$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"], 0, -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"], 0, -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', null, 1),
];
$xAxisTickValues = [
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$E:$E', null, 5),
];

$dataSeriesValues = [
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$F:$F', null, 5),
];
// 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, null, 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!