|
|
@@ -5,8 +5,6 @@ namespace app\api\controller;
|
|
|
use app\common\controller\Api;
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
-use Symfony\Component\HttpFoundation\Response;
|
|
|
-use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
|
|
/**
|
|
|
* 包装计件单据维护接口
|
|
|
@@ -642,17 +640,13 @@ class PackagingCountDocument extends Api
|
|
|
// $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
|
|
|
}
|
|
|
|
|
|
- public function Ex()
|
|
|
+ public function Ex($file_name,$data)
|
|
|
{
|
|
|
$spreadsheet = new Spreadsheet();
|
|
|
+
|
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
|
- $file_name = 'example'; // 设置默认的文件名
|
|
|
|
|
|
- // 获取需要导出的数据
|
|
|
- $data = [
|
|
|
- ['2022-01-01', '001', '产品A'],
|
|
|
- ['2022-01-02', '002', '产品B'],
|
|
|
- ];
|
|
|
+ $sheet->setTitle($file_name);
|
|
|
|
|
|
// 将数据逐行写入工作表
|
|
|
foreach ($data as $rowIndex => $rowData) {
|
|
|
@@ -661,21 +655,12 @@ class PackagingCountDocument extends Api
|
|
|
$sheet->setCellValueByColumnAndRow($columnIndex + 1, $rowIndex + 1, $cellData);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- // 创建 Excel 文件流
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
|
- $tempFilePath = tempnam(sys_get_temp_dir(), 'excel');
|
|
|
- $writer->save($tempFilePath);
|
|
|
-
|
|
|
- // 创建响应对象并设置头信息
|
|
|
- $response = new StreamedResponse(function () use ($tempFilePath) {
|
|
|
- readfile($tempFilePath);
|
|
|
- unlink($tempFilePath);
|
|
|
- });
|
|
|
- $response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
|
- $response->headers->set('Content-Disposition', 'attachment;filename="' . $file_name . '.xlsx"');
|
|
|
-
|
|
|
- return $response;
|
|
|
+
|
|
|
+ header('Content-Disposition: attachment;filename="'.$file_name.'.xlsx"');
|
|
|
+ header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
|
+ $writer->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|