|
|
|
@ -36,6 +36,11 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
|
*/
|
|
|
|
|
private String name;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 表名
|
|
|
|
|
*/
|
|
|
|
|
private boolean freezeFirst;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 表头的集合
|
|
|
|
|
*/
|
|
|
|
@ -60,9 +65,10 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
|
*/
|
|
|
|
|
private List<WColumn> initColumns(Class<?> clazz) {
|
|
|
|
|
//获取工作簿名称,没有则以类名为默认工作簿名称
|
|
|
|
|
SheetName sheetName = clazz.getAnnotation(SheetName.class);
|
|
|
|
|
if (sheetName != null) {
|
|
|
|
|
if (clazz.isAnnotationPresent(SheetName.class)) {
|
|
|
|
|
SheetName sheetName = clazz.getAnnotation(SheetName.class);
|
|
|
|
|
this.setName(sheetName.value());
|
|
|
|
|
this.setFreezeFirst(sheetName.freezeFirst());
|
|
|
|
|
} else {
|
|
|
|
|
this.setName(clazz.getName());
|
|
|
|
|
}
|
|
|
|
@ -83,6 +89,7 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
|
} else {
|
|
|
|
|
ColumnName columnColumnName = field.getAnnotation(ColumnName.class);
|
|
|
|
|
WColumn.setName(columnColumnName.value());
|
|
|
|
|
WColumn.setCellWidth(columnColumnName.width());
|
|
|
|
|
}
|
|
|
|
|
//获取列填写说明或描述
|
|
|
|
|
if (field.isAnnotationPresent(ColumnDescription.class)) {
|
|
|
|
@ -182,8 +189,8 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
|
if (null == value) {
|
|
|
|
|
row.put(column.getName(), new WCell());
|
|
|
|
|
} else {
|
|
|
|
|
column.getConverter().string(value);
|
|
|
|
|
row.put(column.getName(), new WCell());
|
|
|
|
|
String string = column.getConverter().string(value);
|
|
|
|
|
row.put(column.getName(), new WCell(string));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.rowList.add(row);
|
|
|
|
@ -231,8 +238,8 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
|
//第一张Sheet表
|
|
|
|
|
Sheet sheet = workbook.getSheetAt(0);
|
|
|
|
|
//获取Sheet名称
|
|
|
|
|
SheetName sheetName = clazz.getAnnotation(SheetName.class);
|
|
|
|
|
if (sheetName != null) {//如果模板存在注解则使用注解名称
|
|
|
|
|
if (clazz.isAnnotationPresent(SheetName.class)) {//如果模板存在注解则使用注解名称
|
|
|
|
|
SheetName sheetName = clazz.getAnnotation(SheetName.class);
|
|
|
|
|
this.setName(sheetName.value());
|
|
|
|
|
} else {//将类名设为表名,如果类名不存在,将Excel表名设为表名
|
|
|
|
|
this.setName(sheet.getSheetName());
|
|
|
|
@ -296,6 +303,7 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
row.addError("数据检查错误");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -390,6 +398,14 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
|
this.name = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean getFreezeFirst() {
|
|
|
|
|
return freezeFirst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setFreezeFirst(boolean freezeFirst) {
|
|
|
|
|
this.freezeFirst = freezeFirst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// public final String toCSV() {
|
|
|
|
|
// StringBuilder sb = new StringBuilder();
|
|
|
|
@ -460,43 +476,35 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
|
|
|
|
|
|
public XSSFWorkbook getExcel(boolean checkResult) {
|
|
|
|
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
|
|
|
|
|
|
|
|
|
//---------- 创建样式 ------------------
|
|
|
|
|
CellStyle headCellStyle = new HeadCellStyle(workbook).getStyle();
|
|
|
|
|
CellStyle alignRightCellStyle = new AlignRightCellStyle(workbook).getStyle();
|
|
|
|
|
|
|
|
|
|
//----------- 创建字体 ----------------
|
|
|
|
|
Font headFont = new HeadFont(workbook).getFont();
|
|
|
|
|
Font normalFont = new NormalFont(workbook).getFont();
|
|
|
|
|
Font redFont = new RedFont(workbook).getFont();
|
|
|
|
|
|
|
|
|
|
//创建一个Sheet表
|
|
|
|
|
XSSFSheet sheet = workbook.createSheet(name);
|
|
|
|
|
sheet.setDefaultRowHeightInPoints(18);
|
|
|
|
|
Row firstRow = sheet.createRow(0); // 下标为0的行开始
|
|
|
|
|
XSSFDrawing drawing = sheet.createDrawingPatriarch();
|
|
|
|
|
|
|
|
|
|
// 错误表头
|
|
|
|
|
// if (checkResult) {
|
|
|
|
|
// // 检查结果栏
|
|
|
|
|
// Cell firstCell = firstRow.createCell(this.columnList.size());
|
|
|
|
|
// firstCell.setCellStyle(headCellStyle);
|
|
|
|
|
// firstCell.setCellValue(new XSSFRichTextString("检查结果"));
|
|
|
|
|
// sheet.setColumnWidth(0, (4 + 8) * 256);
|
|
|
|
|
// }
|
|
|
|
|
int offset = 0;
|
|
|
|
|
// 添加错误说明列
|
|
|
|
|
if (checkResult && hasError()) {
|
|
|
|
|
offset++;
|
|
|
|
|
Cell firstCell = firstRow.createCell(0);
|
|
|
|
|
firstCell.setCellStyle(new ErrorCellStyle(workbook).getStyle());
|
|
|
|
|
firstCell.setCellValue(new XSSFRichTextString("检查结果说明"));
|
|
|
|
|
sheet.setColumnWidth(0, 18 * 256);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < this.columnList.size(); j++) {
|
|
|
|
|
WColumn column = this.columnList.get(j);
|
|
|
|
|
Field field = column.getField();
|
|
|
|
|
Cell firstCell = firstRow.createCell(j);
|
|
|
|
|
Cell firstCell = firstRow.createCell(j + offset);
|
|
|
|
|
String columnName = column.getName();
|
|
|
|
|
XSSFRichTextString textString;
|
|
|
|
|
if (column.isRequired()) {
|
|
|
|
|
textString = new XSSFRichTextString("*" + columnName);
|
|
|
|
|
textString.applyFont(0, 1, redFont);
|
|
|
|
|
textString.applyFont(1, textString.length(), headFont);
|
|
|
|
|
textString.applyFont(0, 1, new RedFont(workbook).getFont());
|
|
|
|
|
textString.applyFont(1, textString.length(), workbook.createFont());
|
|
|
|
|
} else {
|
|
|
|
|
textString = new XSSFRichTextString(columnName);
|
|
|
|
|
textString.applyFont(headFont);
|
|
|
|
|
textString.applyFont(workbook.createFont());
|
|
|
|
|
}
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
sb.append(column.getDescription()).append("\n");
|
|
|
|
@ -516,48 +524,47 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
|
firstCell.setCellComment(comment);
|
|
|
|
|
}
|
|
|
|
|
firstCell.setCellValue(textString);
|
|
|
|
|
firstCell.setCellStyle(headCellStyle);
|
|
|
|
|
sheet.setColumnWidth(j, (4 + column.getCellWidth()) * 256);
|
|
|
|
|
firstCell.setCellStyle(new HeadCellStyle(workbook).getStyle());
|
|
|
|
|
sheet.setColumnWidth(j + offset, (4 + column.getCellWidth()) * 256);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 错误消息栏
|
|
|
|
|
// if (checkResult) {
|
|
|
|
|
// Cell firstCell = firstRow.createCell(this.columnList.size());
|
|
|
|
|
// firstCell.setCellStyle(headCellStyle);
|
|
|
|
|
// firstCell.setCellValue(new XSSFRichTextString("======================"));
|
|
|
|
|
// sheet.setColumnWidth(this.columnList.size() + 1, (4 + 10) * 256);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// 冻结第一行
|
|
|
|
|
// sheet.createFreezePane(255,1);
|
|
|
|
|
if (freezeFirst) {
|
|
|
|
|
sheet.createFreezePane(255, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 填充数据
|
|
|
|
|
for (int i = 0; i < this.rowList.size(); i++) {
|
|
|
|
|
boolean flag = this.rowList.get(i).;
|
|
|
|
|
boolean flag = this.rowList.get(i).hasError();
|
|
|
|
|
WRow wRow = this.rowList.get(i);
|
|
|
|
|
Row row = sheet.createRow(i + 1);
|
|
|
|
|
|
|
|
|
|
if (flag) {
|
|
|
|
|
List<String> errorList = wRow.getErrorList();
|
|
|
|
|
Cell xssfCell = row.createCell(0);
|
|
|
|
|
xssfCell.setCellStyle(new ErrorCellStyle(workbook).getStyle());
|
|
|
|
|
String join = String.join(";", errorList);
|
|
|
|
|
xssfCell.setCellValue(new XSSFRichTextString(join));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < this.columnList.size(); j++) {
|
|
|
|
|
WColumn column = this.columnList.get(j);
|
|
|
|
|
|
|
|
|
|
Cell xssfCell = row.createCell(j);
|
|
|
|
|
Cell xssfCell = row.createCell(j + offset);
|
|
|
|
|
WCell WCell = this.rowList.get(i).get(column.getName());
|
|
|
|
|
if (null == WCell) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
String value = WCell.getValue();
|
|
|
|
|
List<String> errorList = wRow.getErrorList();
|
|
|
|
|
xssfCell.setCellType(column.getCellType());
|
|
|
|
|
if (column.getCellType() == Cell.CELL_TYPE_NUMERIC || column.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
|
|
|
|
|
xssfCell.setCellStyle(new AlignRightCellStyle(workbook, wRow.hasError()).getStyle());
|
|
|
|
|
xssfCell.setCellStyle(new DataCellStyle(workbook, CellStyle.ALIGN_RIGHT, wRow.hasError()).getStyle());
|
|
|
|
|
} else {
|
|
|
|
|
xssfCell.setCellStyle(new NormalCellStyle(workbook, flag).getStyle());
|
|
|
|
|
xssfCell.setCellStyle(new DataCellStyle(workbook, CellStyle.ALIGN_LEFT, wRow.hasError()).getStyle());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xssfCell.setCellValue(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flag)
|
|
|
|
|
}
|
|
|
|
|
return workbook;
|
|
|
|
|
}
|
|
|
|
@ -569,6 +576,9 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
|
* @return 单元格的值
|
|
|
|
|
*/
|
|
|
|
|
public static String getValue(Cell cell) {
|
|
|
|
|
if (cell == null) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
switch (cell.getCellType()) {
|
|
|
|
|
case Cell.CELL_TYPE_STRING:
|
|
|
|
|
return cell.getStringCellValue();
|
|
|
|
|