refactor: 修改xlsx导出

This commit is contained in:
2025-03-10 14:59:47 +08:00
parent 43630eb20a
commit 2e7c0887ea
7 changed files with 20 additions and 13 deletions

View File

@@ -77,9 +77,10 @@ if (!function_exists('xlsx_writer')) {
* @param array $data 数据 例:[['name'=>'张三','age'=>18, 'sex'=>'男']]
* @param array $schema 表头信息 例:['name' => '姓名', 'age' => '年龄', 'sex' => '性别']
* @param string $filename 下载文件名称 默认为YmdHis时间
* @return \PhpOffice\PhpSpreadsheet\Writer\IWriter
* @param string $output 输出方式 php://output 或 文件路径
* @return \think\Response
*/
function xlsx_writer(array|\think\model\Collection $data, array $schema, string $filename = ''): \PhpOffice\PhpSpreadsheet\Writer\IWriter
function xlsx_writer(array|\think\model\Collection $data, array $schema, string $filename = '', string $output = 'php://output'): \think\Response
{
// 获取Spreadsheet对象
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
@@ -109,10 +110,16 @@ if (!function_exists('xlsx_writer')) {
flush();
ob_flush();
if ($filename == '') $filename = date('YmdHis');
header('Access-Control-Expose-Headers: Content-Disposition');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; Charset=UTF-8');
header('Content-Disposition: attachment;filename=' . $filename . '.xlsx');
header('Cache-Control: max-age=0');
return \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
// 文件写入缓冲区
\PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx')->save($output);
// 从缓冲区获取导出文件并通过Response返回解决thinkphp框架的Response无法获取设置的header问题
$file_data = ob_get_clean();
$response = \think\Response::create($file_data)->header([
'Access-Control-Expose-Headers' => 'Content-Disposition',
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; Charset=UTF-8',
'Content-Disposition' => 'attachment;filename=' . $filename . '.xlsx',
'Cache-Control' => 'max-age=0'
]);
return $response;
}
}

View File

@@ -220,7 +220,7 @@ class Article
$data = $this->getExportArticleData();
// 导出
xlsx_writer($data, $schema)->save('php://output');
return xlsx_writer($data, $schema);
}
// 获取文章导出数据

View File

@@ -105,7 +105,7 @@ class ArticleLeaveMessage
$data = $this->getExportMessageData();
// 导出
xlsx_writer($data, $schema)->save('php://output');
return xlsx_writer($data, $schema);
}
private function getExportMessageData()
{

View File

@@ -433,7 +433,7 @@ class Menu
$data = $this->getExportMenuData();
// 导出
xlsx_writer($data, $schema)->save('php://output');
return xlsx_writer($data, $schema, '', 'php://output', 'file');
}
private function getExportMenuData()
{

View File

@@ -335,7 +335,7 @@ class Product
$data = $this->getExportProductData();
// 导出
xlsx_writer($data, $schema)->save('php://output');
return xlsx_writer($data, $schema);
}
// 获取产品导出数据
private function getExportProductData()

View File

@@ -230,7 +230,7 @@ class ProductPurchaseLink
$data = $this->getExportLinkData();
/// 导出
xlsx_writer($data, $schema)->save('php://output');
return xlsx_writer($data, $schema);
}
private function getExportLinkData()
{

View File

@@ -196,7 +196,7 @@ class Video
$data = $this->getExportVideoData();
// 导出
xlsx_writer($data, $schema)->save('php://output');
return xlsx_writer($data, $schema);
}
private function getExportVideoData()
{