PHP_XLSXWriter 是一个轻量级的 PHP Excel 库,可以用于快速创建和导出 XLSX 文件。它支持设置单元格样式和公式,以及大量的数据格式。
github地址https://github.com/mk-j/PHP_XLSXWriter
<?php
# 引入xlsxwriter类文件
include("xlsxwriter.class.php");
$filename = "export.xlsx";
# 设置heard头
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$writer = new XLSXWriter();
$writer->setAuthor('Some Author'); // 设置作者信息
$title = [
'时间',
'下载总次数',
'浏览次数',
'下载率',
];
$writer->writeSheetRow('Sheet1', $title); // 设置表头
$data = [
['2023-02-01',30,30,'100%'],
['2023-02-02',30,30,'100%'],
];
foreach($data as $row)
{
$writer->writeSheetRow('Sheet1', $row); // 循环写入每行数据
}
$writer->writeToStdOut(); // 导出文件
<?php
include("xlsxwriter.class.php");
$writer = new XLSXWriter();
$writer->setAuthor('Some Author'); // 设置作者信息
$title = [
'时间',
'下载总次数',
'浏览次数',
'下载率',
];
$writer->writeSheetRow('Sheet1', $title); // 设置表头
$data = [
['2023-02-01',30,30,'100%'],
['2023-02-02',30,30,'100%'],
];
foreach($data as $row)
{
$writer->writeSheetRow('Sheet1', $row); // 循环写入每行数据
}
$filename = "./files/export.xlsx";
$writer->writeToFile($filename); // 导出文件
希望对大家有所帮助