From 1311ae3603a26dc44dfffc5be86ea1ab81ff7958 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Sun, 26 Nov 2023 19:20:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=80=82=E9=85=8D=20ContiNew=20Sta?= =?UTF-8?q?rter=20Excel=EF=BC=88=E6=96=87=E4=BB=B6=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- continew-admin-common/pom.xml | 13 ++- .../cnadmin/common/base/BaseServiceImpl.java | 2 +- .../easyexcel/ExcelBigNumberConverter.java | 77 --------------- .../cnadmin/common/util/ExcelUtils.java | 96 ------------------- .../system/service/impl/DictServiceImpl.java | 2 +- pom.xml | 9 -- 6 files changed, 8 insertions(+), 191 deletions(-) delete mode 100644 continew-admin-common/src/main/java/top/charles7c/cnadmin/common/config/easyexcel/ExcelBigNumberConverter.java delete mode 100644 continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/ExcelUtils.java diff --git a/continew-admin-common/pom.xml b/continew-admin-common/pom.xml index cd596e9b..026f3e08 100644 --- a/continew-admin-common/pom.xml +++ b/continew-admin-common/pom.xml @@ -40,6 +40,12 @@ continew-starter-captcha-graphic + + + top.charles7c.continew + continew-starter-file-excel + + top.charles7c.continew @@ -75,17 +81,10 @@ mysql-connector-j - org.dromara.sms4j sms4j-spring-boot-starter - - - - com.alibaba - easyexcel - \ No newline at end of file diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/base/BaseServiceImpl.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/base/BaseServiceImpl.java index 4c97252d..41862b02 100644 --- a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/base/BaseServiceImpl.java +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/base/BaseServiceImpl.java @@ -46,12 +46,12 @@ import top.charles7c.cnadmin.common.model.query.SortQuery; import top.charles7c.cnadmin.common.model.resp.PageDataResp; import top.charles7c.cnadmin.common.service.CommonUserService; -import top.charles7c.cnadmin.common.util.ExcelUtils; import top.charles7c.cnadmin.common.util.ReflectUtils; import top.charles7c.cnadmin.common.util.TreeUtils; import top.charles7c.cnadmin.common.util.helper.QueryHelper; import top.charles7c.cnadmin.common.util.validate.CheckUtils; import top.charles7c.continew.starter.core.util.ExceptionUtils; +import top.charles7c.continew.starter.file.excel.util.ExcelUtils; /** * 业务实现基类 diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/config/easyexcel/ExcelBigNumberConverter.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/config/easyexcel/ExcelBigNumberConverter.java deleted file mode 100644 index 5d5814cf..00000000 --- a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/config/easyexcel/ExcelBigNumberConverter.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package top.charles7c.cnadmin.common.config.easyexcel; - -import com.alibaba.excel.converters.Converter; -import com.alibaba.excel.enums.CellDataTypeEnum; -import com.alibaba.excel.metadata.GlobalConfiguration; -import com.alibaba.excel.metadata.data.ReadCellData; -import com.alibaba.excel.metadata.data.WriteCellData; -import com.alibaba.excel.metadata.property.ExcelContentProperty; - -import cn.hutool.core.convert.Convert; -import cn.hutool.core.util.NumberUtil; - -/** - * Easy Excel 大数值转换器(Excel 中对长度超过 15 位的数值输入是有限制的,从 16 位开始无论录入什么数字均会变为 0,因此输入时只能以文本的形式进行录入) - * - * @author Charles7c - * @since 2023/2/5 19:29 - */ -public class ExcelBigNumberConverter implements Converter { - - /** - * Excel 输入数值长度限制 - */ - private static final int MAX_LENGTH = 15; - - @Override - public Class supportJavaTypeKey() { - return Long.class; - } - - @Override - public CellDataTypeEnum supportExcelTypeKey() { - return CellDataTypeEnum.STRING; - } - - /** - * 转换为 Java 数据(读取 Excel) - */ - @Override - public Long convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, - GlobalConfiguration globalConfiguration) { - return Convert.toLong(cellData.getData()); - } - - /** - * 转换为 Excel 数据(写入 Excel) - */ - @Override - public WriteCellData convertToExcelData(Long value, ExcelContentProperty contentProperty, - GlobalConfiguration globalConfiguration) { - if (null != value) { - String str = Long.toString(value); - if (str.length() > MAX_LENGTH) { - return new WriteCellData<>(str); - } - } - WriteCellData writeCellData = new WriteCellData<>(NumberUtil.toBigDecimal(value)); - writeCellData.setType(CellDataTypeEnum.NUMBER); - return writeCellData; - } -} diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/ExcelUtils.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/ExcelUtils.java deleted file mode 100644 index 831751f4..00000000 --- a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/ExcelUtils.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package top.charles7c.cnadmin.common.util; - -import java.util.Date; -import java.util.List; - -import jakarta.servlet.http.HttpServletResponse; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import lombok.extern.slf4j.Slf4j; - -import com.alibaba.excel.EasyExcel; -import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; - -import cn.hutool.core.date.DatePattern; -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.URLUtil; - -import top.charles7c.cnadmin.common.config.easyexcel.ExcelBigNumberConverter; -import top.charles7c.cnadmin.common.exception.ServiceException; - -/** - * Excel 工具类 - * - * @author Charles7c - * @since 2023/2/5 18:00 - */ -@Slf4j -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class ExcelUtils { - - /** - * 导出 - * - * @param list - * 导出数据集合 - * @param fileName - * 文件名 - * @param clazz - * 导出数据类型 - * @param response - * 响应对象 - */ - public static void export(List list, String fileName, Class clazz, HttpServletResponse response) { - export(list, fileName, "Sheet1", clazz, response); - } - - /** - * 导出 - * - * @param list - * 导出数据集合 - * @param fileName - * 文件名 - * @param sheetName - * 工作表名称 - * @param clazz - * 导出数据类型 - * @param response - * 响应对象 - */ - public static void export(List list, String fileName, String sheetName, Class clazz, - HttpServletResponse response) { - try { - fileName = - String.format("%s_%s.xlsx", fileName, DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN)); - fileName = URLUtil.encode(fileName); - response.setHeader("Content-disposition", "attachment;filename=" + fileName); - response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"); - EasyExcel.write(response.getOutputStream(), clazz).autoCloseStream(false) - // 自动适配宽度 - .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) - // 自动转换大数值 - .registerConverter(new ExcelBigNumberConverter()).sheet(sheetName).doWrite(list); - } catch (Exception e) { - log.error("Export excel occurred an error: {}. fileName: {}.", e.getMessage(), fileName, e); - throw new ServiceException("导出 Excel 出现错误"); - } - } -} diff --git a/continew-admin-system/src/main/java/top/charles7c/cnadmin/system/service/impl/DictServiceImpl.java b/continew-admin-system/src/main/java/top/charles7c/cnadmin/system/service/impl/DictServiceImpl.java index 3690065a..6e0923fd 100644 --- a/continew-admin-system/src/main/java/top/charles7c/cnadmin/system/service/impl/DictServiceImpl.java +++ b/continew-admin-system/src/main/java/top/charles7c/cnadmin/system/service/impl/DictServiceImpl.java @@ -29,7 +29,6 @@ import top.charles7c.cnadmin.common.base.BaseServiceImpl; import top.charles7c.cnadmin.common.model.query.SortQuery; -import top.charles7c.cnadmin.common.util.ExcelUtils; import top.charles7c.cnadmin.common.util.validate.CheckUtils; import top.charles7c.cnadmin.system.mapper.DictMapper; import top.charles7c.cnadmin.system.model.entity.DictDO; @@ -38,6 +37,7 @@ import top.charles7c.cnadmin.system.model.resp.*; import top.charles7c.cnadmin.system.service.DictItemService; import top.charles7c.cnadmin.system.service.DictService; +import top.charles7c.continew.starter.file.excel.util.ExcelUtils; /** * 字典业务实现 diff --git a/pom.xml b/pom.xml index 01a3c54e..2722549f 100644 --- a/pom.xml +++ b/pom.xml @@ -34,7 +34,6 @@ 2.1.0-SNAPSHOT - 3.3.2 2.40.0 @@ -42,14 +41,6 @@ - - - - com.alibaba - easyexcel - ${easyexcel.version} - - top.charles7c.continew