1、Excel优化

Former-commit-id: ba5e91b4cfc03fd2e5b59792d815f338561ae1ca
master
王兵 5 years ago
parent fe447605aa
commit a0baf67677

@ -19,7 +19,9 @@ import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import ${basePackage}.frame.excel.annotation.ColumnDescription;
import ${basePackage}.frame.excel.annotation.ColumnList;
import ${basePackage}.frame.excel.annotation.ColumnName;
import ${basePackage}.frame.excel.annotation.Convert;
import ${basePackage}.frame.excel.annotation.Ignore;
import ${basePackage}.frame.excel.annotation.ParentFirst;
import ${basePackage}.frame.excel.annotation.SheetName;
@ -41,6 +43,7 @@ import ${basePackage}.frame.excel.style.DataCellStyle;
import ${basePackage}.frame.excel.style.ErrorCellStyle;
import ${basePackage}.frame.excel.style.HeadCellStyle;
import ${basePackage}.frame.excel.style.RedFont;
import ${basePackage}.frame.excel.style.SuccessCellStyle;
import ${basePackage}.frame.utils.ClassUtil;
import ${basePackage}.frame.utils.FileUtil;
import ${basePackage}.frame.utils.LogUtil;
@ -59,6 +62,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@ -162,8 +166,8 @@ public class WExcel<T> implements Serializable, Cloneable {
}
//获取列类型
if (field.isAnnotationPresent(${basePackage}.frame.excel.annotation.Converter.class)) {
Converter converter = field.getAnnotation(Converter.class);
if (field.isAnnotationPresent(Convert.class)) {
Convert converter = field.getAnnotation(Convert.class);
Class target = converter.target();
try {
WColumn.setConverter((Converter) target.newInstance());
@ -247,7 +251,8 @@ public class WExcel<T> implements Serializable, Cloneable {
row.addErrors(ValidationUtil.validate(t));
// <3层检查>如果没有错误,则可以执行处理器进行业务处理
if (!row.hasError() && processor != null) {
row.addErrors(processor.exec(t));
List<String> exec = processor.exec(t);
row.addErrors(exec != null ? exec : Collections.EMPTY_LIST);
}
}
this.rowList.add(row);
@ -362,7 +367,8 @@ public class WExcel<T> implements Serializable, Cloneable {
row.addErrors(ValidationUtil.validate(t));
// <3层检查>如果没有错误,则可以执行处理器进行业务处理
if (!row.hasError() && processor != null) {
row.addErrors(processor.exec(t));
List<String> exec = processor.exec(t);
row.addErrors(exec != null ? exec : Collections.EMPTY_LIST);
}
} catch (InstantiationException | IllegalAccessException e) {
row.addError("模板对象默认构造函数错误");
@ -666,4 +672,29 @@ public class WExcel<T> implements Serializable, Cloneable {
fileOutputStream.write(getBytes());
fileOutputStream.close();
}
public static void main(String[] args) throws IOException {
Dept dept = new Dept();
dept.setDeptName("AAAAAA");
ArrayList<Dept> depts = new ArrayList<Dept>();
depts.add(dept);
File file = new File("E:\\E.xlsx");
// 导出模板
// new WExcel<Dept>(Dept.class).loadData(depts).toFile(file);
// 检查excel文件是否符合导入
byte[] bytes = FileUtil.readFileToByteArray(new File("E:\\E.xlsx"));
try {
WExcel check = new WExcel<Dept>(Dept.class).loadData(bytes);
check.toFile(new File("E:\\E_err.xlsx"));
System.out.println(check.hasError());
} catch (TemplateNotMatchException e) {
e.printStackTrace();
} catch (ReadErrorException e) {
e.printStackTrace();
}
System.out.println();
}
}

@ -16,6 +16,6 @@ import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Converter {
public @interface Convert {
Class target();
}

@ -41,6 +41,6 @@ public class BooleanConverter implements Converter<Boolean> {
if (var == null) {
return "";
}
return var ? return var ? "是" : "否";
return var ? "是" : "否";
}
}

@ -5,6 +5,7 @@ import java.util.Date;
</#if>
<#if table.getHtml()>
import ${basePackage}.frame.excel.annotation.ColumnDescription;
import ${basePackage}.frame.excel.annotation.ColumnList;
import ${basePackage}.frame.excel.annotation.ColumnName;
import ${basePackage}.frame.excel.annotation.SheetName;
</#if>
@ -30,6 +31,9 @@ public class ${table.getCName()} extends BaseEntity {
<#if table.getHtml()>
@ColumnName("${field.fieldComment?default("")}")
@ColumnDescription("")
<#if field.fieldType.javaType()=="Boolean">
@ColumnList({"是","否"})
</#if>
</#if>
private ${field.fieldType.javaType()} ${field.getFName()};
</#if>

Loading…
Cancel
Save

Powered by TurnKey Linux.