feat: 新增 Excel 相关配置(文件处理模块)

This commit is contained in:
2023-11-26 19:19:34 +08:00
parent 25a6e69444
commit 766338bf02
6 changed files with 241 additions and 0 deletions

View File

@@ -63,6 +63,7 @@
<redisson.version>3.24.3</redisson.version>
<sms4j.version>3.0.4</sms4j.version>
<easy-captcha.version>1.6.2</easy-captcha.version>
<easy-excel.version>3.3.2</easy-excel.version>
<knife4j.version>4.3.0</knife4j.version>
<ip2region.version>3.1.5.1</ip2region.version>
<hutool.version>5.8.23</hutool.version>
@@ -154,6 +155,13 @@
<version>${easy-captcha.version}</version>
</dependency>
<!-- Easy Excel一个基于 Java 的、快速、简洁、解决大文件内存溢出的 Excel 处理工具) -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>${easy-excel.version}</version>
</dependency>
<!-- Knife4j前身是 swagger-bootstrap-ui集 Swagger2 和 OpenAPI3 为一体的增强解决方案) -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
@@ -220,6 +228,13 @@
<version>${revision}</version>
</dependency>
<!-- 文件处理模块 - Excel -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-file-excel</artifactId>
<version>${revision}</version>
</dependency>
<!-- API 文档模块 -->
<dependency>
<groupId>top.charles7c.continew</groupId>

View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-file</artifactId>
<version>${revision}</version>
</parent>
<artifactId>continew-starter-file-excel</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>ContiNew Starter 文件处理模块 - Excel</description>
<dependencies>
<!-- Easy Excel一个基于 Java 的、快速、简洁、解决大文件内存溢出的 Excel 处理工具) -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,79 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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 大数值转换器
* <p>
* Excel 中对长度超过 15 位的数值输入是有限制的,从 16 位开始无论录入什么数字均会变为 0因此输入时只能以文本的形式进行录入
* </p>
*
* @author Charles7c
* @since 1.0.0
*/
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;
}
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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 <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 BaseException("导出 Excel 出现错误");
}
}
}

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter</artifactId>
<version>${revision}</version>
</parent>
<artifactId>continew-starter-file</artifactId>
<packaging>pom</packaging>
<name>${project.artifactId}</name>
<description>ContiNew Starter 文件处理模块</description>
<modules>
<module>continew-starter-file-excel</module>
</modules>
<dependencies>
<!-- 核心模块 -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-core</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -71,6 +71,7 @@
<module>continew-starter-core</module>
<module>continew-starter-json</module>
<module>continew-starter-api-doc</module>
<module>continew-starter-file</module>
<module>continew-starter-captcha</module>
<module>continew-starter-cache</module>
<module>continew-starter-data</module>