mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-01 15:32:48 +08:00
优化导出代码;
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
92
app/common/tools/ExcelTools.php
Normal file
92
app/common/tools/ExcelTools.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\tools;
|
||||
|
||||
class ExcelTools
|
||||
{
|
||||
public static function exportModel($model, $where = [], $fields = [], $image_fields = [], $select_fields = [])
|
||||
{
|
||||
|
||||
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
||||
$sheet = $spreadsheet->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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user