{
@Override
public Long convert(String var) {
try {
+ //增加对1.0一类的的兼容性
+ if (var.matches("(\\d+)\\.(\\d*)")) {
+ Pattern compile = Pattern.compile("(\\d+)\\.(\\d*)");
+ Matcher matcher = compile.matcher(var);
+ if (matcher.find()) {
+ var = matcher.group(1);
+ }
+ }
+
return Long.parseLong(var);
} catch (Exception e) {
return 0L;
diff --git a/src/main/java/com/wb/excel/api/entity/ExcelImportResult.java b/src/main/java/com/wb/excel/api/entity/ExcelImportResult.java
deleted file mode 100644
index 56be87f..0000000
--- a/src/main/java/com/wb/excel/api/entity/ExcelImportResult.java
+++ /dev/null
@@ -1,183 +0,0 @@
-package com.wb.excel.api.entity;
-
-import com.wb.excel.api.style.ErrorCellStyle;
-import org.apache.poi.ss.usermodel.*;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-/**
- * ***************************************************************
- *
- *
- * Copyright (c) 2014 –
- * ColumnDescription:导入返回结果
- * ***************************************************************
- *
- */
-public class ExcelImportResult {
- /**
- * 结果集
- */
- private List list;
- /**
- * 是否存在校验失败
- */
- private boolean verfiyFail;
-
- private Map verifyResult;
-
- /**
- * 数据源
- */
- private Workbook workbook;
- private CellStyle errorMessageStyle;
- private ErrorCellStyle errorCellStyle;
-
- public ExcelImportResult(List list, boolean verfiyFail, Workbook workbook, Map verifyResult) {
- this.list = list;
- this.verfiyFail = verfiyFail;
- this.workbook = workbook;
- this.verifyResult = verifyResult;
- this.createErrorCellStyle(workbook);
- errorCellStyle = new ErrorCellStyle(workbook);
- }
-
- public List getList() {
- return list;
- }
-
- public Workbook getWorkbook() {
- //循环添加错误信息
- Sheet sheet = workbook.getSheetAt(0);
- Iterator rows = sheet.rowIterator();
- //行信息,添加校验结果
- Row titleRow = rows.next();
- addValidateTitleInfo(titleRow);
- //循环,给行添加错误信息
- Row row = null;
- while (rows.hasNext() && (row == null || sheet.getLastRowNum() - row.getRowNum() > 0)) {
- row = rows.next();
- addValidateInfo(row, verifyResult.get(row.getRowNum()));
- }
- return workbook;
- }
-
- public byte[] getBytes() throws IOException {
- Workbook tempworkbook = this.getWorkbook();
- if (tempworkbook != null) {
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- tempworkbook.write(outputStream);
- return outputStream.toByteArray();
- }
- return null;
- }
-
- /**
- * 添加校验信息
- *
- * @param row
- */
- private void addValidateTitleInfo(Row row) {
- Map temptitle = new HashMap();
- for (int i = row.getFirstCellNum(), le = row.getLastCellNum(); i < le; i++) {
- temptitle.put(i, row.getCell(i).getStringCellValue());
- }
- for (int i = row.getFirstCellNum(), le = row.getLastCellNum(); i < le; i++) {
- row.createCell(i + 1).setCellValue(temptitle.get(i));
- }
- row.createCell(0).setCellValue("检查状态");
- row.createCell(row.getLastCellNum()).setCellValue("错误信息");
- }
-
- /**
- * 添加错误行信息
- *
- * @param row
- * @param dataVerifyResult
- */
- private void addValidateInfo(Row row, DataVerifyResult dataVerifyResult) {
- Map temptitle = new HashMap();
- for (int i = row.getFirstCellNum(), le = row.getLastCellNum(); i < le; i++) {
- temptitle.put(i, String.valueOf(getCellValue(row.getCell(i))));
- }
- for (int i = row.getFirstCellNum(), le = row.getLastCellNum(); i < le; i++) {
- row.createCell(i + 1).setCellValue(temptitle.get(i));
- }
- Boolean result = dataVerifyResult == null || dataVerifyResult.isSuccess();
- Cell statusCell = row.createCell(0);
- if (!result) {
- statusCell.setCellStyle(errorCellStyle.getStyle());
- }
- statusCell.setCellValue(result ? "通过" : "不通过");
- Cell errorcell = row.createCell(row.getLastCellNum());
- errorcell.setCellStyle(errorMessageStyle);
- errorcell.setCellValue(dataVerifyResult == null ? "" : dataVerifyResult.getMsg());
- }
-
- /**
- * 获取列数据,根据列属性
- *
- * @param cell
- * @return
- */
- private Object getCellValue(Cell cell) {
- Object result = null;
- if (cell != null) {
- if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
- result = cell.getNumericCellValue();
- } else if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
- result = cell.getBooleanCellValue();
- } else {
- result = cell.getStringCellValue();
- }
- }
- result = result == null ? "" : result;
- return result;
- }
-
- public boolean isVerfiyFail() {
- return verfiyFail;
- }
-
- public void setList(List list) {
- this.list = list;
- }
-
- public void setVerfiyFail(boolean verfiyFail) {
- this.verfiyFail = verfiyFail;
- }
-
- public void setWorkbook(Workbook workbook) {
- this.workbook = workbook;
- }
-
- public void setVerifyResult(Map verifyResult) {
- Iterator keys = verifyResult.keySet().iterator();
- while (keys.hasNext()) {
- Integer key = keys.next();
- if (this.verifyResult == null) {
- this.verifyResult = new HashMap<>();
- }
- if (this.verifyResult.containsKey(key)) {
- DataVerifyResult result = this.verifyResult.get(key);
- result.setMsg(result.getMsg() + " " + verifyResult.get(key).getMsg());
- result.setSuccess(verifyResult.get(key).isSuccess());
- this.verifyResult.put(key, result);
- } else {
- this.verifyResult.put(key, verifyResult.get(key));
- }
- }
- }
-
- private void createErrorCellStyle(Workbook workbook) {
- errorMessageStyle = workbook.createCellStyle();
- Font font = workbook.createFont();
- font.setColor(Font.COLOR_RED);
- errorMessageStyle.setFont(font);
- }
-}
diff --git a/src/main/java/com/wb/excel/api/style/AlignRightCellStyle.java b/src/main/java/com/wb/excel/api/style/AlignRightCellStyle.java
new file mode 100644
index 0000000..2555c43
--- /dev/null
+++ b/src/main/java/com/wb/excel/api/style/AlignRightCellStyle.java
@@ -0,0 +1,24 @@
+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 b76f82b..b18360f 100644
--- a/src/main/java/com/wb/excel/api/style/BaseCellStyle.java
+++ b/src/main/java/com/wb/excel/api/style/BaseCellStyle.java
@@ -1,5 +1,6 @@
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;
@@ -16,7 +17,16 @@ 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); //设置单元格填充样式
+ }
}
public CellStyle getStyle() {
diff --git a/src/main/java/com/wb/excel/api/style/CheckFailureCellStyle.java b/src/main/java/com/wb/excel/api/style/CheckFailureCellStyle.java
deleted file mode 100644
index b84b512..0000000
--- a/src/main/java/com/wb/excel/api/style/CheckFailureCellStyle.java
+++ /dev/null
@@ -1,44 +0,0 @@
-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.Font;
-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 CheckFailureCellStyle extends BaseCellStyle {
- public CheckFailureCellStyle(Workbook workbook) {
- super(workbook);
- Font font = workbook.createFont(); // 单元格的字体
- font.setColor(HSSFColor.WHITE.index); // 字体颜色-白色
- CellStyle style = workbook.createCellStyle(); // 单元格的样式
- style.setFillForegroundColor(HSSFColor.RED.index); // 背景颜色-红色
- style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 设置单元格填充样式
- style.setAlignment(CellStyle.ALIGN_CENTER); // 居中
- style.setFont(font);
- 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/style/CheckMessageCellStyle.java b/src/main/java/com/wb/excel/api/style/CheckMessageCellStyle.java
deleted file mode 100644
index 3759662..0000000
--- a/src/main/java/com/wb/excel/api/style/CheckMessageCellStyle.java
+++ /dev/null
@@ -1,24 +0,0 @@
-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.Font;
-import org.apache.poi.ss.usermodel.Workbook;
-
-/**
- * Created on 2015/1/29.
- *
- * @author
- * @since 2.0.0
- */
-public class CheckMessageCellStyle extends BaseCellStyle {
- public CheckMessageCellStyle(Workbook workbook) {
- super(workbook);
- Font font = workbook.createFont(); // 单元格的字体
- font.setColor(HSSFColor.BLACK.index); // 字体颜色-黑色
- CellStyle style = workbook.createCellStyle(); // 单元格的样式
- style.setFillPattern(CellStyle.NO_FILL); // 设置单元格无填充
- style.setFont(font);
- style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //上下居中
- }
-}
diff --git a/src/main/java/com/wb/excel/api/style/CheckSuccessCellStyle.java b/src/main/java/com/wb/excel/api/style/CheckSuccessCellStyle.java
deleted file mode 100644
index a6a2e02..0000000
--- a/src/main/java/com/wb/excel/api/style/CheckSuccessCellStyle.java
+++ /dev/null
@@ -1,40 +0,0 @@
-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;
-
-/**
- * Created on 2015/1/29.
- *
- * @author
- * @since 2.0.0
- */
-public class CheckSuccessCellStyle extends BaseCellStyle {
- public CheckSuccessCellStyle(Workbook workbook) {
- super(workbook);
- style.setFillForegroundColor(HSSFColor.LIME.index); // 背景颜色-绿色
- style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 设置单元格填充样式
- style.setAlignment(CellStyle.ALIGN_CENTER); // 居中
- //style.setFont(font);
- 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/style/ErrorCellStyle.java b/src/main/java/com/wb/excel/api/style/ErrorCellStyle.java
deleted file mode 100644
index 9eea4b1..0000000
--- a/src/main/java/com/wb/excel/api/style/ErrorCellStyle.java
+++ /dev/null
@@ -1,39 +0,0 @@
-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;
-
-/**
- * Created on 2015/1/29.
- *
- * @author
- * @since 2.0.0
- */
-public class ErrorCellStyle extends BaseCellStyle {
- public ErrorCellStyle(Workbook workbook) {
- super(workbook);
- style.setFillForegroundColor(HSSFColor.ORANGE.index); //背景颜色-紫罗兰色
- style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置单元格填充样式
- //style.setFont(font); //设置单元格字体
- 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/style/ErrorNumberCellStyle.java b/src/main/java/com/wb/excel/api/style/ErrorNumberCellStyle.java
deleted file mode 100644
index 3d5ebb5..0000000
--- a/src/main/java/com/wb/excel/api/style/ErrorNumberCellStyle.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package com.wb.excel.api.style;
-
-import org.apache.poi.ss.usermodel.CellStyle;
-import org.apache.poi.ss.usermodel.Workbook;
-
-/**
- * Created on 2015/1/29.
- *
- * @author
- * @since 2.0.0
- */
-public class ErrorNumberCellStyle extends ErrorCellStyle {
- public ErrorNumberCellStyle(Workbook workbook) {
- super(workbook);
- style.setAlignment(CellStyle.ALIGN_RIGHT);
- }
-}
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 2e668c9..15705c7 100644
--- a/src/main/java/com/wb/excel/api/style/HeadCellStyle.java
+++ b/src/main/java/com/wb/excel/api/style/HeadCellStyle.java
@@ -5,12 +5,6 @@ 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 HeadCellStyle extends BaseCellStyle {
public HeadCellStyle(Workbook workbook) {
super(workbook);
@@ -18,6 +12,7 @@ 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);
diff --git a/src/main/java/com/wb/excel/api/style/HeadFont.java b/src/main/java/com/wb/excel/api/style/HeadFont.java
new file mode 100644
index 0000000..f50b992
--- /dev/null
+++ b/src/main/java/com/wb/excel/api/style/HeadFont.java
@@ -0,0 +1,16 @@
+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
index 27e62c3..ac7126d 100644
--- a/src/main/java/com/wb/excel/api/style/NormalCellStyle.java
+++ b/src/main/java/com/wb/excel/api/style/NormalCellStyle.java
@@ -12,9 +12,11 @@ import org.apache.poi.ss.usermodel.Workbook;
*/
public class NormalCellStyle extends BaseCellStyle {
public NormalCellStyle(Workbook workbook) {
- super(workbook);
- style.setFillPattern(CellStyle.NO_FILL); //单元格不填充
- //style.setFont(font); //设置单元格字体
+ 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()); //下边框颜色
diff --git a/src/main/java/com/wb/excel/api/style/NormalNumberCellStyle.java b/src/main/java/com/wb/excel/api/style/NormalNumberCellStyle.java
deleted file mode 100644
index b2f6513..0000000
--- a/src/main/java/com/wb/excel/api/style/NormalNumberCellStyle.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package com.wb.excel.api.style;
-
-import org.apache.poi.ss.usermodel.CellStyle;
-import org.apache.poi.ss.usermodel.Workbook;
-
-/**
- * Created on 2015/1/29.
- *
- * @author
- * @since 2.0.0
- */
-public class NormalNumberCellStyle extends NormalCellStyle {
- public NormalNumberCellStyle(Workbook workbook) {
- super(workbook);
- style.setAlignment(CellStyle.ALIGN_RIGHT);
- }
-}
diff --git a/src/main/java/com/wb/excel/api/util/ValidationUtil.java b/src/main/java/com/wb/excel/api/util/ValidationUtil.java
new file mode 100644
index 0000000..675d82a
--- /dev/null
+++ b/src/main/java/com/wb/excel/api/util/ValidationUtil.java
@@ -0,0 +1,42 @@
+package com.wb.excel.api.util;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.Validation;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * 验证工具类。提供一些通用简单的数据验证功能
+ *
+ * @author wangbing
+ * @version 0.0.1
+ * @since 2017-01-01
+ */
+public class ValidationUtil {
+
+ private static ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
+
+ public static List validate(Object req) {
+ List validResult = new ArrayList<>();
+ if (req == null) {
+ validResult.add("当前数据为空");
+ return validResult;
+ }
+
+ try {
+ Validator validator = factory.getValidator();
+ Set> constraintViolations = validator.validate(req);
+ if (constraintViolations.size() > 0) {
+ for (ConstraintViolation