diff --git a/src/main/java/com/wb/excel/api/WSheet.java b/src/main/java/com/wb/excel/api/WSheet.java index f058cb4..5ddff8c 100644 --- a/src/main/java/com/wb/excel/api/WSheet.java +++ b/src/main/java/com/wb/excel/api/WSheet.java @@ -36,6 +36,11 @@ public class WSheet implements Serializable, Cloneable { */ private String name; + /** + * 表名 + */ + private boolean freezeFirst; + /** * 表头的集合 */ @@ -60,9 +65,10 @@ public class WSheet implements Serializable, Cloneable { */ private List 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 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 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 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 implements Serializable, Cloneable { } } } catch (Exception e) { + e.printStackTrace(); row.addError("数据检查错误"); } } @@ -390,6 +398,14 @@ public class WSheet 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 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 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 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 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 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(); diff --git a/src/main/java/com/wb/excel/api/annotation/ColumnName.java b/src/main/java/com/wb/excel/api/annotation/ColumnName.java index a082685..d0d54aa 100644 --- a/src/main/java/com/wb/excel/api/annotation/ColumnName.java +++ b/src/main/java/com/wb/excel/api/annotation/ColumnName.java @@ -20,7 +20,7 @@ public @interface ColumnName { * * @return */ - String orderNum() default "0"; + int width() default 8; /** * 日期格式化 diff --git a/src/main/java/com/wb/excel/api/annotation/SheetName.java b/src/main/java/com/wb/excel/api/annotation/SheetName.java index c113196..959fe6f 100644 --- a/src/main/java/com/wb/excel/api/annotation/SheetName.java +++ b/src/main/java/com/wb/excel/api/annotation/SheetName.java @@ -14,4 +14,6 @@ import java.lang.annotation.*; @Documented public @interface SheetName { String value(); + + boolean freezeFirst() default false; } diff --git a/src/main/java/com/wb/excel/api/interfaces/EnumSupport.java b/src/main/java/com/wb/excel/api/interfaces/EnumSupport.java deleted file mode 100644 index 125fa06..0000000 --- a/src/main/java/com/wb/excel/api/interfaces/EnumSupport.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.wb.excel.api.interfaces; - -/** - * 自定义枚举的基础类。 - * Created on 2014/9/27. - * - * @author - * @version 0.1.0 - */ -@Deprecated -public interface EnumSupport { - /** - * 每个实现类自身都要实现检查方法。
- * 用于判断输入的值是否属于枚举内容。 - * - * @param str 要判断的值 - * @return true || false / 符合||不符合 - */ - Boolean check(String str); - - /** - * 通过Value来获取Key。
- * 例如性别: 输入男、Male、male都返回F(F为数据库中存储的值) - * - * @param value - * @return Key。最终存储至数据库中的值。 - */ - Object getKey(String value); - - /** - * 通过Key来获取Value
- * 例如性别:输入F,返回男 - * - * @param key - * @return Value。最终要展现给用户看的值。 - */ - String getValue(Object key); -} diff --git a/src/main/java/com/wb/excel/api/interfaces/IExcelVerifyHandler.java b/src/main/java/com/wb/excel/api/interfaces/IExcelVerifyHandler.java deleted file mode 100644 index 8e89396..0000000 --- a/src/main/java/com/wb/excel/api/interfaces/IExcelVerifyHandler.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.wb.excel.api.interfaces; - -import com.wb.excel.api.entity.DataVerifyResult; - -/** - * 导入校验接口 - */ -public interface IExcelVerifyHandler { - -// /** -// * 获取需要处理的字段,导入和导出统一处理了, 减少书写的字段 -// * -// * @return -// */ -// public String[] getNeedVerifyFields(); -// -// /** -// * 获取需要处理的字段,导入和导出统一处理了, 减少书写的字段 -// * -// * @return -// */ -// public void setNeedVerifyFields(String[] arr); - - /** - * 数据校验 - * - * @param obj 当前对象 - * @param name 当前字段名称 - * @param value 当前值 - * @return - */ - DataVerifyResult verifyHandler(Object obj, String name, Object value); - -} diff --git a/src/main/java/com/wb/excel/api/style/AlignRightCellStyle.java b/src/main/java/com/wb/excel/api/style/AlignRightCellStyle.java deleted file mode 100644 index 2555c43..0000000 --- a/src/main/java/com/wb/excel/api/style/AlignRightCellStyle.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.wb.excel.api.style; - -import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.Font; -import org.apache.poi.ss.usermodel.Workbook; - -/** - * Created on 2015/1/29. - * - * @author - * @since 2.0.0 - */ -public class AlignRightCellStyle extends NormalCellStyle { - public AlignRightCellStyle(Workbook workbook) { - this(workbook, false); - } - - public AlignRightCellStyle(Workbook workbook, boolean error) { - super(workbook, error); - style.setAlignment(CellStyle.ALIGN_RIGHT); - Font font = new BaseFont(workbook).getFont(); - style.setFont(font); - } -} diff --git a/src/main/java/com/wb/excel/api/style/BaseCellStyle.java b/src/main/java/com/wb/excel/api/style/BaseCellStyle.java index b18360f..7529073 100644 --- a/src/main/java/com/wb/excel/api/style/BaseCellStyle.java +++ b/src/main/java/com/wb/excel/api/style/BaseCellStyle.java @@ -2,6 +2,7 @@ package com.wb.excel.api.style; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Workbook; /** @@ -17,16 +18,34 @@ public class BaseCellStyle { protected CellStyle style; public BaseCellStyle(Workbook workbook) { - this(workbook, false); - } - - public BaseCellStyle(Workbook workbook, boolean error) { style = workbook.createCellStyle(); - style.setFillPattern(CellStyle.NO_FILL); //单元格不填充 - if (error) { - style.setFillForegroundColor(HSSFColor.RED.index); //背景颜色红色 - style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置单元格填充样式 - } + style.setFillPattern(CellStyle.NO_FILL); + //下边框 + style.setBorderBottom(CellStyle.SOLID_FOREGROUND); + //下边框颜色 + style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); + //左边框 + style.setBorderLeft(CellStyle.SOLID_FOREGROUND); + //左边框颜色 + style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); + //右边框 + style.setBorderRight(CellStyle.SOLID_FOREGROUND); + //右边框颜色 + style.setRightBorderColor(IndexedColors.BLACK.getIndex()); + //上边框 + style.setBorderTop(CellStyle.SOLID_FOREGROUND); + //上边框颜色 + style.setTopBorderColor(IndexedColors.BLACK.getIndex()); + + style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //上下居中 + style.setBorderBottom(CellStyle.SOLID_FOREGROUND); //下边框 + style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //下边框颜色 + style.setBorderLeft(CellStyle.SOLID_FOREGROUND); //左边框 + style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); //左边框颜色 + style.setBorderRight(CellStyle.SOLID_FOREGROUND); //右边框 + style.setRightBorderColor(IndexedColors.BLACK.getIndex()); //右边框颜色 + style.setBorderTop(CellStyle.SOLID_FOREGROUND); //上边框 + style.setTopBorderColor(IndexedColors.BLACK.getIndex()); //上边框颜色 } public CellStyle getStyle() { diff --git a/src/main/java/com/wb/excel/api/style/DataCellStyle.java b/src/main/java/com/wb/excel/api/style/DataCellStyle.java new file mode 100644 index 0000000..f0fcccf --- /dev/null +++ b/src/main/java/com/wb/excel/api/style/DataCellStyle.java @@ -0,0 +1,29 @@ +package com.wb.excel.api.style; + +import org.apache.poi.hssf.util.HSSFColor; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Workbook; + +public class DataCellStyle extends BaseCellStyle { + + public DataCellStyle(Workbook workbook, short align, boolean error) { + super(workbook); + style.setAlignment(align); + if (error) { + style.setFillForegroundColor(HSSFColor.RED.index); //背景颜色红色 + style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置单元格填充样式 + } + } + + public DataCellStyle(Workbook workbook, short align) { + this(workbook, align, false); + } + + public DataCellStyle(Workbook workbook, boolean error) { + this(workbook, CellStyle.ALIGN_LEFT, error); + } + + public DataCellStyle(Workbook workbook) { + this(workbook, CellStyle.ALIGN_LEFT, false); + } +} diff --git a/src/main/java/com/wb/excel/api/style/ErrorCellStyle.java b/src/main/java/com/wb/excel/api/style/ErrorCellStyle.java new file mode 100644 index 0000000..04a8f2c --- /dev/null +++ b/src/main/java/com/wb/excel/api/style/ErrorCellStyle.java @@ -0,0 +1,22 @@ +package com.wb.excel.api.style; + +import org.apache.poi.hssf.util.HSSFColor; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Workbook; + +public class ErrorCellStyle extends BaseCellStyle { + + public ErrorCellStyle(Workbook workbook) { + super(workbook); + style.setFillForegroundColor(HSSFColor.RED.index); //背景颜色红色 + style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置单元格填充样式 + } + + public CellStyle getStyle() { + return style; + } + + public void setStyle(CellStyle style) { + this.style = style; + } +} diff --git a/src/main/java/com/wb/excel/api/style/HeadCellStyle.java b/src/main/java/com/wb/excel/api/style/HeadCellStyle.java index 15705c7..9d6ec1e 100644 --- a/src/main/java/com/wb/excel/api/style/HeadCellStyle.java +++ b/src/main/java/com/wb/excel/api/style/HeadCellStyle.java @@ -12,23 +12,5 @@ public class HeadCellStyle extends BaseCellStyle { style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 设置单元格填充样式 style.setAlignment(CellStyle.ALIGN_CENTER); // 居中 style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//上下居中 - style.setFont(new HeadFont(workbook).getFont()); - - //下边框 - style.setBorderBottom(CellStyle.SOLID_FOREGROUND); - //下边框颜色 - style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); - //左边框 - style.setBorderLeft(CellStyle.SOLID_FOREGROUND); - //左边框颜色 - style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); - //右边框 - style.setBorderRight(CellStyle.SOLID_FOREGROUND); - //右边框颜色 - style.setRightBorderColor(IndexedColors.BLACK.getIndex()); - //上边框 - style.setBorderTop(CellStyle.SOLID_FOREGROUND); - //上边框颜色 - style.setTopBorderColor(IndexedColors.BLACK.getIndex()); } } diff --git a/src/main/java/com/wb/excel/api/style/HeadFont.java b/src/main/java/com/wb/excel/api/style/HeadFont.java deleted file mode 100644 index f50b992..0000000 --- a/src/main/java/com/wb/excel/api/style/HeadFont.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.wb.excel.api.style; - -import org.apache.poi.ss.usermodel.Workbook; - -/** - * 普通字体.颜色黑 - * Created on 2015/1/29. - * - * @author - * @since 2.0.0 - */ -public class HeadFont extends BaseFont { - public HeadFont(Workbook workbook) { - super(workbook); - } -} diff --git a/src/main/java/com/wb/excel/api/style/NormalCellStyle.java b/src/main/java/com/wb/excel/api/style/NormalCellStyle.java deleted file mode 100644 index ac7126d..0000000 --- a/src/main/java/com/wb/excel/api/style/NormalCellStyle.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.wb.excel.api.style; - -import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.IndexedColors; -import org.apache.poi.ss.usermodel.Workbook; - -/** - * Created on 2015/1/29. - * - * @author - * @since 2.0.0 - */ -public class NormalCellStyle extends BaseCellStyle { - public NormalCellStyle(Workbook workbook) { - this(workbook, false); - } - - public NormalCellStyle(Workbook workbook, boolean error) { - super(workbook, error); - style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //上下居中 - style.setBorderBottom(CellStyle.SOLID_FOREGROUND); //下边框 - style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //下边框颜色 - style.setBorderLeft(CellStyle.SOLID_FOREGROUND); //左边框 - style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); //左边框颜色 - style.setBorderRight(CellStyle.SOLID_FOREGROUND); //右边框 - style.setRightBorderColor(IndexedColors.BLACK.getIndex()); //右边框颜色 - style.setBorderTop(CellStyle.SOLID_FOREGROUND); //上边框 - style.setTopBorderColor(IndexedColors.BLACK.getIndex()); //上边框颜色 - } -} diff --git a/src/main/java/com/wb/excel/api/util/ValidationUtil.java b/src/main/java/com/wb/excel/api/util/ValidationUtil.java index 675d82a..baf697f 100644 --- a/src/main/java/com/wb/excel/api/util/ValidationUtil.java +++ b/src/main/java/com/wb/excel/api/util/ValidationUtil.java @@ -35,6 +35,7 @@ public class ValidationUtil { } } } catch (Exception e) { + e.printStackTrace(); validResult.add("数据检查错误"); } return validResult; diff --git a/src/test/java/ExampleTest.java b/src/test/java/ExampleTest.java index ebc67f2..5d257bd 100644 --- a/src/test/java/ExampleTest.java +++ b/src/test/java/ExampleTest.java @@ -13,8 +13,8 @@ import java.util.List; public class ExampleTest { public static void main(String[] args) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { -// testTemplate(); -// testExport(); + testTemplate(); + testExport(); testImport(); } @@ -33,20 +33,24 @@ public class ExampleTest { //第一步,准备数据模型及数据,模型需要打上注解 List pos = new ArrayList(); - User user = new User(); - user.setWz("张三"); - user.setDate(new Date()); - user.setBr(true); - user.setBt((byte) 1); - user.setZf('A'); - user.setDzs((short) 1); - user.setZs(1); - user.setCzs((long) 1); - user.setFd(1.0f); - user.setSjd(1.0); - pos.add(user); - pos.add(user); - + { + User user = new User(); + user.setWz("张三"); + user.setDate(new Date()); + user.setBr(true); + user.setCzs(1500); + pos.add(user); + } + { + User user = new User(); + user.setWz("张三"); + pos.add(user); + } + { + User user = new User(); + user.setWz("张四"); + pos.add(user); + } //第二步,初始化数据 WSheet WSheet = new WSheet<>(pos); diff --git a/src/test/java/User.java b/src/test/java/User.java index eb95c24..3e4891d 100644 --- a/src/test/java/User.java +++ b/src/test/java/User.java @@ -2,8 +2,7 @@ import com.wb.excel.api.annotation.ColumnDescription; import com.wb.excel.api.annotation.ColumnName; import com.wb.excel.api.annotation.SheetName; -import javax.validation.constraints.Min; -import javax.validation.constraints.NotNull; +import javax.validation.constraints.*; import java.util.Date; @SheetName("用户") @@ -16,6 +15,7 @@ public class User { @ColumnName("日期时间") private Date date; @ColumnName("布尔") + @AssertTrue(message = "布尔必须是TRUE") private boolean br; @ColumnName("比特") private byte bt; @@ -26,9 +26,11 @@ public class User { @ColumnName("整数") private int zs; @ColumnName("长整数") - @Min(value = 100,message = "最低不得小于100") + @Max(value = 2000, message = "长整数是需<2000之间") + @Min(value = 1000, message = "长整数是需>1000之间") private long czs; @ColumnName("浮点") + @Max(value = 1, message = "浮点必须小于1之间") private float fd; @ColumnName("双精度") private double sjd;