diff --git a/app/admin/controller/mall/Goods.php b/app/admin/controller/mall/Goods.php index b353511..b4612f6 100644 --- a/app/admin/controller/mall/Goods.php +++ b/app/admin/controller/mall/Goods.php @@ -9,7 +9,11 @@ use app\admin\traits\Curd; use app\common\controller\AdminController; use EasyAdmin\annotation\ControllerAnnotation; use EasyAdmin\annotation\NodeAnotation; +use PhpOffice\PhpSpreadsheet\Cell\Coordinate; +use PhpOffice\PhpSpreadsheet\Spreadsheet; +use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use think\App; +use think\helper\Arr; /** * Class Goods @@ -83,5 +87,5 @@ class Goods extends AdminController $this->assign('row', $row); return $this->fetch(); } - -} \ No newline at end of file + +} diff --git a/app/admin/traits/Curd.php b/app/admin/traits/Curd.php index a3440da..712f4a2 100644 --- a/app/admin/traits/Curd.php +++ b/app/admin/traits/Curd.php @@ -13,9 +13,7 @@ namespace app\admin\traits; use EasyAdmin\annotation\NodeAnotation; -use EasyAdmin\tool\CommonTool; -use jianyan\excel\Excel; -use think\facade\Db; + /** * 后台CURD复用 @@ -67,7 +65,7 @@ trait Curd try { $save = $this->model->save($post); } catch (\Exception $e) { - $this->error('保存失败:'.$e->getMessage()); + $this->error('保存失败:' . $e->getMessage()); } $save ? $this->success('保存成功') : $this->error('保存失败'); } @@ -118,26 +116,48 @@ trait Curd public function export() { list($page, $limit, $where) = $this->buildTableParames(); - $tableName = $this->model->getName(); - $tableName = CommonTool::humpToLine(lcfirst($tableName)); - $prefix = config('database.connections.mysql.prefix'); - $dbList = Db::query("show full columns from {$prefix}{$tableName}"); - $header = []; - foreach ($dbList as $vo) { - $comment = !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field']; - if (!in_array($vo['Field'], $this->noExportFields)) { - $header[] = [$comment, $vo['Field']]; - } - } - $list = $this->model - ->where($where) - ->limit(100000) - ->order('id', 'desc') - ->select() - ->toArray(); - $fileName = time(); - return Excel::exportData($list, $header, $fileName, 'xlsx',); + $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + + $fields = $this->request->param('fields', '{}', null); + + $fields = json_decode($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); + + $write_col++; + } + + $this->model + ->where($where)->chunk(100, function ($list) use ($sheet, &$write_line, $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); + + $value = \think\helper\Arr::get($item, $field_key); + + $sheet->setCellValue($col_key . $write_line, $value); + $write_col++; + } + } + }); + + $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet); + + ob_start(); + $writer->save('php://output'); + $content = ob_get_contents(); + ob_clean(); + + return download($content, $this->model->getName() . time() . '.xlsx', true); } /** @@ -169,5 +189,4 @@ trait Curd } $this->success('保存成功'); } - } diff --git a/composer.json b/composer.json index bb7c55d..fe5ab82 100644 --- a/composer.json +++ b/composer.json @@ -27,10 +27,10 @@ "qcloud/cos-sdk-v5": "^2.0", "qiniu/php-sdk": "^7.2", "alibabacloud/client": "^1.5", - "jianyan74/php-excel": "^1.0", "zhongshaofa/easy-admin": "^1.0.2", "ext-json": "*", - "guzzlehttp/guzzle": "^7.4" + "guzzlehttp/guzzle": "^7.4", + "phpoffice/phpspreadsheet": "^1.22" }, "require-dev": { "symfony/var-dumper": "^4.2", diff --git a/composer.lock b/composer.lock index 14facb2..5633e5b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b165f907f620d029664c50085142cb1a", + "content-hash": "c3e52378111fa626fade09fbbe489d34", "packages": [ { "name": "adbario/php-dot-notation", @@ -1160,60 +1160,6 @@ ], "time": "2021-10-07T12:57:01+00:00" }, - { - "name": "jianyan74/php-excel", - "version": "1.0.2", - "source": { - "type": "git", - "url": "https://github.com/jianyan74/php-excel.git", - "reference": "347e80c98a39d9743c9d7582497f891b90d5e5cf" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/jianyan74/php-excel/zipball/347e80c98a39d9743c9d7582497f891b90d5e5cf", - "reference": "347e80c98a39d9743c9d7582497f891b90d5e5cf", - "shasum": "", - "mirrors": [ - { - "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", - "preferred": true - } - ] - }, - "require": { - "php": ">=7.0", - "phpoffice/phpspreadsheet": "^1.3" - }, - "type": "extension", - "autoload": { - "psr-4": { - "jianyan\\excel\\": "./src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "jianyan74" - } - ], - "description": "php excel 导入导出", - "keywords": [ - "csv", - "excel", - "html", - "jianyan74", - "xls", - "xlsx" - ], - "support": { - "issues": "https://github.com/jianyan74/php-excel/issues", - "source": "https://github.com/jianyan74/php-excel/tree/1.0.2" - }, - "time": "2020-09-04T06:16:34+00:00" - }, { "name": "league/flysystem", "version": "1.1.9", @@ -1764,16 +1710,16 @@ }, { "name": "phpoffice/phpspreadsheet", - "version": "1.21.0", + "version": "1.22.0", "source": { "type": "git", "url": "https://github.com/PHPOffice/PhpSpreadsheet.git", - "reference": "1a359d2ccbb89c05f5dffb32711a95f4afc67964" + "reference": "3a9e29b4f386a08a151a33578e80ef1747037a48" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPOffice/PhpSpreadsheet/zipball/1a359d2ccbb89c05f5dffb32711a95f4afc67964", - "reference": "1a359d2ccbb89c05f5dffb32711a95f4afc67964", + "url": "https://api.github.com/repos/PHPOffice/PhpSpreadsheet/zipball/3a9e29b4f386a08a151a33578e80ef1747037a48", + "reference": "3a9e29b4f386a08a151a33578e80ef1747037a48", "shasum": "", "mirrors": [ { @@ -1810,7 +1756,7 @@ "dompdf/dompdf": "^1.0", "friendsofphp/php-cs-fixer": "^3.2", "jpgraph/jpgraph": "^4.0", - "mpdf/mpdf": "^8.0", + "mpdf/mpdf": "8.0.17", "phpcompatibility/php-compatibility": "^9.3", "phpstan/phpstan": "^1.1", "phpstan/phpstan-phpunit": "^1.0", @@ -1868,9 +1814,9 @@ ], "support": { "issues": "https://github.com/PHPOffice/PhpSpreadsheet/issues", - "source": "https://github.com/PHPOffice/PhpSpreadsheet/tree/1.21.0" + "source": "https://github.com/PHPOffice/PhpSpreadsheet/tree/1.22.0" }, - "time": "2022-01-06T11:10:08+00:00" + "time": "2022-02-18T12:57:07+00:00" }, { "name": "psr/cache", diff --git a/public/static/admin/css/public.css b/public/static/admin/css/public.css index 6ca2ce2..068a6c9 100644 --- a/public/static/admin/css/public.css +++ b/public/static/admin/css/public.css @@ -1,4 +1,4 @@ -@import url("../../plugs/layui-v2.5.6/css/layui.css"); +@import url("../../plugs/layui-v2.6.8/css/layui.css"); @import url("../../plugs/font-awesome-4.7.0/css/font-awesome.min.css"); @import url("../css/iconfont.css"); diff --git a/public/static/config-admin.js b/public/static/config-admin.js index 6ad933b..fa4dcc8 100644 --- a/public/static/config-admin.js +++ b/public/static/config-admin.js @@ -9,7 +9,6 @@ require.config({ "echarts": ["plugs/echarts/echarts.min"], "echarts-theme": ["plugs/echarts/echarts-theme"], "easy-admin": ["plugs/easy-admin/easy-admin"], - "layuiall": ["plugs/layui-v2.6.8/layui.all"], "layui": ["plugs/layui-v2.6.8/layui"], "miniAdmin": ["plugs/lay-module/layuimini/miniAdmin"], "miniMenu": ["plugs/lay-module/layuimini/miniMenu"], diff --git a/public/static/plugs/easy-admin/easy-admin.js b/public/static/plugs/easy-admin/easy-admin.js index 9e76668..b83e8d3 100644 --- a/public/static/plugs/easy-admin/easy-admin.js +++ b/public/static/plugs/easy-admin/easy-admin.js @@ -300,6 +300,10 @@ define(["jquery", "tableSelect", "ckeditor"], function ($, tableSelect, undefine // 监听表格开关切换 admin.table.listenEdit(options.init, options.layFilter, options.id, options.modifyReload); + admin.table.listenExport(options); + + + return newTable; }, renderToolbar: function (data, elem, tableId, init) { @@ -432,7 +436,7 @@ define(["jquery", "tableSelect", "ckeditor"], function ($, tableSelect, undefine $(elem).before('