|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
package xyz.wbsite.frame.excel;
|
|
|
|
|
package ${basePackage}.frame.excel;
|
|
|
|
|
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
|
@ -13,34 +13,35 @@ import org.apache.poi.xssf.usermodel.XSSFDrawing;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
import xyz.wbsite.frame.excel.annotation.ColumnDescription;
|
|
|
|
|
import xyz.wbsite.frame.excel.annotation.ColumnName;
|
|
|
|
|
import xyz.wbsite.frame.excel.annotation.Ignore;
|
|
|
|
|
import xyz.wbsite.frame.excel.annotation.ParentFirst;
|
|
|
|
|
import xyz.wbsite.frame.excel.annotation.SheetName;
|
|
|
|
|
import xyz.wbsite.frame.excel.converter.BooleanConverter;
|
|
|
|
|
import xyz.wbsite.frame.excel.converter.ByteConverter;
|
|
|
|
|
import xyz.wbsite.frame.excel.converter.CharacterConverter;
|
|
|
|
|
import xyz.wbsite.frame.excel.converter.Converter;
|
|
|
|
|
import xyz.wbsite.frame.excel.converter.DateConverter;
|
|
|
|
|
import xyz.wbsite.frame.excel.converter.DoubleConverter;
|
|
|
|
|
import xyz.wbsite.frame.excel.converter.FloatConverter;
|
|
|
|
|
import xyz.wbsite.frame.excel.converter.IntegerConverter;
|
|
|
|
|
import xyz.wbsite.frame.excel.converter.LongConverter;
|
|
|
|
|
import xyz.wbsite.frame.excel.converter.ShortConverter;
|
|
|
|
|
import xyz.wbsite.frame.excel.converter.StringConverter;
|
|
|
|
|
import xyz.wbsite.frame.excel.exception.ReadErrorException;
|
|
|
|
|
import xyz.wbsite.frame.excel.exception.TemplateNotMatchException;
|
|
|
|
|
import xyz.wbsite.frame.excel.exception.ValueConverterException;
|
|
|
|
|
import xyz.wbsite.frame.excel.style.DataCellStyle;
|
|
|
|
|
import xyz.wbsite.frame.excel.style.ErrorCellStyle;
|
|
|
|
|
import xyz.wbsite.frame.excel.style.HeadCellStyle;
|
|
|
|
|
import xyz.wbsite.frame.excel.style.RedFont;
|
|
|
|
|
import xyz.wbsite.frame.utils.ClassUtil;
|
|
|
|
|
import xyz.wbsite.frame.utils.FileUtil;
|
|
|
|
|
import xyz.wbsite.frame.utils.LogUtil;
|
|
|
|
|
import xyz.wbsite.frame.utils.ValidationUtil;
|
|
|
|
|
import xyz.wbsite.module.system.ent.Dept;
|
|
|
|
|
import ${basePackage}.frame.excel.annotation.ColumnDescription;
|
|
|
|
|
import ${basePackage}.frame.excel.annotation.ColumnName;
|
|
|
|
|
import ${basePackage}.frame.excel.annotation.Ignore;
|
|
|
|
|
import ${basePackage}.frame.excel.annotation.ParentFirst;
|
|
|
|
|
import ${basePackage}.frame.excel.annotation.SheetName;
|
|
|
|
|
import ${basePackage}.frame.excel.converter.BooleanConverter;
|
|
|
|
|
import ${basePackage}.frame.excel.converter.ByteConverter;
|
|
|
|
|
import ${basePackage}.frame.excel.converter.CharacterConverter;
|
|
|
|
|
import ${basePackage}.frame.excel.converter.Converter;
|
|
|
|
|
import ${basePackage}.frame.excel.converter.DateConverter;
|
|
|
|
|
import ${basePackage}.frame.excel.converter.DoubleConverter;
|
|
|
|
|
import ${basePackage}.frame.excel.converter.FloatConverter;
|
|
|
|
|
import ${basePackage}.frame.excel.converter.IntegerConverter;
|
|
|
|
|
import ${basePackage}.frame.excel.converter.LongConverter;
|
|
|
|
|
import ${basePackage}.frame.excel.converter.ShortConverter;
|
|
|
|
|
import ${basePackage}.frame.excel.converter.StringConverter;
|
|
|
|
|
import ${basePackage}.frame.excel.exception.ReadErrorException;
|
|
|
|
|
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
|
|
|
|
|
import ${basePackage}.frame.excel.exception.ValueConverterException;
|
|
|
|
|
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;
|
|
|
|
|
import ${basePackage}.frame.utils.ValidationUtil;
|
|
|
|
|
import ${basePackage}.module.system.ent.Dept;
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
@ -54,7 +55,6 @@ 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;
|
|
|
|
|
|
|
|
|
@ -147,8 +147,8 @@ public class WExcel<T> implements Serializable, Cloneable {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取列类型
|
|
|
|
|
if (field.isAnnotationPresent(xyz.wbsite.frame.excel.annotation.Converter.class)) {
|
|
|
|
|
xyz.wbsite.frame.excel.annotation.Converter converter = field.getAnnotation(xyz.wbsite.frame.excel.annotation.Converter.class);
|
|
|
|
|
if (field.isAnnotationPresent(${basePackage}.frame.excel.annotation.Converter.class)) {
|
|
|
|
|
${basePackage}.frame.excel.annotation.Converter converter = field.getAnnotation(${basePackage}.frame.excel.annotation.Converter.class);
|
|
|
|
|
Class target = converter.target();
|
|
|
|
|
try {
|
|
|
|
|
WColumn.setConverter((Converter) target.newInstance());
|
|
|
|
@ -204,7 +204,7 @@ public class WExcel<T> implements Serializable, Cloneable {
|
|
|
|
|
*
|
|
|
|
|
* @param list 需要导出的对象列表
|
|
|
|
|
*/
|
|
|
|
|
public xyz.wbsite.frame.excel.WExcel loadData(List<T> list) {
|
|
|
|
|
public WExcel loadData(List<T> list, Processor<T> processor) {
|
|
|
|
|
if (list.size() > 0) {
|
|
|
|
|
for (T t : list) {
|
|
|
|
|
WRow row = new WRow();
|
|
|
|
@ -226,6 +226,12 @@ public class WExcel<T> implements Serializable, Cloneable {
|
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
|
LogUtil.w(method.getName() + " unexpected exception!");
|
|
|
|
|
}
|
|
|
|
|
// <2层检查>检查模板注解验证是否通过
|
|
|
|
|
row.addErrors(ValidationUtil.validate(t));
|
|
|
|
|
// <3层检查>如果没有错误,则可以执行处理器进行逻辑处理
|
|
|
|
|
if (!row.hasError() && processor != null) {
|
|
|
|
|
row.addErrors(processor.exec(t));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.rowList.add(row);
|
|
|
|
|
}
|
|
|
|
@ -233,15 +239,15 @@ public class WExcel<T> implements Serializable, Cloneable {
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public xyz.wbsite.frame.excel.WExcel check(byte[] bytes) throws TemplateNotMatchException, ReadErrorException {
|
|
|
|
|
return check(bytes, null);
|
|
|
|
|
public WExcel loadData(byte[] bytes) throws TemplateNotMatchException, ReadErrorException {
|
|
|
|
|
return loadData(bytes, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 以Excel文件的字节数组为数据源,为自己赋值的同时,与指定的模板类进行数据校验。<br/>
|
|
|
|
|
* 如果匹配过程中发现了不符合的数据,会在对应的单元中添加错误信息。<br/>
|
|
|
|
|
*/
|
|
|
|
|
public xyz.wbsite.frame.excel.WExcel check(byte[] bytes, Validator validator) throws TemplateNotMatchException, ReadErrorException {
|
|
|
|
|
public WExcel loadData(byte[] bytes, Processor<T> processor) throws TemplateNotMatchException, ReadErrorException {
|
|
|
|
|
Workbook workbook = null;
|
|
|
|
|
InputStream is = null;
|
|
|
|
|
boolean flag;
|
|
|
|
@ -331,13 +337,13 @@ public class WExcel<T> implements Serializable, Cloneable {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
T t = templateClass.newInstance();
|
|
|
|
|
List<String> errs = transferMap(row, t);
|
|
|
|
|
errs.addAll(ValidationUtil.validate(t));
|
|
|
|
|
errs.addAll(validator != null ? validator.validate(t) : Collections.emptyList());
|
|
|
|
|
if (errs.size() > 0) {
|
|
|
|
|
for (String s : errs) {
|
|
|
|
|
row.addError(s);
|
|
|
|
|
}
|
|
|
|
|
// <1层检查>检查数据格式是否正确
|
|
|
|
|
row.addErrors(transferMap(row, t));
|
|
|
|
|
// <2层检查>检查模板注解验证是否通过
|
|
|
|
|
row.addErrors(ValidationUtil.validate(t));
|
|
|
|
|
// <3层检查>如果没有错误,则可以执行处理器进行逻辑处理
|
|
|
|
|
if (!row.hasError() && processor != null) {
|
|
|
|
|
row.addErrors(processor.exec(t));
|
|
|
|
|
}
|
|
|
|
|
} catch (InstantiationException | IllegalAccessException e) {
|
|
|
|
|
row.addError("模板对象默认构造函数错误");
|
|
|
|
@ -545,11 +551,11 @@ public class WExcel<T> implements Serializable, Cloneable {
|
|
|
|
|
} else {
|
|
|
|
|
// 添加结果
|
|
|
|
|
Cell resultCell = row.createCell(0);
|
|
|
|
|
resultCell.setCellStyle(new xyz.wbsite.frame.excel.style.SuccessCellStyle(workbook).getStyle());
|
|
|
|
|
resultCell.setCellStyle(new SuccessCellStyle(workbook).getStyle());
|
|
|
|
|
resultCell.setCellValue("成功");
|
|
|
|
|
// 添加错误信息详细说明
|
|
|
|
|
Cell errsCell = row.createCell(1);
|
|
|
|
|
errsCell.setCellStyle(new xyz.wbsite.frame.excel.style.SuccessCellStyle(workbook).getStyle());
|
|
|
|
|
errsCell.setCellStyle(new SuccessCellStyle(workbook).getStyle());
|
|
|
|
|
errsCell.setCellValue("");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -604,8 +610,8 @@ public class WExcel<T> implements Serializable, Cloneable {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface Validator<T> {
|
|
|
|
|
List<String> validate(T t);
|
|
|
|
|
public interface Processor<T> {
|
|
|
|
|
List<String> exec(T t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void toFile(File file) throws IOException {
|
|
|
|
@ -634,7 +640,7 @@ public class WExcel<T> implements Serializable, Cloneable {
|
|
|
|
|
// 检查excel文件是否符合导入
|
|
|
|
|
byte[] bytes = FileUtil.readFileToByteArray(new File("E:\\E.xlsx"));
|
|
|
|
|
try {
|
|
|
|
|
xyz.wbsite.frame.excel.WExcel check = new xyz.wbsite.frame.excel.WExcel<Dept>(Dept.class).check(bytes);
|
|
|
|
|
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) {
|
|
|
|
|