refactor: 导出xlsx封装
This commit is contained in:
@@ -9,7 +9,6 @@ use app\admin\model\v1\ProductPurchaseLinkModel;
|
||||
use app\admin\model\v1\ProductPurchaseLinkPlatformModel;
|
||||
use app\admin\validate\v1\ProductPurchaseLinkValidate;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use think\facade\Db;
|
||||
|
||||
class ProductPurchaseLink
|
||||
@@ -241,40 +240,8 @@ class ProductPurchaseLink
|
||||
// 获取导出数据
|
||||
$data = $this->getExportLinkData();
|
||||
|
||||
// 获取Spreadsheet对象
|
||||
$spreadsheet = new 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();
|
||||
$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');
|
||||
/// 导出
|
||||
xlsx_writer($data, $schema)->save('php://output');
|
||||
}
|
||||
private function getExportLinkData()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user