refactor: 导出xlsx封装

This commit is contained in:
2025-02-20 16:08:37 +08:00
parent 8cd8f594f3
commit 0aef5fafbb
6 changed files with 57 additions and 180 deletions

View File

@@ -25,4 +25,50 @@ if (!function_exists('array_to_tree')) {
} }
return $ret; return $ret;
} }
}
if (!function_exists('xlsx_writer')) {
/**
* 构建Excel数据
* @param array $data 数据 例:[['name'=>'张三','age'=>18, 'sex'=>'男']]
* @param array $schema 表头信息 例:['name' => '姓名', 'age' => '年龄', 'sex' => '性别']
* @param string $filename 下载文件名称 默认为YmdHis时间
* @return \PhpOffice\PhpSpreadsheet\Writer\IWriter
*/
function xlsx_writer(array|\think\model\Collection $data, array $schema, string $filename = ''): \PhpOffice\PhpSpreadsheet\Writer\IWriter
{
// 获取Spreadsheet对象
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// 写入表头
$title = array_values($schema);
$title_col = 'A';
foreach ($title as $value) {
// 单元格内容写入
$sheet->setCellValue($title_col . '1', $value);
$title_col++;
}
// 写入数据
$row = 2;
$keys = array_keys($schema);
foreach ($data as $item) {
$data_col = 'A';
foreach ($keys as $key) {
$sheet->setCellValue($data_col . $row, $item[$key] ?? '');
$data_col++;
}
$row++;
}
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');
}
} }

View File

