Former-commit-id: 1556c3c2efd11688e7c25601634708064781b376
master
wangbing 5 years ago
parent 042aa18a45
commit 7aa13d85d4

@ -73,7 +73,7 @@ public class DeptAjax {
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response; return response;
} }
return ResponseUtil.apply(new WExcel<>(response.getResult())); return ResponseUtil.apply(new WExcel<>(Dept.class).loadData(response.getResult()));
} }
public Object imports(MultipartFile file) { public Object imports(MultipartFile file) {

@ -73,7 +73,7 @@ public class DictAjax {
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response; return response;
} }
return ResponseUtil.apply(new WExcel<>(response.getResult())); return ResponseUtil.apply(new WExcel<>(Dict.class).loadData(response.getResult()));
} }
public Object imports(MultipartFile file) { public Object imports(MultipartFile file) {

@ -67,7 +67,7 @@ public class FileAjax {
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response; return response;
} }
return ResponseUtil.apply(new WExcel<>(response.getResult())); return ResponseUtil.apply(new WExcel<>(File.class).loadData(response.getResult()));
} }
public Object imports(MultipartFile file) { public Object imports(MultipartFile file) {

@ -73,7 +73,7 @@ public class ResAjax {
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response; return response;
} }
return ResponseUtil.apply(new WExcel<>(response.getResult())); return ResponseUtil.apply(new WExcel<>(Res.class).loadData(response.getResult()));
} }
public Object imports(MultipartFile file) { public Object imports(MultipartFile file) {

@ -67,7 +67,7 @@ public class RoleAjax {
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response; return response;
} }
return ResponseUtil.apply(new WExcel<>(response.getResult())); return ResponseUtil.apply(new WExcel<>(Role.class).loadData(response.getResult()));
} }
public Object imports(MultipartFile file) { public Object imports(MultipartFile file) {

@ -73,7 +73,7 @@ public class TokensAjax {
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response; return response;
} }
return ResponseUtil.apply(new WExcel<>(response.getResult())); return ResponseUtil.apply(new WExcel<>(Tokens.class).loadData(response.getResult()));
} }
public Object imports(MultipartFile file) { public Object imports(MultipartFile file) {

@ -208,7 +208,7 @@ public class UserAjax {
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空"); response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response; return response;
} }
return ResponseUtil.apply(new WExcel<>(response.getResult())); return ResponseUtil.apply(new WExcel<>(User.class).loadData(response.getResult()));
} }
public Object imports(MultipartFile file) { public Object imports(MultipartFile file) {

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

@ -24,6 +24,10 @@ public class WRow extends HashMap<String, WCell> implements Serializable {
errorList.add(errorMsg); errorList.add(errorMsg);
} }
public final void addErrors(List<String> errorMsgs) {
errorList.addAll(errorMsgs);
}
public List<String> getErrorList() { public List<String> getErrorList() {
return errorList; return errorList;
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.