1、Excel优化

Former-commit-id: 63f7c80d0a641e57e244f20c496690532176ba1e
master
王兵 5 years ago
parent b96ef902ca
commit fe447605aa

@ -2,7 +2,6 @@ package ${basePackage}.frame.excel;
import ${basePackage}.frame.excel.converter.Converter; import ${basePackage}.frame.excel.converter.Converter;
import ${basePackage}.frame.utils.StringUtil;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -52,6 +51,10 @@ public class WColumn implements Serializable {
* ExcelType * ExcelType
*/ */
private int cellType = 1; private int cellType = 1;
/**
*
*/
private String[] cellList;
public WColumn() { public WColumn() {
this.name = ""; this.name = "";
@ -132,4 +135,12 @@ public class WColumn implements Serializable {
public void setGetMethod(Method getMethod) { public void setGetMethod(Method getMethod) {
this.getMethod = getMethod; this.getMethod = getMethod;
} }
public String[] getCellList() {
return cellList;
}
public void setCellList(String[] cellList) {
this.cellList = cellList;
}
} }

@ -4,11 +4,16 @@ import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFComment; import org.apache.poi.xssf.usermodel.XSSFComment;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.apache.poi.xssf.usermodel.XSSFDrawing; 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;
@ -36,7 +41,6 @@ import ${basePackage}.frame.excel.style.DataCellStyle;
import ${basePackage}.frame.excel.style.ErrorCellStyle; import ${basePackage}.frame.excel.style.ErrorCellStyle;
import ${basePackage}.frame.excel.style.HeadCellStyle; import ${basePackage}.frame.excel.style.HeadCellStyle;
import ${basePackage}.frame.excel.style.RedFont; import ${basePackage}.frame.excel.style.RedFont;
import ${basePackage}.frame.excel.style.SuccessCellStyle;
import ${basePackage}.frame.utils.ClassUtil; import ${basePackage}.frame.utils.ClassUtil;
import ${basePackage}.frame.utils.FileUtil; import ${basePackage}.frame.utils.FileUtil;
import ${basePackage}.frame.utils.LogUtil; import ${basePackage}.frame.utils.LogUtil;
@ -59,7 +63,14 @@ import java.util.Date;
import java.util.List; import java.util.List;
/** /**
* WExcel - Excel-Sheet * WExcel - Excel
*
*
* new WExcel<Dept>(Dept.class).loadData(depts).toFile(file);<br>
*
*
* byte[] bytes = FileUtil.readFileToByteArray(new File("E:\\E.xlsx"));<br>
* WExcel check = new WExcel<Dept>(Dept.class).loadData(bytes);
* *
* @author wangbing * @author wangbing
* @version 0.0.1 * @version 0.0.1
@ -85,7 +96,7 @@ public class WExcel<T> implements Serializable, Cloneable {
/** /**
* *
*/ */
private List<WColumn> columnList = new ArrayList<>(); private List<${basePackage}.frame.excel.WColumn> columnList = new ArrayList<>();
/** /**
* *
@ -105,7 +116,7 @@ public class WExcel<T> implements Serializable, Cloneable {
* *
* @return WColumn * @return WColumn
*/ */
private List<WColumn> initColumns(Class<?> clazz) { private List<${basePackage}.frame.excel.WColumn> initColumns(Class<?> clazz) {
//获取工作簿名称,没有则以类名为默认工作簿名称 //获取工作簿名称,没有则以类名为默认工作簿名称
if (clazz.isAnnotationPresent(SheetName.class)) { if (clazz.isAnnotationPresent(SheetName.class)) {
SheetName sheetName = clazz.getAnnotation(SheetName.class); SheetName sheetName = clazz.getAnnotation(SheetName.class);
@ -123,7 +134,7 @@ public class WExcel<T> implements Serializable, Cloneable {
continue; continue;
} }
WColumn WColumn = new WColumn(); ${basePackage}.frame.excel.WColumn WColumn = new ${basePackage}.frame.excel.WColumn();
WColumn.setField(field); WColumn.setField(field);
Method set = ClassUtil.setMethod(field.getName(), clazz, field.getType()); Method set = ClassUtil.setMethod(field.getName(), clazz, field.getType());
@ -145,16 +156,18 @@ public class WExcel<T> implements Serializable, Cloneable {
if (field.isAnnotationPresent(ColumnDescription.class)) { if (field.isAnnotationPresent(ColumnDescription.class)) {
WColumn.setDescription(field.getAnnotation(ColumnDescription.class).value()); WColumn.setDescription(field.getAnnotation(ColumnDescription.class).value());
} }
//获取下拉列表
if (field.isAnnotationPresent(${basePackage}.frame.excel.annotation.ColumnList.class)) {
WColumn.setCellList(field.getAnnotation(ColumnList.class).value());
}
//获取列类型 //获取列类型
if (field.isAnnotationPresent(${basePackage}.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); Converter converter = field.getAnnotation(Converter.class);
Class target = converter.target(); Class target = converter.target();
try { try {
WColumn.setConverter((Converter) target.newInstance()); WColumn.setConverter((Converter) target.newInstance());
} catch (InstantiationException e) { } catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
} else { } else {
@ -212,7 +225,7 @@ public class WExcel<T> implements Serializable, Cloneable {
if (list.size() > 0) { if (list.size() > 0) {
for (T t : list) { for (T t : list) {
WRow row = new WRow(); WRow row = new WRow();
for (WColumn column : columnList) { for (${basePackage}.frame.excel.WColumn column : columnList) {
if (column == null) { if (column == null) {
continue; continue;
} }
@ -232,7 +245,7 @@ public class WExcel<T> implements Serializable, Cloneable {
} }
// <2层检查>检查模板注解验证是否通过 // <2层检查>检查模板注解验证是否通过
row.addErrors(ValidationUtil.validate(t)); row.addErrors(ValidationUtil.validate(t));
// <3层检查>如果没有错误,则可以执行处理器进行逻辑处理 // <3层检查>如果没有错误,则可以执行处理器进行业务处理
if (!row.hasError() && processor != null) { if (!row.hasError() && processor != null) {
row.addErrors(processor.exec(t)); row.addErrors(processor.exec(t));
} }
@ -313,10 +326,12 @@ public class WExcel<T> implements Serializable, Cloneable {
} }
} }
int maxRowNumber = sheet.getLastRowNum(); //Excel文件的总行数 //Excel文件的总行数
int maxRowNumber = sheet.getLastRowNum();
// 逐行读取导入文件的数据 // 逐行读取导入文件的数据
for (int i = 0; i < maxRowNumber; i++) { for (int i = 0; i < maxRowNumber; i++) {
Row inputRow = sheet.getRow(i + 1); //Excel中的一行数据第0行为表头所以要加1 //Excel中的一行数据第0行为表头所以要加1
Row inputRow = sheet.getRow(i + 1);
WRow row = new WRow(); WRow row = new WRow();
rowList.add(row); rowList.add(row);
@ -345,7 +360,7 @@ public class WExcel<T> implements Serializable, Cloneable {
row.addErrors(transferMap(row, t)); row.addErrors(transferMap(row, t));
// <2层检查>检查模板注解验证是否通过 // <2层检查>检查模板注解验证是否通过
row.addErrors(ValidationUtil.validate(t)); row.addErrors(ValidationUtil.validate(t));
// <3层检查>如果没有错误,则可以执行处理器进行逻辑处理 // <3层检查>如果没有错误,则可以执行处理器进行业务处理
if (!row.hasError() && processor != null) { if (!row.hasError() && processor != null) {
row.addErrors(processor.exec(t)); row.addErrors(processor.exec(t));
} }
@ -529,6 +544,25 @@ public class WExcel<T> implements Serializable, Cloneable {
firstCell.setCellValue(textString); firstCell.setCellValue(textString);
firstCell.setCellStyle(new HeadCellStyle(workbook).getStyle()); firstCell.setCellStyle(new HeadCellStyle(workbook).getStyle());
sheet.setColumnWidth(j + offset, (4 + column.getCellWidth()) * 256); sheet.setColumnWidth(j + offset, (4 + column.getCellWidth()) * 256);
if (column.getCellList() != null){
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 65535, j, j);
DataValidationHelper helper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = helper.createExplicitListConstraint(column.getCellList());
DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
//处理Excel兼容性问题
if (dataValidation instanceof XSSFDataValidation) {
dataValidation.setSuppressDropDownArrow(true);
dataValidation.setShowErrorBox(true);
} else {
dataValidation.setSuppressDropDownArrow(false);
}
dataValidation.setEmptyCellAllowed(true);
dataValidation.setShowPromptBox(true);
dataValidation.createPromptBox("提示", "只能选择下拉框里面的数据");
sheet.addValidationData(dataValidation);
}
} }
// 冻结第一行 // 冻结第一行
@ -541,7 +575,7 @@ public class WExcel<T> implements Serializable, Cloneable {
WRow wRow = this.rowList.get(i); WRow wRow = this.rowList.get(i);
Row row = sheet.createRow(i + 1); Row row = sheet.createRow(i + 1);
if(offset> 0) { if (offset > 0) {
if (this.rowList.get(i).hasError()) { if (this.rowList.get(i).hasError()) {
// 添加结果 // 添加结果
List<String> errorList = wRow.getErrorList(); List<String> errorList = wRow.getErrorList();
@ -632,29 +666,4 @@ public class WExcel<T> implements Serializable, Cloneable {
fileOutputStream.write(getBytes()); fileOutputStream.write(getBytes());
fileOutputStream.close(); 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();
}
} }

@ -1,6 +1,10 @@
package ${basePackage}.frame.excel.annotation; package ${basePackage}.frame.excel.annotation;
import java.lang.annotation.*; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** /**
* ColumnDescription - Excel * ColumnDescription - Excel

@ -0,0 +1,25 @@
package ${basePackage}.frame.excel.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* ColumnList -
*
* @author wangbing
* @version 0.0.1
* @since 2017-01-01
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ColumnList {
/**
*
*/
String[] value();
}

@ -1,6 +1,10 @@
package ${basePackage}.frame.excel.annotation; package ${basePackage}.frame.excel.annotation;
import java.lang.annotation.*; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** /**
* ColumnName - ExcelExcel使 * ColumnName - ExcelExcel使

@ -1,6 +1,10 @@
package ${basePackage}.frame.excel.annotation; package ${basePackage}.frame.excel.annotation;
import java.lang.annotation.*; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** /**
* Converter - Excel * Converter - Excel

@ -1,6 +1,10 @@
package ${basePackage}.frame.excel.annotation; package ${basePackage}.frame.excel.annotation;
import java.lang.annotation.*; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** /**
* Ignore - Excel * Ignore - Excel

@ -1,6 +1,10 @@
package ${basePackage}.frame.excel.annotation; package ${basePackage}.frame.excel.annotation;
import java.lang.annotation.*; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** /**
* ParentFirst - * ParentFirst -

@ -1,6 +1,10 @@
package ${basePackage}.frame.excel.annotation; package ${basePackage}.frame.excel.annotation;
import java.lang.annotation.*; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** /**
* SheetName - Excel使Class * SheetName - Excel使Class

@ -1,6 +1,6 @@
package ${basePackage}.frame.excel.converter; package ${basePackage}.frame.excel.converter;
import ${basePackage}.frame.excel.exception.ValueConverterException; import ${basePackage}.frame.excel.exception.ValueConverterException;
/** /**
* BooleanConverter - BooleanStringString * BooleanConverter - BooleanStringString
@ -41,6 +41,6 @@ public class BooleanConverter implements Converter<Boolean> {
if (var == null) { if (var == null) {
return ""; return "";
} }
return var ? "Y" : "N"; return var ? return var ? "是" : "否";
} }
} }

@ -1,4 +1,4 @@
package xyz.wbsite.frame.excel.style; package ${basePackage}.frame.excel.style;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;

Loading…
Cancel
Save

Powered by TurnKey Linux.