@@ -5,8 +5,6 @@ namespace app\admin\controller\v1;
use app\admin\model\v1\ArticleModel; use app\admin\model\v1\ArticleModel;
use app\admin\validate\v1\ArticleValidate; use app\admin\validate\v1\ArticleValidate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
/** /**
* 文章管理控制器 * 文章管理控制器
@@ -221,40 +219,8 @@ class Article
// 获取导出数据 // 获取导出数据
$data = $this->getExportArticleData(); $data = $this->getExportArticleData();
// 获取Spreadsheet对象 // 导出
$spreadsheet = new Spreadsheet(); xlsx_writer($data, $schema)->save('php://output');
$sheet = $spreadsheet->getActiveSheet();
// 写入表头
$title = array_values($schema);
$title_col = 'A';
foreach ($title as $value) {
// 单元格内容写入
$sheet->setCellValue($title_col . '1', $value);
$title_col++;
}
// 写入数据
$row = 2;
$keys = array_keys($schema);
foreach ($data as $item) {
$data_col = 'A';
foreach ($keys as $key) {
$sheet->setCellValue($data_col . $row, $item[$key]);
$data_col++;
}
$row++;
}
flush();
ob_flush();
$filename = date('YmdHms');
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');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
} }
// 获取文章导出数据 // 获取文章导出数据

View File

@@ -4,8 +4,6 @@ declare (strict_types = 1);
namespace app\admin\controller\v1; namespace app\admin\controller\v1;
use app\admin\model\v1\ArticleLeaveMessageModel; use app\admin\model\v1\ArticleLeaveMessageModel;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
/** /**
* 文章留言(评论)管理控制器 * 文章留言(评论)管理控制器
@@ -106,40 +104,8 @@ class ArticleLeaveMessage
// 获取导出数据 // 获取导出数据
$data = $this->getExportMessageData(); $data = $this->getExportMessageData();
// 获取Spreadsheet对象 // 导出
$spreadsheet = new Spreadsheet(); xlsx_writer($data, $schema)->save('php://output');
$sheet = $spreadsheet->getActiveSheet();
// 写入表头
$title = array_values($schema);
$title_col = 'A';
foreach ($title as $value) {
// 单元格内容写入
$sheet->setCellValue($title_col . '1', $value);
$title_col++;
}
// 写入数据
$row = 2;
$keys = array_keys($schema);
foreach ($data as $item) {
$data_col = 'A';
foreach ($keys as $key) {
$sheet->setCellValue($data_col . $row, $item[$key]);
$data_col++;
}
$row++;
}
flush();
ob_flush();
$filename = date('YmdHms');
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');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
} }
private function getExportMessageData() private function getExportMessageData()
{ {
@@ -149,7 +115,7 @@ class ArticleLeaveMessage
'CASE WHEN is_audited = 1 THEN "已审核" ELSE "未审核" END' => 'is_audited' 'CASE WHEN is_audited = 1 THEN "已审核" ELSE "未审核" END' => 'is_audited'
]) ])
->withJoin(['article' => function($query) use($param) { ->withJoin(['article' => function($query) use($param) {
$query->where('article.language_id', '=', $param['lang_id']); $query->where('article.language_id', '=', request()->lang_id);
if (!empty($param['title'])) { if (!empty($param['title'])) {
$query->where('article.title', 'like', '%' . $param['title'] . '%'); $query->where('article.title', 'like', '%' . $param['title'] . '%');
} }

View File

@@ -9,8 +9,6 @@ use app\admin\model\v1\ProductRelatedModel;
use app\admin\model\v1\ProductSkuAttrModel; use app\admin\model\v1\ProductSkuAttrModel;
use app\admin\model\v1\ProductSkuModel; use app\admin\model\v1\ProductSkuModel;
use app\admin\validate\v1\ProductValidate; use app\admin\validate\v1\ProductValidate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
/** /**
* 产品管理控制器 * 产品管理控制器
@@ -336,40 +334,8 @@ class Product
// 获取导出数据 // 获取导出数据
$data = $this->getExportProductData(); $data = $this->getExportProductData();
// 获取Spreadsheet对象 // 导出
$spreadsheet = new Spreadsheet(); xlsx_writer($data, $schema)->save('php://output');
$sheet = $spreadsheet->getActiveSheet();
// 写入表头
$title = array_values($schema);
$title_col = 'A';
foreach ($title as $value) {
// 单元格内容写入
$sheet->setCellValue($title_col . '1', $value);
$title_col++;
}
// 写入数据
$row = 2;
$keys = array_keys($schema);
foreach ($data as $item) {
$data_col = 'A';
foreach ($keys as $key) {
$sheet->setCellValue($data_col . $row, $item[$key]??'');
$data_col++;
}
$row++;
}
flush();
ob_flush();
$filename = date('YmdHms');
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');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
} }
// 获取产品导出数据 // 获取产品导出数据
private function getExportProductData() private function getExportProductData()

View File

@@ -9,7 +9,6 @@ use app\admin\model\v1\ProductPurchaseLinkModel;
use app\admin\model\v1\ProductPurchaseLinkPlatformModel; use app\admin\model\v1\ProductPurchaseLinkPlatformModel;
use app\admin\validate\v1\ProductPurchaseLinkValidate; use app\admin\validate\v1\ProductPurchaseLinkValidate;
use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\facade\Db; use think\facade\Db;
class ProductPurchaseLink class ProductPurchaseLink
@@ -241,40 +240,8 @@ class ProductPurchaseLink
// 获取导出数据 // 获取导出数据
$data = $this->getExportLinkData(); $data = $this->getExportLinkData();
// 获取Spreadsheet对象 /// 导出
$spreadsheet = new Spreadsheet(); xlsx_writer($data, $schema)->save('php://output');
$sheet = $spreadsheet->getActiveSheet();
// 写入表头
$title = array_values($schema);
$title_col = 'A';
foreach ($title as $value) {
// 单元格内容写入
$sheet->setCellValue($title_col . '1', $value);
$title_col++;
}
// 写入数据
$row = 2;
$keys = array_keys($schema);
foreach ($data as $item) {
$data_col = 'A';
foreach ($keys as $key) {
$sheet->setCellValue($data_col . $row, $item[$key] ?? '');
$data_col++;
}
$row++;
}
flush();
ob_flush();
$filename = date('YmdHms');
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');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
} }
private function getExportLinkData() private function getExportLinkData()
{ {

View File

@@ -6,8 +6,6 @@ namespace app\admin\controller\v1;
use app\admin\model\v1\VideoModel; use app\admin\model\v1\VideoModel;
use app\admin\validate\v1\VideoValidate; use app\admin\validate\v1\VideoValidate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\facade\Config; use think\facade\Config;
/** /**
@@ -195,40 +193,8 @@ class Video
// 获取导出数据 // 获取导出数据
$data = $this->getExportVideoData(); $data = $this->getExportVideoData();
// 获取Spreadsheet对象 // 导出
$spreadsheet = new Spreadsheet(); xlsx_writer($data, $schema)->save('php://output');
$sheet = $spreadsheet->getActiveSheet();
// 写入表头
$title = array_values($schema);
$title_col = 'A';
foreach ($title as $value) {
// 单元格内容写入
$sheet->setCellValue($title_col . '1', $value);
$title_col++;
}
// 写入数据
$row = 2;
$keys = array_keys($schema);
foreach ($data as $item) {
$data_col = 'A';
foreach ($keys as $key) {
$sheet->setCellValue($data_col . $row, $item[$key] ?? '');
$data_col++;
}
$row++;
}
flush();
ob_flush();
$filename = date('YmdHms');
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');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
} }
private function getExportVideoData() private function getExportVideoData()
{ {