|
|
|
@ -2,6 +2,9 @@ package ${basePackage}.frame.excel;
|
|
|
|
|
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
|
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
|
|
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
|
|
|
|
|
import org.apache.poi.openxml4j.opc.OPCPackage;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
|
|
|
import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
|
|
import org.apache.poi.ss.usermodel.DataValidation;
|
|
|
|
@ -10,6 +13,12 @@ import org.apache.poi.ss.usermodel.DataValidationHelper;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
|
|
import org.apache.poi.ss.util.NumberToTextConverter;
|
|
|
|
|
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
|
|
|
|
|
import org.apache.poi.xssf.eventusermodel.XSSFReader;
|
|
|
|
|
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
|
|
|
|
|
import org.apache.poi.xssf.model.StylesTable;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFComment;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
|
|
|
|
@ -17,8 +26,10 @@ 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 org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
|
|
import org.apache.poi.ss.util.NumberToTextConverter;
|
|
|
|
|
import org.xml.sax.InputSource;
|
|
|
|
|
import org.xml.sax.SAXException;
|
|
|
|
|
import org.xml.sax.XMLReader;
|
|
|
|
|
import org.xml.sax.helpers.XMLReaderFactory;
|
|
|
|
|
import ${basePackage}.frame.excel.annotation.ColumnDescription;
|
|
|
|
|
import ${basePackage}.frame.excel.annotation.ColumnList;
|
|
|
|
|
import ${basePackage}.frame.excel.annotation.ColumnName;
|
|
|
|
@ -48,7 +59,9 @@ 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.MapperUtil;
|
|
|
|
|
import ${basePackage}.frame.utils.ValidationUtil;
|
|
|
|
|
import ${basePackage}.module.system.ent.Visitor;
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
@ -60,6 +73,7 @@ import java.io.Serializable;
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.net.URI;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
@ -68,10 +82,10 @@ import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* WExcel - 抽象的Excel对象数据集合
|
|
|
|
|
*
|
|
|
|
|
* <p>
|
|
|
|
|
* 导出
|
|
|
|
|
* new WExcel<Dept>(Dept.class).loadData(depts).toFile(file);<br>
|
|
|
|
|
*
|
|
|
|
|
* <p>
|
|
|
|
|
* 导入
|
|
|
|
|
* byte[] bytes = FileUtil.readFileToByteArray(new File("E:\\E.xlsx"));<br>
|
|
|
|
|
* WExcel check = new WExcel<Dept>(Dept.class).loadData(bytes);
|
|
|
|
@ -385,6 +399,99 @@ public class WExcel<T> implements Serializable, Cloneable {
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WExcel readData(byte[] bytes, Processor<T> processor) throws ReadErrorException {
|
|
|
|
|
try {
|
|
|
|
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
|
|
|
|
|
// 1.根据excel报表获取OPCpackage
|
|
|
|
|
OPCPackage opcPackage = OPCPackage.open(inputStream);
|
|
|
|
|
// 2.创建XSSFReader
|
|
|
|
|
XSSFReader xssfReader = new XSSFReader(opcPackage);
|
|
|
|
|
// 3.获取SharedStringTable对象
|
|
|
|
|
ReadOnlySharedStringsTable readOnlySharedStringsTable = new ReadOnlySharedStringsTable(opcPackage);
|
|
|
|
|
// 4.获取styleTable对象
|
|
|
|
|
StylesTable stylesTable = xssfReader.getStylesTable();
|
|
|
|
|
// 5.创建sax xmlReader对象
|
|
|
|
|
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
|
|
|
|
|
// 6.注册事件驱动处理器
|
|
|
|
|
XSSFSheetXMLHandler xssfSheetXMLHandler = new XSSFSheetXMLHandler(stylesTable, readOnlySharedStringsTable, new XSSFSheetXMLHandler.SheetContentsHandler() {
|
|
|
|
|
int current;
|
|
|
|
|
List<String> cells = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void startRow(int i) {
|
|
|
|
|
current = i;
|
|
|
|
|
cells.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void endRow() {
|
|
|
|
|
if (current == 0) {
|
|
|
|
|
if (columnList.size() != cells.size()) {
|
|
|
|
|
throw new RuntimeException("与模板列数量不同。");
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < columnList.size(); i++) {
|
|
|
|
|
WColumn wColumn = columnList.get(i);
|
|
|
|
|
String headValue = cells.get(i);
|
|
|
|
|
headValue = headValue.replace("*", "");
|
|
|
|
|
headValue = headValue.replace(" ", "");
|
|
|
|
|
if (!wColumn.getName().equals(headValue)) {
|
|
|
|
|
throw new RuntimeException("第" + (i + 1) + "项,不匹配的列名," + wColumn.getName() + "和" + headValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
WRow row = new WRow();
|
|
|
|
|
rowList.add(row);
|
|
|
|
|
for (int j = 0; j < columnList.size(); j++) {
|
|
|
|
|
WColumn wcolumn = columnList.get(j);
|
|
|
|
|
WCell WCell = new WCell();
|
|
|
|
|
row.put(wcolumn.getName(), WCell);
|
|
|
|
|
|
|
|
|
|
String value = cells.get(j);
|
|
|
|
|
WCell.setValue(value.trim());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
T t = templateClass.newInstance();
|
|
|
|
|
// <1层检查>检查数据格式是否正确
|
|
|
|
|
row.addErrors(transferMap(row, t));
|
|
|
|
|
// <2层检查>检查模板注解验证是否通过
|
|
|
|
|
row.addErrors(ValidationUtil.validate(t));
|
|
|
|
|
// <3层检查>如果没有错误,则可以执行处理器进行业务处理
|
|
|
|
|
if (!row.hasError() && processor != null) {
|
|
|
|
|
List<String> exec = processor.exec(t);
|
|
|
|
|
row.addErrors(exec != null ? exec : Collections.EMPTY_LIST);
|
|
|
|
|
}
|
|
|
|
|
} catch (InstantiationException | IllegalAccessException e) {
|
|
|
|
|
row.addError("模板对象默认构造函数错误");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void cell(String s, String s1) {
|
|
|
|
|
cells.add(s1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void headerFooter(String s, boolean b, String s1) {
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
}, false);
|
|
|
|
|
xmlReader.setContentHandler(xssfSheetXMLHandler);
|
|
|
|
|
// 7.逐行读取
|
|
|
|
|
XSSFReader.SheetIterator sheetIterator = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
|
|
|
|
|
while (sheetIterator.hasNext()) {
|
|
|
|
|
InputStream in = sheetIterator.next();
|
|
|
|
|
InputSource is = new InputSource(in);
|
|
|
|
|
xmlReader.parse(is);
|
|
|
|
|
}
|
|
|
|
|
} catch (OpenXML4JException | SAXException | IOException e) {
|
|
|
|
|
throw new ReadErrorException("读取Excel文件错误");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 转换某行为一个对象
|
|
|
|
|
*
|
|
|
|
@ -559,8 +666,8 @@ public class WExcel<T> implements Serializable, Cloneable {
|
|
|
|
|
firstCell.setCellStyle(new HeadCellStyle(workbook).getStyle());
|
|
|
|
|
sheet.setColumnWidth(j + offset, (4 + column.getCellWidth()) * 256);
|
|
|
|
|
|
|
|
|
|
if (column.getCellList() != null){
|
|
|
|
|
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 65535, j+offset, j+offset);
|
|
|
|
|
if (column.getCellList() != null) {
|
|
|
|
|
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 65535, j + offset, j + offset);
|
|
|
|
|
DataValidationHelper helper = sheet.getDataValidationHelper();
|
|
|
|
|
DataValidationConstraint constraint = helper.createExplicitListConstraint(column.getCellList());
|
|
|
|
|
DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
|
|
|
|
|