|
|
@ -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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|