From 766338bf0279fa60a9eca25edf0d62f27a74305a Mon Sep 17 00:00:00 2001 From: Charles7c Date: Sun, 26 Nov 2023 19:19:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20Excel=20=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E9=85=8D=E7=BD=AE=EF=BC=88=E6=96=87=E4=BB=B6=E5=A4=84?= =?UTF-8?q?=E7=90=86=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-starter-dependencies/pom.xml | 15 +++ .../continew-starter-file-excel/pom.xml | 25 +++++ .../converter/ExcelBigNumberConverter.java | 79 ++++++++++++++++ .../starter/file/excel/util/ExcelUtils.java | 92 +++++++++++++++++++ continew-starter-file/pom.xml | 29 ++++++ pom.xml | 1 + 6 files changed, 241 insertions(+) create mode 100644 continew-starter-file/continew-starter-file-excel/pom.xml create mode 100644 continew-starter-file/continew-starter-file-excel/src/main/java/top/charles7c/continew/starter/file/excel/converter/ExcelBigNumberConverter.java create mode 100644 continew-starter-file/continew-starter-file-excel/src/main/java/top/charles7c/continew/starter/file/excel/util/ExcelUtils.java create mode 100644 continew-starter-file/pom.xml diff --git a/continew-starter-dependencies/pom.xml b/continew-starter-dependencies/pom.xml index 12c2b552..564caf11 100644 --- a/continew-starter-dependencies/pom.xml +++ b/continew-starter-dependencies/pom.xml @@ -63,6 +63,7 @@ 3.24.3 3.0.4 1.6.2 + 3.3.2 4.3.0 3.1.5.1 5.8.23 @@ -154,6 +155,13 @@ ${easy-captcha.version} + + + com.alibaba + easyexcel + ${easy-excel.version} + + com.github.xiaoymin @@ -220,6 +228,13 @@ ${revision} + + + top.charles7c.continew + continew-starter-file-excel + ${revision} + + top.charles7c.continew diff --git a/continew-starter-file/continew-starter-file-excel/pom.xml b/continew-starter-file/continew-starter-file-excel/pom.xml new file mode 100644 index 00000000..c5e9ddd1 --- /dev/null +++ b/continew-starter-file/continew-starter-file-excel/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + top.charles7c.continew + continew-starter-file + ${revision} + + + continew-starter-file-excel + jar + + ${project.artifactId} + ContiNew Starter 文件处理模块 - Excel + + + + + com.alibaba + easyexcel + + + \ No newline at end of file diff --git a/continew-starter-file/continew-starter-file-excel/src/main/java/top/charles7c/continew/starter/file/excel/converter/ExcelBigNumberConverter.java b/continew-starter-file/continew-starter-file-excel/src/main/java/top/charles7c/continew/starter/file/excel/converter/ExcelBigNumberConverter.java new file mode 100644 index 00000000..8c1db821 --- /dev/null +++ b/continew-starter-file/continew-starter-file-excel/src/main/java/top/charles7c/continew/starter/file/excel/converter/ExcelBigNumberConverter.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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.continew.starter.file.excel.converter; + +import cn.hutool.core.convert.Convert; +import cn.hutool.core.util.NumberUtil; +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; + +/** + * Easy Excel 大数值转换器 + *

+ * Excel 中对长度超过 15 位的数值输入是有限制的,从 16 位开始无论录入什么数字均会变为 0,因此输入时只能以文本的形式进行录入 + *

+ * + * @author Charles7c + * @since 1.0.0 + */ +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-starter-file/continew-starter-file-excel/src/main/java/top/charles7c/continew/starter/file/excel/util/ExcelUtils.java b/continew-starter-file/continew-starter-file-excel/src/main/java/top/charles7c/continew/starter/file/excel/util/ExcelUtils.java new file mode 100644 index 00000000..4a57f288 --- /dev/null +++ b/continew-starter-file/continew-starter-file-excel/src/main/java/top/charles7c/continew/starter/file/excel/util/ExcelUtils.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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.continew.starter.file.excel.util; + +import cn.hutool.core.date.DatePattern; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.URLUtil; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; +import jakarta.servlet.http.HttpServletResponse; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import top.charles7c.continew.starter.core.exception.BaseException; +import top.charles7c.continew.starter.file.excel.converter.ExcelBigNumberConverter; + +import java.util.Date; +import java.util.List; + +/** + * Excel 工具类 + * + * @author Charles7c + * @since 1.0.0 + */ +@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 BaseException("导出 Excel 出现错误"); + } + } +} diff --git a/continew-starter-file/pom.xml b/continew-starter-file/pom.xml new file mode 100644 index 00000000..e7eba139 --- /dev/null +++ b/continew-starter-file/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + top.charles7c.continew + continew-starter + ${revision} + + + continew-starter-file + pom + + ${project.artifactId} + ContiNew Starter 文件处理模块 + + + continew-starter-file-excel + + + + + + top.charles7c.continew + continew-starter-core + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index a0c01a3c..c326597f 100644 --- a/pom.xml +++ b/pom.xml @@ -71,6 +71,7 @@ continew-starter-core continew-starter-json continew-starter-api-doc + continew-starter-file continew-starter-captcha continew-starter-cache continew-starter-data