From eaf56e3652b441b96f042be9c7fc379e6895b51c Mon Sep 17 00:00:00 2001 From: augushong Date: Sun, 17 Apr 2022 20:36:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AF=BC=E5=87=BA=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/traits/Curd.php | 83 +---------------------------- app/common/tools/ExcelTools.php | 92 +++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 81 deletions(-) create mode 100644 app/common/tools/ExcelTools.php diff --git a/app/admin/traits/Curd.php b/app/admin/traits/Curd.php index ca3ed64..11ebee3 100644 --- a/app/admin/traits/Curd.php +++ b/app/admin/traits/Curd.php @@ -12,7 +12,6 @@ namespace app\admin\traits; -use app\common\tools\PathTools; use EasyAdmin\annotation\NodeAnotation; @@ -118,9 +117,6 @@ trait Curd { list($page, $limit, $where) = $this->buildTableParames(); - $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); - $sheet = $spreadsheet->getActiveSheet(); - $fields = $this->request->param('fields', '{}', null); $image_fields = $this->request->param('image_fields', '{}', null); $select_fields = $this->request->param('select_fields', '{}', null); @@ -129,84 +125,9 @@ trait Curd $image_fields = json_decode($image_fields, true); $select_fields = json_decode($select_fields, true); - $write_col = 1; - $write_line = 1; - foreach ($fields as $field_key => $field_name) { - $col_key = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($write_col); - $sheet->setCellValue($col_key . $write_line, $field_name); + $content = \app\common\tools\ExcelTools::exportModel($this->model, $where, $fields, $image_fields, $select_fields); - $write_col++; - } - - $runtime_file_list = []; - - $this->model - ->where($where)->chunk(100, function ($list) use ($sheet, &$write_line, $fields, $image_fields, &$runtime_file_list, $select_fields) { - foreach ($list as $list_index => $item) { - $write_line++; - $write_col = 1; - - foreach ($fields as $field_key => $field_name) { - $col_key = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($write_col); - - $cel = null; - - $value = \think\helper\Arr::get($item, $field_key); - - if (in_array($field_key, $image_fields)) { - // 是图片 - $cel = $value; - - try { - - if (filter_var($value, FILTER_VALIDATE_URL)) { - - $runtime_file = PathTools::tempBuildPath(uniqid()); - - $runtime_file_list[] = $runtime_file; - - file_put_contents($runtime_file, file_get_contents($value)); - - $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); - $drawing->setName($field_name); - $drawing->setDescription($field_key); - $drawing->setPath($runtime_file); - $drawing->setHeight(36); - - $drawing->setCoordinates($col_key . $write_line); - $drawing->setWorksheet($sheet); - } - } catch (\Throwable $th) { - $message = $th->getMessage(); - - $cel .= "\n" . $message; - } - } else if (array_key_exists($field_key, $select_fields)) { - // 需要设置选项 - - $cel = $select_fields[$field_key][$value]; - } else { - $cel = $value; - } - - $sheet->setCellValue($col_key . $write_line, $cel); - $write_col++; - } - } - }); - - $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet); - - ob_start(); - $writer->save('php://output'); - $content = ob_get_contents(); - ob_clean(); - - foreach ($runtime_file_list as $runtime_file) { - unlink($runtime_file); - } - - return download($content, $this->model->getName() . date('Ymd') . '.xlsx', true); + return download($content, $this->model->getName() . date('YmdHis') . '.xlsx', true); } /** diff --git a/app/common/tools/ExcelTools.php b/app/common/tools/ExcelTools.php new file mode 100644 index 0000000..f4ef6c1 --- /dev/null +++ b/app/common/tools/ExcelTools.php @@ -0,0 +1,92 @@ +getActiveSheet(); + + $write_col = 1; + $write_line = 1; + foreach ($fields as $field_key => $field_name) { + $col_key = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($write_col); + $sheet->setCellValue($col_key . $write_line, $field_name); + + $write_col++; + } + + $runtime_file_list = []; + + $model + ->where($where)->chunk(100, function ($list) use ($sheet, &$write_line, $fields, $image_fields, &$runtime_file_list, $select_fields) { + foreach ($list as $list_index => $item) { + $write_line++; + $write_col = 1; + + foreach ($fields as $field_key => $field_name) { + $col_key = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($write_col); + + $cel = null; + + $value = \think\helper\Arr::get($item, $field_key); + + if (in_array($field_key, $image_fields)) { + // 是图片 + $cel = $value; + + try { + + if (filter_var($value, FILTER_VALIDATE_URL)) { + + $runtime_file = PathTools::tempBuildPath(uniqid()); + + $runtime_file_list[] = $runtime_file; + + file_put_contents($runtime_file, file_get_contents($value)); + + $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); + $drawing->setName($field_name); + $drawing->setDescription($field_key); + $drawing->setPath($runtime_file); + $drawing->setHeight(36); + + $drawing->setCoordinates($col_key . $write_line); + $drawing->setWorksheet($sheet); + } + } catch (\Throwable $th) { + $message = $th->getMessage(); + + $cel .= "\n" . $message; + } + } else if (array_key_exists($field_key, $select_fields)) { + // 需要设置选项 + + $cel = $select_fields[$field_key][$value]; + } else { + $cel = $value; + } + + $sheet->setCellValue($col_key . $write_line, $cel); + $write_col++; + } + } + }); + + $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet); + + ob_start(); + $writer->save('php://output'); + $content = ob_get_contents(); + ob_clean(); + + foreach ($runtime_file_list as $runtime_file) { + unlink($runtime_file); + } + + return $content; + } +}