mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-10-31 10:57:13 +08:00 
			
		
		
		
	refactor: 适配 ContiNew Starter Excel(文件处理模块)
This commit is contained in:
		| @@ -40,6 +40,12 @@ | ||||
|             <artifactId>continew-starter-captcha-graphic</artifactId> | ||||
|         </dependency> | ||||
|  | ||||
|         <!-- ContiNew Starter 文件处理模块 - Excel --> | ||||
|         <dependency> | ||||
|             <groupId>top.charles7c.continew</groupId> | ||||
|             <artifactId>continew-starter-file-excel</artifactId> | ||||
|         </dependency> | ||||
|  | ||||
|         <!-- ContiNew Starter API 文档模块 --> | ||||
|         <dependency> | ||||
|             <groupId>top.charles7c.continew</groupId> | ||||
| @@ -75,17 +81,10 @@ | ||||
|             <artifactId>mysql-connector-j</artifactId> | ||||
|         </dependency> | ||||
|  | ||||
|         <!-- ################ 工具库相关 ################ --> | ||||
|         <!-- SMS4J(短信聚合框架,轻松集成多家短信服务,解决接入多个短信 SDK 的繁琐流程) --> | ||||
|         <dependency> | ||||
|             <groupId>org.dromara.sms4j</groupId> | ||||
|             <artifactId>sms4j-spring-boot-starter</artifactId> | ||||
|         </dependency> | ||||
|  | ||||
|         <!-- Easy Excel(一个基于 Java 的、快速、简洁、解决大文件内存溢出的 Excel 处理工具) --> | ||||
|         <dependency> | ||||
|             <groupId>com.alibaba</groupId> | ||||
|             <artifactId>easyexcel</artifactId> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| </project> | ||||
| @@ -46,12 +46,12 @@ import top.charles7c.cnadmin.common.model.query.PageQuery; | ||||
| 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; | ||||
|  | ||||
| /** | ||||
|  * 业务实现基类 | ||||
|   | ||||
| @@ -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<Long> { | ||||
|  | ||||
|     /** | ||||
|      * Excel 输入数值长度限制 | ||||
|      */ | ||||
|     private static final int MAX_LENGTH = 15; | ||||
|  | ||||
|     @Override | ||||
|     public Class<Long> 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<Object> 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<Object> writeCellData = new WriteCellData<>(NumberUtil.toBigDecimal(value)); | ||||
|         writeCellData.setType(CellDataTypeEnum.NUMBER); | ||||
|         return writeCellData; | ||||
|     } | ||||
| } | ||||
| @@ -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 <T> void export(List<T> list, String fileName, Class<T> clazz, HttpServletResponse response) { | ||||
|         export(list, fileName, "Sheet1", clazz, response); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 导出 | ||||
|      * | ||||
|      * @param list | ||||
|      *            导出数据集合 | ||||
|      * @param fileName | ||||
|      *            文件名 | ||||
|      * @param sheetName | ||||
|      *            工作表名称 | ||||
|      * @param clazz | ||||
|      *            导出数据类型 | ||||
|      * @param response | ||||
|      *            响应对象 | ||||
|      */ | ||||
|     public static <T> void export(List<T> list, String fileName, String sheetName, Class<T> 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 出现错误"); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -29,7 +29,6 @@ import org.springframework.transaction.annotation.Transactional; | ||||
|  | ||||
| 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.req.DictReq; | ||||
| 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; | ||||
|  | ||||
| /** | ||||
|  * 字典业务实现 | ||||
|   | ||||
							
								
								
									
										9
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								pom.xml
									
									
									
									
									
								
							| @@ -34,7 +34,6 @@ | ||||
|  | ||||
|     <properties> | ||||
|         <revision>2.1.0-SNAPSHOT</revision> | ||||
|         <easyexcel.version>3.3.2</easyexcel.version> | ||||
|         <!-- Maven Plugin Versions --> | ||||
|         <spotless.version>2.40.0</spotless.version> | ||||
|     </properties> | ||||
| @@ -42,14 +41,6 @@ | ||||
|     <!-- 全局依赖版本管理 --> | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <!-- ################ 工具库相关 ################ --> | ||||
|             <!-- Easy Excel(一个基于 Java 的、快速、简洁、解决大文件内存溢出的 Excel 处理工具) --> | ||||
|             <dependency> | ||||
|                 <groupId>com.alibaba</groupId> | ||||
|                 <artifactId>easyexcel</artifactId> | ||||
|                 <version>${easyexcel.version}</version> | ||||
|             </dependency> | ||||
|  | ||||
|             <!-- API 模块(存放 Controller 层代码,打包部署的模块) --> | ||||
|             <dependency> | ||||
|                 <groupId>top.charles7c.continew</groupId> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user