diff --git a/app/admin/controller/system/Log.php b/app/admin/controller/system/Log.php index 5cfd384..1e54eb4 100644 --- a/app/admin/controller/system/Log.php +++ b/app/admin/controller/system/Log.php @@ -54,7 +54,7 @@ class Log extends AdminController } #[NodeAnnotation(title: '导出', auth: true)] - public function export(): bool + public function export() { if (env('EASYADMIN.IS_DEMO', false)) { $this->error('演示环境下不允许操作'); @@ -75,7 +75,6 @@ class Log extends AdminController $model = (new self::$model)->setSuffix("_$month")->with('admin')->where($where); try { $list = $model - ->where($where) ->limit(10000) ->order('id', 'desc') ->select() @@ -84,11 +83,10 @@ class Log extends AdminController $vo['content'] = json_encode($vo['content'], JSON_UNESCAPED_UNICODE); $vo['response'] = json_encode($vo['response'], JSON_UNESCAPED_UNICODE); } - }catch (PDOException|DbException $exception) { + exportExcel($header, $list, '操作日志'); + }catch (\Throwable $exception) { $this->error($exception->getMessage()); } - $fileName = time(); - return Excel::exportData($list, $header, $fileName, 'xlsx'); } diff --git a/app/admin/traits/Curd.php b/app/admin/traits/Curd.php index 6d4436e..dab79d4 100644 --- a/app/admin/traits/Curd.php +++ b/app/admin/traits/Curd.php @@ -5,7 +5,6 @@ namespace app\admin\traits; use app\admin\service\annotation\NodeAnnotation; use app\admin\service\tool\CommonTool; use app\Request; -use jianyan\excel\Excel; use think\facade\Db; use think\response\Json; @@ -113,13 +112,16 @@ trait Curd $header[] = [$comment, $vo['Field']]; } } - $list = self::$model::where($where) + $list = self::$model::where($where) ->limit(100000) - ->order('id', 'desc') + ->order($this->sort) ->select() ->toArray(); - $fileName = time(); - return Excel::exportData($list, $header, $fileName, 'xlsx'); + try { + exportExcel($header, $list); + }catch (\Throwable $exception) { + $this->error('导出失败: ' . $exception->getMessage() . PHP_EOL . $exception->getFile() . PHP_EOL . $exception->getLine()); + } } #[NodeAnnotation(title: '属性修改', auth: true)] diff --git a/app/common.php b/app/common.php index b382fc3..8abf212 100644 --- a/app/common.php +++ b/app/common.php @@ -2,6 +2,8 @@ // 应用公共文件 use app\common\service\AuthService; +use PhpOffice\PhpSpreadsheet\Spreadsheet; +use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; @@ -115,6 +117,48 @@ function editor_textarea(?string $detail, string $name = 'desc', string $placeho 'ckeditor' => "", 'ueditor' => "", 'EasyMDE' => "", - default => "
", + default => "
", }; } + +/** + * @desc 导出excel + * @tip 追求性能请使用 xlsWriter https://xlswriter-docs.viest.me/zh-cn + * @param array $header + * @param array $list + * @param string $fileName + * @return void + * @throws Exception + */ +function exportExcel(array $header = [], array $list = [], string $fileName = ''): void +{ + if (empty($fileName)) $fileName = time(); + if (empty($header) || empty($list)) throw new \Exception('导出数据不能为空'); + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $headers = array_column($header, 0) ?? array_keys($list[0]); + $sheet->fromArray([$headers], null, 'A1'); + $rowIndex = 2; + foreach ($list as $row) { + $rowData = []; + foreach ($header as $item) { + $value = $row[$item[1]] ?? ''; + if ($value === null) { + $rowData[] = ''; + continue; + } + $rowData[] = $value; + } + $sheet->fromArray([$rowData], null, "A{$rowIndex}"); + $rowIndex++; + } + foreach (range('A', $sheet->getHighestColumn()) as $col) { + $sheet->getColumnDimension($col)->setAutoSize(true); + } + header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); + header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"'); + header('Cache-Control: max-age=0'); + $writer = new Xlsx($spreadsheet); + $writer->save('php://output'); + die(); +} \ No newline at end of file diff --git a/composer.json b/composer.json index 16a9081..1480072 100644 --- a/composer.json +++ b/composer.json @@ -29,9 +29,8 @@ "topthink/think-filesystem": "^2.0", "aliyuncs/oss-sdk-php": "^2.7.2", "qcloud/cos-sdk-v5": "^2.6", - "jianyan74/php-excel": "^1.0.2", "doctrine/annotations": "^2.0.0", - "phpoffice/phpspreadsheet": "^1.28", + "phpoffice/phpspreadsheet": "^4.1.0", "myclabs/php-enum": "^1.8", "qiniu/php-sdk": "^7.11.0", "wolf-leo/phplogviewer": "^0.11.3", diff --git a/public/static/admin/js/index.js b/public/static/admin/js/index.js index ca5cfe7..8316721 100644 --- a/public/static/admin/js/index.js +++ b/public/static/admin/js/index.js @@ -160,6 +160,7 @@ define(["jquery", "easy-admin", "echarts", "echarts-theme", "miniAdmin", "miniTh area: ['50%', '90%'], shade: 0.8, shadeClose: true, + scrollbar: false, content: html, success: function () { layui.code({elem: '.code-demo', theme: 'dark', lang: 'php'});