提交 038eaa6b 作者: 谭志磊

fix: excel工具类修改错误

上级 ad00c1e6
...@@ -9,8 +9,8 @@ dependencies { ...@@ -9,8 +9,8 @@ dependencies {
implementation "io.swagger:swagger-annotations:${swaggerAnnotationsVersion}" implementation "io.swagger:swagger-annotations:${swaggerAnnotationsVersion}"
implementation "org.hibernate.validator:hibernate-validator:${hibernateValidatorVersion}" implementation "org.hibernate.validator:hibernate-validator:${hibernateValidatorVersion}"
implementation "cn.dev33:sa-token-dao-redis-jackson:${saTokenVersion}" implementation "cn.dev33:sa-token-dao-redis-jackson:${saTokenVersion}"
implementation fileTree(dir: project.rootDir.getPath() + '\\libs', includes: ['*jar']) implementation fileTree(dir: project.rootDir.getPath() + '\\libs', includes: ['*jar'])
} }
dependencies { dependencies {
implementation project(":basic-common:core") implementation project(":basic-common:core")
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-aop' implementation 'org.springframework.boot:spring-boot-starter-aop'
// hutool-extra // hutool-extra
implementation "cn.hutool:hutool-extra:${hutoolVersion}" implementation "cn.hutool:hutool-extra:${hutoolVersion}"
// fastjson // fastjson
implementation "com.alibaba:fastjson:${fastJsonVersion}" implementation "com.alibaba:fastjson:${fastJsonVersion}"
} }
...@@ -15,125 +15,125 @@ import java.math.BigDecimal; ...@@ -15,125 +15,125 @@ import java.math.BigDecimal;
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD) @Target(ElementType.FIELD)
public @interface Excel { public @interface Excel {
/** /**
* 导出时在excel中排序 * 导出时在excel中排序
*/ */
int sort() default Integer.MAX_VALUE; int sort() default Integer.MAX_VALUE;
/** /**
* 导出到Excel中的名字. * 导出到Excel中的名字.
*/ */
String name() default ""; String name() default "";
/** /**
* 日期格式, 如: yyyy-MM-dd * 日期格式, 如: yyyy-MM-dd
*/ */
String dateFormat() default ""; String dateFormat() default "";
/** /**
* 如果是字典类型,请设置字典的type值 (如: user_sex) * 如果是字典类型,请设置字典的type值 (如: user_sex)
*/ */
String dictType() default ""; String dictType() default "";
/** /**
* 读取内容转表达式 (如: 0=男,1=女,2=未知) * 读取内容转表达式 (如: 0=男,1=女,2=未知)
*/ */
String readConverterExp() default ""; String readConverterExp() default "";
/** /**
* 分隔符,读取字符串组内容 * 分隔符,读取字符串组内容
*/ */
String separator() default ","; String separator() default ",";
/** /**
* BigDecimal 精度 默认:-1(默认不开启BigDecimal格式化) * BigDecimal 精度 默认:-1(默认不开启BigDecimal格式化)
*/ */
int scale() default -1; int scale() default -1;
/** /**
* BigDecimal 舍入规则 默认:BigDecimal.ROUND_HALF_EVEN * BigDecimal 舍入规则 默认:BigDecimal.ROUND_HALF_EVEN
*/ */
int roundingMode() default BigDecimal.ROUND_HALF_EVEN; int roundingMode() default BigDecimal.ROUND_HALF_EVEN;
/** /**
* 导出类型(0数字 1字符串) * 导出类型(0数字 1字符串)
*/ */
ColumnType cellType() default ColumnType.STRING; ColumnType cellType() default ColumnType.STRING;
/** /**
* 导出时在excel中每个列的高度 单位为字符 * 导出时在excel中每个列的高度 单位为字符
*/ */
double height() default 14; double height() default 14;
/** /**
* 导出时在excel中每个列的宽 单位为字符 * 导出时在excel中每个列的宽 单位为字符
*/ */
double width() default 16; double width() default 16;
/** /**
* 文字后缀,如% 90 变成90% * 文字后缀,如% 90 变成90%
*/ */
String suffix() default ""; String suffix() default "";
/** /**
* 当值为空时,字段的默认值 * 当值为空时,字段的默认值
*/ */
String defaultValue() default ""; String defaultValue() default "";
/** /**
* 提示信息 * 提示信息
*/ */
String prompt() default ""; String prompt() default "";
/** /**
* 设置只能选择不能输入的列内容. * 设置只能选择不能输入的列内容.
*/ */
String[] combo() default {}; String[] combo() default {};
/** /**
* 是否导出数据,应对需求:有时我们需要导出一份模板,这是标题需要但内容需要用户手工填写. * 是否导出数据,应对需求:有时我们需要导出一份模板,这是标题需要但内容需要用户手工填写.
*/ */
boolean isExport() default true; boolean isExport() default true;
/** /**
* 另一个类中的属性名称,支持多级获取,以小数点隔开 * 另一个类中的属性名称,支持多级获取,以小数点隔开
*/ */
String targetAttr() default ""; String targetAttr() default "";
/** /**
* 是否自动统计数据,在最后追加一行统计数据总和 * 是否自动统计数据,在最后追加一行统计数据总和
*/ */
boolean isStatistics() default false; boolean isStatistics() default false;
/** /**
* 导出字段对齐方式(0:默认;1:靠左;2:居中;3:靠右) * 导出字段对齐方式(0:默认;1:靠左;2:居中;3:靠右)
*/ */
Align align() default Align.AUTO; Align align() default Align.AUTO;
/** /**
* 自定义数据处理器 * 自定义数据处理器
*/ */
Class<?> handler() default ExcelHandlerAdapter.class; Class<?> handler() default ExcelHandlerAdapter.class;
/** /**
* 自定义数据处理器参数 * 自定义数据处理器参数
*/ */
String[] args() default {}; String[] args() default {};
enum Align {
AUTO(0),
LEFT(1),
CENTER(2),
RIGHT(3);
enum Align
{
AUTO(0), LEFT(1), CENTER(2), RIGHT(3);
private final int value; private final int value;
Align(int value) Align(int value) {
{
this.value = value; this.value = value;
} }
public int value() public int value() {
{
return this.value; return this.value;
} }
} }
...@@ -143,34 +143,34 @@ import java.math.BigDecimal; ...@@ -143,34 +143,34 @@ import java.math.BigDecimal;
*/ */
Type type() default Type.ALL; Type type() default Type.ALL;
enum Type enum Type {
{ ALL(0),
ALL(0), EXPORT(1), IMPORT(2); EXPORT(1),
IMPORT(2);
private final int value; private final int value;
Type(int value) Type(int value) {
{
this.value = value; this.value = value;
} }
int value() int value() {
{
return this.value; return this.value;
} }
} }
enum ColumnType enum ColumnType {
{ NUMERIC(0),
NUMERIC(0), STRING(1), IMAGE(2); STRING(1),
IMAGE(2);
private final int value; private final int value;
ColumnType(int value) ColumnType(int value) {
{
this.value = value; this.value = value;
} }
int value() int value() {
{
return this.value; return this.value;
} }
} }
......
/* (C) 2022 YiRing, Inc. */
package com.yiring.common.util.poi; package com.yiring.common.util.poi;
import com.yiring.common.annotation.Excel; import com.yiring.common.annotation.Excel;
import com.yiring.common.annotation.Excel.ColumnType; import com.yiring.common.annotation.Excel.ColumnType;
import com.yiring.common.annotation.Excel.Type; import com.yiring.common.annotation.Excel.Type;
...@@ -12,18 +12,6 @@ import com.yiring.common.util.date.DateUtils; ...@@ -12,18 +12,6 @@ import com.yiring.common.util.date.DateUtils;
import com.yiring.common.util.file.FileTypeUtils; import com.yiring.common.util.file.FileTypeUtils;
import com.yiring.common.util.file.ImageUtils; import com.yiring.common.util.file.ImageUtils;
import com.yiring.common.util.reflect.ReflectUtils; import com.yiring.common.util.reflect.ReflectUtils;
import org.apache.commons.lang3.RegExUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
...@@ -35,6 +23,17 @@ import java.time.LocalDate; ...@@ -35,6 +23,17 @@ import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.RegExUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Excel相关处理 * Excel相关处理
...@@ -47,7 +46,7 @@ public class ExcelUtils<T> { ...@@ -47,7 +46,7 @@ public class ExcelUtils<T> {
public static final String FORMULA_REGEX_STR = "=|-|\\+|@"; public static final String FORMULA_REGEX_STR = "=|-|\\+|@";
public static final String[] FORMULA_STR = {"=", "-", "+", "@"}; public static final String[] FORMULA_STR = { "=", "-", "+", "@" };
/** /**
* Excel sheet最大行数,默认65536 * Excel sheet最大行数,默认65536
...@@ -255,7 +254,7 @@ public class ExcelUtils<T> { ...@@ -255,7 +254,7 @@ public class ExcelUtils<T> {
} }
} else if ( } else if (
(Integer.TYPE == fieldType || Integer.class == fieldType) && (Integer.TYPE == fieldType || Integer.class == fieldType) &&
StrUtils.isNumeric(Convert.toStr(val)) StrUtils.isNumeric(Convert.toStr(val))
) { ) {
val = Convert.toInt(val); val = Convert.toInt(val);
} else if ( } else if (
...@@ -363,7 +362,7 @@ public class ExcelUtils<T> { ...@@ -363,7 +362,7 @@ public class ExcelUtils<T> {
public void exportExcel(HttpServletResponse response) { public void exportExcel(HttpServletResponse response) {
try { try {
writeSheet(); writeSheet();
// wb.write(response.getOutputStream()); // wb.write(response.getOutputStream());
FileOutputStream fos = new FileOutputStream("D:/a.xls"); FileOutputStream fos = new FileOutputStream("D:/a.xls");
wb.write(fos); wb.write(fos);
fos.close(); fos.close();
...@@ -766,7 +765,7 @@ public class ExcelUtils<T> { ...@@ -766,7 +765,7 @@ public class ExcelUtils<T> {
public String dataFormatHandlerAdapter(Object value, Excel excel) { public String dataFormatHandlerAdapter(Object value, Excel excel) {
try { try {
Object instance = excel.handler().newInstance(); Object instance = excel.handler().newInstance();
Method formatMethod = excel.handler().getMethod("format", new Class[]{Object.class, String[].class}); Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class });
value = formatMethod.invoke(instance, value, excel.args()); value = formatMethod.invoke(instance, value, excel.args());
} catch (Exception e) { } catch (Exception e) {
log.error("不能格式化数据 " + excel.handler(), e.getMessage()); log.error("不能格式化数据 " + excel.handler(), e.getMessage());
...@@ -785,8 +784,7 @@ public class ExcelUtils<T> { ...@@ -785,8 +784,7 @@ public class ExcelUtils<T> {
} }
try { try {
temp = Double.valueOf(text); temp = Double.valueOf(text);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {}
}
statistics.put(index, statistics.get(index) + temp); statistics.put(index, statistics.get(index) + temp);
} }
} }
...@@ -880,7 +878,7 @@ public class ExcelUtils<T> { ...@@ -880,7 +878,7 @@ public class ExcelUtils<T> {
Excel attr = field.getAnnotation(Excel.class); Excel attr = field.getAnnotation(Excel.class);
if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) { if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) {
field.setAccessible(true); field.setAccessible(true);
fields.add(new Object[]{field, attr}); fields.add(new Object[] { field, attr });
} }
} }
...@@ -891,7 +889,7 @@ public class ExcelUtils<T> { ...@@ -891,7 +889,7 @@ public class ExcelUtils<T> {
for (Excel attr : excels) { for (Excel attr : excels) {
if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) { if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) {
field.setAccessible(true); field.setAccessible(true);
fields.add(new Object[]{field, attr}); fields.add(new Object[] { field, attr });
} }
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论