mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
refactor(excel): file => excel
This commit is contained in:
@@ -196,13 +196,24 @@
|
|||||||
<version>${revision}</version>
|
<version>${revision}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- 文件处理模块 - Excel -->
|
<!-- Excel 文件处理模块 - FastExcel -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>top.continew</groupId>
|
<groupId>top.continew</groupId>
|
||||||
<artifactId>continew-starter-file-excel</artifactId>
|
<artifactId>continew-starter-excel-fastexcel</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Excel 文件处理模块 - POI -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>top.continew</groupId>
|
||||||
|
<artifactId>continew-starter-excel-poi</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Excel 文件处理模块 - 核心模块 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>top.continew</groupId>
|
||||||
|
<artifactId>continew-starter-excel-core</artifactId>
|
||||||
<version>${revision}</version>
|
<version>${revision}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<!-- 存储模块 - 本地存储 -->
|
<!-- 存储模块 - 本地存储 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@@ -32,6 +32,7 @@
|
|||||||
<easy-captcha.version>1.6.2</easy-captcha.version>
|
<easy-captcha.version>1.6.2</easy-captcha.version>
|
||||||
<nashorn.version>15.6</nashorn.version>
|
<nashorn.version>15.6</nashorn.version>
|
||||||
<fastexcel.version>1.2.0</fastexcel.version>
|
<fastexcel.version>1.2.0</fastexcel.version>
|
||||||
|
<poi.version>5.4.1</poi.version>
|
||||||
<x-file-storage.version>2.2.1</x-file-storage.version>
|
<x-file-storage.version>2.2.1</x-file-storage.version>
|
||||||
<aws-s3-v1.version>1.12.783</aws-s3-v1.version>
|
<aws-s3-v1.version>1.12.783</aws-s3-v1.version>
|
||||||
<aws-sdk.version>2.31.63</aws-sdk.version>
|
<aws-sdk.version>2.31.63</aws-sdk.version>
|
||||||
@@ -207,13 +208,20 @@
|
|||||||
<version>${nashorn.version}</version>
|
<version>${nashorn.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Easy Excel(基于 Java 的、快速、简洁、解决大文件内存溢出的 Excel 处理工具) -->
|
<!-- FastExcel(基于 Java 的快速、简洁、解决大文件内存溢出的 Excel 处理工具) -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.idev.excel</groupId>
|
<groupId>cn.idev.excel</groupId>
|
||||||
<artifactId>fastexcel</artifactId>
|
<artifactId>fastexcel</artifactId>
|
||||||
<version>${fastexcel.version}</version>
|
<version>${fastexcel.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Apache POI(适用于 Microsoft 文档的 Java API) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.poi</groupId>
|
||||||
|
<artifactId>poi-ooxml</artifactId>
|
||||||
|
<version>${poi.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- X File Storage(一行代码将文件存储到本地、FTP、SFTP、WebDAV、阿里云 OSS、华为云 OBS...等其它兼容 S3 协议的存储平台) -->
|
<!-- X File Storage(一行代码将文件存储到本地、FTP、SFTP、WebDAV、阿里云 OSS、华为云 OBS...等其它兼容 S3 协议的存储平台) -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.dromara.x-file-storage</groupId>
|
<groupId>org.dromara.x-file-storage</groupId>
|
||||||
|
17
continew-starter-excel/continew-starter-excel-core/pom.xml
Normal file
17
continew-starter-excel/continew-starter-excel-core/pom.xml
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<?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.continew</groupId>
|
||||||
|
<artifactId>continew-starter-excel</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>continew-starter-excel-core</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>${project.artifactId}</name>
|
||||||
|
<description>ContiNew Starter Excel 文件处理模块 - 核心模块</description>
|
||||||
|
</project>
|
@@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* 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.continew.excel.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Excel导出注解
|
||||||
|
*
|
||||||
|
* @author jiang4yu
|
||||||
|
* @since 2.13.0
|
||||||
|
*/
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface ExcelExport {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字段名称
|
||||||
|
*/
|
||||||
|
String value();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导出排序先后: 数字越小越靠前(默认按Java类字段顺序导出)
|
||||||
|
*/
|
||||||
|
int sort() default 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导出映射,格式如:0-未知;1-男;2-女
|
||||||
|
*/
|
||||||
|
String kv() default "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导出模板示例值(有值的话,直接取该值,不做映射)
|
||||||
|
*/
|
||||||
|
String example() default "";
|
||||||
|
|
||||||
|
}
|
@@ -1,4 +1,20 @@
|
|||||||
package top.continew.starter.file.poi.annotation;
|
/*
|
||||||
|
* 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.continew.excel.annotation;
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
@@ -6,7 +22,10 @@ import java.lang.annotation.RetentionPolicy;
|
|||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Excel 导入注解
|
||||||
|
*
|
||||||
* @author jiang4yu
|
* @author jiang4yu
|
||||||
|
* @since 2.13.0
|
||||||
*/
|
*/
|
||||||
@Target(ElementType.FIELD)
|
@Target(ElementType.FIELD)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
@@ -5,18 +5,24 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>top.continew</groupId>
|
<groupId>top.continew</groupId>
|
||||||
<artifactId>continew-starter-file</artifactId>
|
<artifactId>continew-starter-excel</artifactId>
|
||||||
<version>${revision}</version>
|
<version>${revision}</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>continew-starter-file-excel</artifactId>
|
<artifactId>continew-starter-excel-fastexcel</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>${project.artifactId}</name>
|
<name>${project.artifactId}</name>
|
||||||
<description>ContiNew Starter 文件处理模块 - Excel</description>
|
<description>ContiNew Starter Excel 文件处理模块 - FastExcel</description>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!-- Easy Excel(基于 Java 的、快速、简洁、解决大文件内存溢出的 Excel 处理工具) -->
|
<!-- Excel 文件处理模块 - 核心模块 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>top.continew</groupId>
|
||||||
|
<artifactId>continew-starter-excel-core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- FastExcel(基于 Java 的快速、简洁、解决大文件内存溢出的 Excel 处理工具) -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.idev.excel</groupId>
|
<groupId>cn.idev.excel</groupId>
|
||||||
<artifactId>fastexcel</artifactId>
|
<artifactId>fastexcel</artifactId>
|
@@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package top.continew.starter.file.excel.converter;
|
package top.continew.starter.excel.converter;
|
||||||
|
|
||||||
import cn.idev.excel.converters.Converter;
|
import cn.idev.excel.converters.Converter;
|
||||||
import cn.idev.excel.enums.CellDataTypeEnum;
|
import cn.idev.excel.enums.CellDataTypeEnum;
|
@@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package top.continew.starter.file.excel.converter;
|
package top.continew.starter.excel.converter;
|
||||||
|
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.util.NumberUtil;
|
import cn.hutool.core.util.NumberUtil;
|
@@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package top.continew.starter.file.excel.converter;
|
package top.continew.starter.excel.converter;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.text.CharSequenceUtil;
|
import cn.hutool.core.text.CharSequenceUtil;
|
@@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package top.continew.starter.file.excel.util;
|
package top.continew.starter.excel.util;
|
||||||
|
|
||||||
import cn.hutool.core.date.DatePattern;
|
import cn.hutool.core.date.DatePattern;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
@@ -25,7 +25,7 @@ import jakarta.servlet.http.HttpServletResponse;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import top.continew.starter.core.exception.BaseException;
|
import top.continew.starter.core.exception.BaseException;
|
||||||
import top.continew.starter.file.excel.converter.ExcelBigNumberConverter;
|
import top.continew.starter.excel.converter.ExcelBigNumberConverter;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
@@ -3,37 +3,33 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>top.continew</groupId>
|
<groupId>top.continew</groupId>
|
||||||
<artifactId>continew-starter-file</artifactId>
|
<artifactId>continew-starter-excel</artifactId>
|
||||||
<version>${revision}</version>
|
<version>${revision}</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>continew-starter-file-poi</artifactId>
|
<artifactId>continew-starter-excel-poi</artifactId>
|
||||||
<description>ContiNew Starter 文件处理模块 - POI</description>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>${project.artifactId}</name>
|
||||||
|
<description>ContiNew Starter Excel 文件处理模块 - POI</description>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<!-- Excel 文件处理模块 - 核心模块 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>top.continew</groupId>
|
||||||
|
<artifactId>continew-starter-excel-core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Apache POI(适用于 Microsoft 文档的 Java API) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.poi</groupId>
|
||||||
|
<artifactId>poi-ooxml</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 文件上传 -->
|
<!-- 文件上传 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||||
<artifactId>httpclient5</artifactId>
|
<artifactId>httpclient5</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- JSON -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.alibaba</groupId>
|
|
||||||
<artifactId>fastjson</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-webmvc</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>jakarta.servlet</groupId>
|
|
||||||
<artifactId>jakarta.servlet-api</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<!-- POI -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.poi</groupId>
|
|
||||||
<artifactId>poi-ooxml</artifactId>
|
|
||||||
<version>5.4.1</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
@@ -1,9 +1,28 @@
|
|||||||
package top.continew.starter.file.poi.model;
|
/*
|
||||||
|
* 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.continew.starter.excel.model;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Excel 字段信息
|
||||||
|
*
|
||||||
* @author jiang4yu
|
* @author jiang4yu
|
||||||
|
* @since 2.13.0
|
||||||
*/
|
*/
|
||||||
public class ExcelClassField {
|
public class ExcelClassField {
|
||||||
|
|
@@ -1,7 +1,23 @@
|
|||||||
package top.continew.starter.file.poi.util;
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONArray;
|
package top.continew.starter.excel.util;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
|
import cn.hutool.json.JSONArray;
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
import jakarta.servlet.ServletOutputStream;
|
import jakarta.servlet.ServletOutputStream;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
|
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
|
||||||
@@ -15,9 +31,9 @@ import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|||||||
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
|
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import top.continew.starter.file.poi.annotation.ExcelExport;
|
import top.continew.excel.annotation.ExcelExport;
|
||||||
import top.continew.starter.file.poi.annotation.ExcelImport;
|
import top.continew.excel.annotation.ExcelImport;
|
||||||
import top.continew.starter.file.poi.model.ExcelClassField;
|
import top.continew.starter.excel.model.ExcelClassField;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
@@ -30,11 +46,11 @@ import java.util.*;
|
|||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Excel导入导出工具类
|
* Excel导入导出工具类
|
||||||
*
|
*
|
||||||
* @author jiang4yu
|
* @author jiang4yu
|
||||||
|
* @since 2.13.0
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class ExcelUtils {
|
public class ExcelUtils {
|
||||||
@@ -56,7 +72,6 @@ public class ExcelUtils {
|
|||||||
private static final int BYTES_DEFAULT_LENGTH = 10240;
|
private static final int BYTES_DEFAULT_LENGTH = 10240;
|
||||||
private static final NumberFormat NUMBER_FORMAT = NumberFormat.getNumberInstance();
|
private static final NumberFormat NUMBER_FORMAT = NumberFormat.getNumberInstance();
|
||||||
|
|
||||||
|
|
||||||
public static <T> List<T> readFile(File file, Class<T> clazz) throws Exception {
|
public static <T> List<T> readFile(File file, Class<T> clazz) throws Exception {
|
||||||
JSONArray array = readFile(file);
|
JSONArray array = readFile(file);
|
||||||
return getBeanList(array, clazz);
|
return getBeanList(array, clazz);
|
||||||
@@ -105,7 +120,7 @@ public class ExcelUtils {
|
|||||||
for (Field field : fields) {
|
for (Field field : fields) {
|
||||||
// 行号
|
// 行号
|
||||||
if (field.getName().equals(ROW_NUM)) {
|
if (field.getName().equals(ROW_NUM)) {
|
||||||
rowNum = obj.getInteger(ROW_NUM);
|
rowNum = obj.getInt(ROW_NUM);
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
field.set(t, rowNum);
|
field.set(t, rowNum);
|
||||||
continue;
|
continue;
|
||||||
@@ -125,11 +140,11 @@ public class ExcelUtils {
|
|||||||
setFieldValue(t, field, obj, uniqueBuilder, errMsgList);
|
setFieldValue(t, field, obj, uniqueBuilder, errMsgList);
|
||||||
}
|
}
|
||||||
// 数据唯一性校验
|
// 数据唯一性校验
|
||||||
if (uniqueBuilder.length() > 0) {
|
if (!uniqueBuilder.isEmpty()) {
|
||||||
if (uniqueMap.containsValue(uniqueBuilder.toString())) {
|
if (uniqueMap.containsValue(uniqueBuilder.toString())) {
|
||||||
Set<Integer> rowNumKeys = uniqueMap.keySet();
|
Set<Integer> rowNumKeys = uniqueMap.keySet();
|
||||||
for (Integer num : rowNumKeys) {
|
for (Integer num : rowNumKeys) {
|
||||||
if (uniqueMap.get(num).equals(uniqueBuilder.toString())) {
|
if (uniqueMap.get(num).contentEquals(uniqueBuilder)) {
|
||||||
errMsgList.add(String.format("数据唯一性校验失败,(%s)与第%s行重复)", uniqueBuilder, num));
|
errMsgList.add(String.format("数据唯一性校验失败,(%s)与第%s行重复)", uniqueBuilder, num));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -160,20 +175,24 @@ public class ExcelUtils {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> void setFieldValue(T t, Field field, JSONObject obj, StringBuilder uniqueBuilder, List<String> errMsgList) {
|
private static <T> void setFieldValue(T t,
|
||||||
|
Field field,
|
||||||
|
JSONObject obj,
|
||||||
|
StringBuilder uniqueBuilder,
|
||||||
|
List<String> errMsgList) {
|
||||||
// 获取 ExcelImport 注解属性
|
// 获取 ExcelImport 注解属性
|
||||||
ExcelImport annotation = field.getAnnotation(ExcelImport.class);
|
ExcelImport annotation = field.getAnnotation(ExcelImport.class);
|
||||||
if (annotation == null) {
|
if (annotation == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String cname = annotation.value();
|
String cname = annotation.value();
|
||||||
if (cname.trim().length() == 0) {
|
if (cname.trim().isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 获取具体值
|
// 获取具体值
|
||||||
String val = null;
|
String val = null;
|
||||||
if (obj.containsKey(cname)) {
|
if (obj.containsKey(cname)) {
|
||||||
val = getString(obj.getString(cname));
|
val = getString(obj.getStr(cname));
|
||||||
}
|
}
|
||||||
if (val == null) {
|
if (val == null) {
|
||||||
return;
|
return;
|
||||||
@@ -188,7 +207,7 @@ public class ExcelUtils {
|
|||||||
// 数据唯一性获取
|
// 数据唯一性获取
|
||||||
boolean unique = annotation.unique();
|
boolean unique = annotation.unique();
|
||||||
if (unique) {
|
if (unique) {
|
||||||
if (uniqueBuilder.length() > 0) {
|
if (!uniqueBuilder.isEmpty()) {
|
||||||
uniqueBuilder.append("--").append(val);
|
uniqueBuilder.append("--").append(val);
|
||||||
} else {
|
} else {
|
||||||
uniqueBuilder.append(val);
|
uniqueBuilder.append(val);
|
||||||
@@ -323,7 +342,7 @@ public class ExcelUtils {
|
|||||||
}
|
}
|
||||||
// 如果表头没有数据则不进行解析
|
// 如果表头没有数据则不进行解析
|
||||||
if (keyMap.isEmpty()) {
|
if (keyMap.isEmpty()) {
|
||||||
return (JSONArray) Collections.emptyList();
|
return (JSONArray)Collections.emptyList();
|
||||||
}
|
}
|
||||||
// 获取每行JSON对象的值
|
// 获取每行JSON对象的值
|
||||||
JSONArray array = new JSONArray();
|
JSONArray array = new JSONArray();
|
||||||
@@ -331,9 +350,9 @@ public class ExcelUtils {
|
|||||||
if (rowStart == rowEnd) {
|
if (rowStart == rowEnd) {
|
||||||
JSONObject obj = new JSONObject();
|
JSONObject obj = new JSONObject();
|
||||||
// 添加行号
|
// 添加行号
|
||||||
obj.put(ROW_NUM, 1);
|
obj.putOnce(ROW_NUM, 1);
|
||||||
for (int i : keyMap.keySet()) {
|
for (int i : keyMap.keySet()) {
|
||||||
obj.put(keyMap.get(i), "");
|
obj.set(keyMap.get(i), "");
|
||||||
}
|
}
|
||||||
array.add(obj);
|
array.add(obj);
|
||||||
return array;
|
return array;
|
||||||
@@ -342,14 +361,14 @@ public class ExcelUtils {
|
|||||||
Row eachRow = sheet.getRow(i);
|
Row eachRow = sheet.getRow(i);
|
||||||
JSONObject obj = new JSONObject();
|
JSONObject obj = new JSONObject();
|
||||||
// 添加行号
|
// 添加行号
|
||||||
obj.put(ROW_NUM, i + 1);
|
obj.set(ROW_NUM, i + 1);
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (int k = cellStart; k < cellEnd; k++) {
|
for (int k = cellStart; k < cellEnd; k++) {
|
||||||
if (eachRow != null) {
|
if (eachRow != null) {
|
||||||
String val = getCellValue(eachRow.getCell(k));
|
String val = getCellValue(eachRow.getCell(k));
|
||||||
// 所有数据添加到里面,用于判断该行是否为空
|
// 所有数据添加到里面,用于判断该行是否为空
|
||||||
sb.append(val);
|
sb.append(val);
|
||||||
obj.put(keyMap.get(k), val);
|
obj.set(keyMap.get(k), val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!sb.isEmpty()) {
|
if (!sb.isEmpty()) {
|
||||||
@@ -394,18 +413,25 @@ public class ExcelUtils {
|
|||||||
exportTemplate(response, fileName, fileName, clazz, false);
|
exportTemplate(response, fileName, fileName, clazz, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> void exportTemplate(HttpServletResponse response, String fileName, String sheetName,
|
public static <T> void exportTemplate(HttpServletResponse response,
|
||||||
|
String fileName,
|
||||||
|
String sheetName,
|
||||||
Class<T> clazz) {
|
Class<T> clazz) {
|
||||||
exportTemplate(response, fileName, sheetName, clazz, false);
|
exportTemplate(response, fileName, sheetName, clazz, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> void exportTemplate(HttpServletResponse response, String fileName, Class<T> clazz,
|
public static <T> void exportTemplate(HttpServletResponse response,
|
||||||
|
String fileName,
|
||||||
|
Class<T> clazz,
|
||||||
boolean isContainExample) {
|
boolean isContainExample) {
|
||||||
exportTemplate(response, fileName, fileName, clazz, isContainExample);
|
exportTemplate(response, fileName, fileName, clazz, isContainExample);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> void exportTemplate(HttpServletResponse response, String fileName, String sheetName,
|
public static <T> void exportTemplate(HttpServletResponse response,
|
||||||
Class<T> clazz, boolean isContainExample) {
|
String fileName,
|
||||||
|
String sheetName,
|
||||||
|
Class<T> clazz,
|
||||||
|
boolean isContainExample) {
|
||||||
// 获取表头字段
|
// 获取表头字段
|
||||||
List<ExcelClassField> headFieldList = getExcelClassFieldList(clazz);
|
List<ExcelClassField> headFieldList = getExcelClassFieldList(clazz);
|
||||||
// 获取表头数据和示例数据
|
// 获取表头数据和示例数据
|
||||||
@@ -671,18 +697,24 @@ public class ExcelUtils {
|
|||||||
export(response, fileName, fileName, sheetDataList, null);
|
export(response, fileName, fileName, sheetDataList, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void exportManySheet(HttpServletResponse response, String fileName, Map<String, List<List<Object>>> sheetMap) {
|
public static void exportManySheet(HttpServletResponse response,
|
||||||
|
String fileName,
|
||||||
|
Map<String, List<List<Object>>> sheetMap) {
|
||||||
export(response, null, fileName, sheetMap, null);
|
export(response, null, fileName, sheetMap, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void export(HttpServletResponse response,
|
||||||
public static void export(HttpServletResponse response, String fileName, String sheetName,
|
String fileName,
|
||||||
|
String sheetName,
|
||||||
List<List<Object>> sheetDataList) {
|
List<List<Object>> sheetDataList) {
|
||||||
export(response, fileName, sheetName, sheetDataList, null);
|
export(response, fileName, sheetName, sheetDataList, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void export(HttpServletResponse response, String fileName, String sheetName,
|
public static void export(HttpServletResponse response,
|
||||||
List<List<Object>> sheetDataList, Map<Integer, List<String>> selectMap) {
|
String fileName,
|
||||||
|
String sheetName,
|
||||||
|
List<List<Object>> sheetDataList,
|
||||||
|
Map<Integer, List<String>> selectMap) {
|
||||||
|
|
||||||
Map<String, List<List<Object>>> map = new HashMap<>();
|
Map<String, List<List<Object>>> map = new HashMap<>();
|
||||||
map.put(sheetName, sheetDataList);
|
map.put(sheetName, sheetDataList);
|
||||||
@@ -707,12 +739,18 @@ public class ExcelUtils {
|
|||||||
export(response, fileName, sheetDataList);
|
export(response, fileName, sheetDataList);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void export(HttpServletResponse response, String fileName, List<List<Object>> sheetDataList, Map<Integer, List<String>> selectMap) {
|
public static void export(HttpServletResponse response,
|
||||||
|
String fileName,
|
||||||
|
List<List<Object>> sheetDataList,
|
||||||
|
Map<Integer, List<String>> selectMap) {
|
||||||
export(response, fileName, fileName, sheetDataList, selectMap);
|
export(response, fileName, fileName, sheetDataList, selectMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void export(HttpServletResponse response, File file, String fileName,
|
private static void export(HttpServletResponse response,
|
||||||
Map<String, List<List<Object>>> sheetMap, Map<Integer, List<String>> selectMap) {
|
File file,
|
||||||
|
String fileName,
|
||||||
|
Map<String, List<List<Object>>> sheetMap,
|
||||||
|
Map<Integer, List<String>> selectMap) {
|
||||||
// 整个 Excel 表格 book 对象
|
// 整个 Excel 表格 book 对象
|
||||||
SXSSFWorkbook book = new SXSSFWorkbook();
|
SXSSFWorkbook book = new SXSSFWorkbook();
|
||||||
// 每个 Sheet 页
|
// 每个 Sheet 页
|
||||||
@@ -747,7 +785,7 @@ public class ExcelUtils {
|
|||||||
int v = 0;
|
int v = 0;
|
||||||
if (o instanceof URL) {
|
if (o instanceof URL) {
|
||||||
// 如果要导出图片的话, 链接需要传递 URL 对象
|
// 如果要导出图片的话, 链接需要传递 URL 对象
|
||||||
setCellPicture(book, row, patriarch, i, j, (URL) o);
|
setCellPicture(book, row, patriarch, i, j, (URL)o);
|
||||||
} else {
|
} else {
|
||||||
Cell cell = row.createCell(j);
|
Cell cell = row.createCell(j);
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
@@ -902,19 +940,19 @@ public class ExcelUtils {
|
|||||||
// 是否为Boolean
|
// 是否为Boolean
|
||||||
if (o instanceof Boolean) {
|
if (o instanceof Boolean) {
|
||||||
cell.setCellType(CellType.BOOLEAN);
|
cell.setCellType(CellType.BOOLEAN);
|
||||||
cell.setCellValue((Boolean) o);
|
cell.setCellValue((Boolean)o);
|
||||||
return CELL_OTHER;
|
return CELL_OTHER;
|
||||||
}
|
}
|
||||||
// 如果是BigDecimal,则默认3位小数
|
// 如果是BigDecimal,则默认3位小数
|
||||||
if (o instanceof BigDecimal) {
|
if (o instanceof BigDecimal) {
|
||||||
cell.setCellType(CellType.NUMERIC);
|
cell.setCellType(CellType.NUMERIC);
|
||||||
cell.setCellValue(((BigDecimal) o).setScale(3, RoundingMode.HALF_UP).doubleValue());
|
cell.setCellValue(((BigDecimal)o).setScale(3, RoundingMode.HALF_UP).doubleValue());
|
||||||
return CELL_OTHER;
|
return CELL_OTHER;
|
||||||
}
|
}
|
||||||
// 如果是Date数据,则显示格式化数据
|
// 如果是Date数据,则显示格式化数据
|
||||||
if (o instanceof Date) {
|
if (o instanceof Date) {
|
||||||
cell.setCellType(CellType.STRING);
|
cell.setCellType(CellType.STRING);
|
||||||
cell.setCellValue(formatDate((Date) o));
|
cell.setCellValue(formatDate((Date)o));
|
||||||
return CELL_OTHER;
|
return CELL_OTHER;
|
||||||
}
|
}
|
||||||
// 如果是其他,则默认字符串类型
|
// 如果是其他,则默认字符串类型
|
||||||
@@ -925,7 +963,7 @@ public class ExcelUtils {
|
|||||||
|
|
||||||
private static void setCellPicture(SXSSFWorkbook wb, Row sr, Drawing<?> patriarch, int x, int y, URL url) {
|
private static void setCellPicture(SXSSFWorkbook wb, Row sr, Drawing<?> patriarch, int x, int y, URL url) {
|
||||||
// 设置图片宽高
|
// 设置图片宽高
|
||||||
sr.setHeight((short) (IMG_WIDTH * IMG_HEIGHT));
|
sr.setHeight((short)(IMG_WIDTH * IMG_HEIGHT));
|
||||||
// (jdk1.7版本try中定义流可自动关闭)
|
// (jdk1.7版本try中定义流可自动关闭)
|
||||||
try (InputStream is = url.openStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
try (InputStream is = url.openStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||||
byte[] buff = new byte[BYTES_DEFAULT_LENGTH];
|
byte[] buff = new byte[BYTES_DEFAULT_LENGTH];
|
||||||
@@ -984,7 +1022,7 @@ public class ExcelUtils {
|
|||||||
if (Objects.nonNull(str) && "0.0".equals(str)) {
|
if (Objects.nonNull(str) && "0.0".equals(str)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
for (int i = str.length(); --i >= 0; ) {
|
for (int i = str.length(); --i >= 0;) {
|
||||||
if (!Character.isDigit(str.charAt(i))) {
|
if (!Character.isDigit(str.charAt(i))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
@@ -9,15 +9,16 @@
|
|||||||
<version>${revision}</version>
|
<version>${revision}</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>continew-starter-file</artifactId>
|
<artifactId>continew-starter-excel</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
<name>${project.artifactId}</name>
|
<name>${project.artifactId}</name>
|
||||||
<description>ContiNew Starter 文件处理模块</description>
|
<description>ContiNew Starter Excel 文件处理模块</description>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>continew-starter-file-excel</module>
|
<module>continew-starter-excel-core</module>
|
||||||
<module>continew-starter-file-poi</module>
|
<module>continew-starter-excel-fastexcel</module>
|
||||||
|
<module>continew-starter-excel-poi</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
@@ -34,10 +34,10 @@
|
|||||||
<artifactId>continew-starter-data-core</artifactId>
|
<artifactId>continew-starter-data-core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- 文件处理模块 - Excel -->
|
<!-- Excel 文件处理模块 - FastExcel -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>top.continew</groupId>
|
<groupId>top.continew</groupId>
|
||||||
<artifactId>continew-starter-file-excel</artifactId>
|
<artifactId>continew-starter-excel-fastexcel</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Crane4j(基于注解的,用于完成一切 “根据 A 的 key 值拿到 B,再把 B 的属性映射到 A” 这类需求的字段填充框架) -->
|
<!-- Crane4j(基于注解的,用于完成一切 “根据 A 的 key 值拿到 B,再把 B 的属性映射到 A” 这类需求的字段填充框架) -->
|
||||||
|
@@ -44,7 +44,7 @@ import top.continew.starter.extension.crud.model.entity.BaseIdDO;
|
|||||||
import top.continew.starter.extension.crud.model.query.PageQuery;
|
import top.continew.starter.extension.crud.model.query.PageQuery;
|
||||||
import top.continew.starter.extension.crud.model.query.SortQuery;
|
import top.continew.starter.extension.crud.model.query.SortQuery;
|
||||||
import top.continew.starter.extension.crud.model.resp.PageResp;
|
import top.continew.starter.extension.crud.model.resp.PageResp;
|
||||||
import top.continew.starter.file.excel.util.ExcelUtils;
|
import top.continew.starter.excel.util.ExcelUtils;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@@ -51,7 +51,7 @@ import top.continew.starter.extension.crud.model.query.PageQuery;
|
|||||||
import top.continew.starter.extension.crud.model.query.SortQuery;
|
import top.continew.starter.extension.crud.model.query.SortQuery;
|
||||||
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
|
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
|
||||||
import top.continew.starter.extension.crud.model.resp.PageResp;
|
import top.continew.starter.extension.crud.model.resp.PageResp;
|
||||||
import top.continew.starter.file.excel.util.ExcelUtils;
|
import top.continew.starter.excel.util.ExcelUtils;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
@@ -1,35 +0,0 @@
|
|||||||
package top.continew.starter.file.poi.annotation;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author jiang4yu
|
|
||||||
*/
|
|
||||||
@Target(ElementType.FIELD)
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
public @interface ExcelExport {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 字段名称
|
|
||||||
*/
|
|
||||||
String value();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 导出排序先后: 数字越小越靠前(默认按Java类字段顺序导出)
|
|
||||||
*/
|
|
||||||
int sort() default 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 导出映射,格式如:0-未知;1-男;2-女
|
|
||||||
*/
|
|
||||||
String kv() default "";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 导出模板示例值(有值的话,直接取该值,不做映射)
|
|
||||||
*/
|
|
||||||
String example() default "";
|
|
||||||
|
|
||||||
}
|
|
2
pom.xml
2
pom.xml
@@ -49,7 +49,7 @@
|
|||||||
<module>continew-starter-captcha</module>
|
<module>continew-starter-captcha</module>
|
||||||
<module>continew-starter-messaging</module>
|
<module>continew-starter-messaging</module>
|
||||||
<module>continew-starter-log</module>
|
<module>continew-starter-log</module>
|
||||||
<module>continew-starter-file</module>
|
<module>continew-starter-excel</module>
|
||||||
<module>continew-starter-storage</module>
|
<module>continew-starter-storage</module>
|
||||||
<module>continew-starter-license</module>
|
<module>continew-starter-license</module>
|
||||||
<module>continew-starter-extension</module>
|
<module>continew-starter-extension</module>
|
||||||
|
Reference in New Issue
Block a user