我在使用 spout 库导出 excel 时遇到问题。我不知道我的代码中的问题在哪里。我不太了解 spout 库。我已经尝试了很多次,但同样的错误一次又一次地发生。请指导我问题在哪里。
代码:
<?php
include('php_script/db.php');
use Box\Spout\Common\Type;
use Box\Spout\Writer\Style\Border;
use Box\Spout\Writer\Style\BorderBuilder;
use Box\Spout\Writer\Style\Color;
use Box\Spout\Writer\Style\StyleBuilder;
use Box\Spout\Writer\WriterFactory;
include('php_script/spout/src/Spout/Autoloader/autoload.php');
$sql = mysqli_query($con,"select * from person ");
$border = (new BorderBuilder())
->setBorderBottom(Color::GREEN, Border::WIDTH_THIN, Border::STYLE_DASHED)
//->setFontColor(Color::BLUE)
//->setBackgroundColor(Color::YELLOW)
->build();
$style = (new StyleBuilder())
->setBorder($border)
->build();
$filePath = "person".date("Y-m-d-H-i-s").'.xlsx';
$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($filePath);
$array = ['TYPE'];
$writer->addRowWithStyle($array, $style);
while( $rows = mysqli_fetch_assoc($sql)) {
$Type_subsidiary = $rows['Type_subsidiary'];
$data = [$Type_subsidiary];
$writer->addRow($data);
}
$writer->close();
if (file_exists($filePath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filePath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
exit;
}
?>
错误:
Excel 无法打开文件“person2018-09-19-07-20-30.xlsx”,因为文件格式或文件扩展名无效。验证文件未损坏且文件扩展名与文件格式匹配