From e7b501e97d340d2fe097f781a68632ab1fec9a9a Mon Sep 17 00:00:00 2001 From: jueyue Date: Fri, 13 Nov 2015 21:29:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=85=A5=E6=A0=A1=E9=AA=8C=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E7=9A=84=E6=AD=A3=E7=A1=AE=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../poi/excel/annotation/Excel.java | 8 +++ .../poi/excel/ExcelImportUtil.java | 26 +++---- .../poi/excel/entity/ImportParams.java | 12 ++++ .../entity/params/ExcelImportEntity.java | 12 ++++ .../excel/export/base/ExcelExportBase.java | 1 + .../poi/excel/export/base/ExportBase.java | 51 ++------------ .../template/ExcelExportOfTemplateUtil.java | 4 +- .../poi/excel/imports/CellValueServer.java | 3 +- .../poi/excel/imports/ExcelImportServer.java | 67 ++++++++++++------- .../excel/imports/base/ImportBaseService.java | 60 +++++++---------- .../exception/excel/ExcelImportException.java | 6 +- .../excel/enums/ExcelImportEnum.java | 2 +- .../poi/util/PoiPublicUtil.java | 22 ++++++ .../poi/util/PoiValidationUtil.java | 55 +++++++++++++++ 14 files changed, 203 insertions(+), 126 deletions(-) create mode 100644 easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiValidationUtil.java diff --git a/easypoi-annotation/src/main/java/org/jeecgframework/poi/excel/annotation/Excel.java b/easypoi-annotation/src/main/java/org/jeecgframework/poi/excel/annotation/Excel.java index 99b4230..ca3300b 100644 --- a/easypoi-annotation/src/main/java/org/jeecgframework/poi/excel/annotation/Excel.java +++ b/easypoi-annotation/src/main/java/org/jeecgframework/poi/excel/annotation/Excel.java @@ -129,4 +129,12 @@ public @interface Excel { * @return */ public boolean isHyperlink() default false; + + /** + * 导入时会校验这个字段,看看这个字段是不是导入的Excel中有,如果没有说明是错误的Excel + * 本意是想用true的,想想还是false比较好 + * 可以使用a_id,b_id来确实是否使用 + * @return + */ + public String isImportField() default "false"; } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/ExcelImportUtil.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/ExcelImportUtil.java index 7b9fa08..f4d0b73 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/ExcelImportUtil.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/ExcelImportUtil.java @@ -17,15 +17,16 @@ package org.jeecgframework.poi.excel; import java.io.File; import java.io.FileInputStream; -import java.io.IOException; import java.io.InputStream; import java.util.List; +import org.apache.poi.util.IOUtils; import org.jeecgframework.poi.excel.entity.ImportParams; import org.jeecgframework.poi.excel.entity.result.ExcelImportResult; import org.jeecgframework.poi.excel.imports.ExcelImportServer; import org.jeecgframework.poi.excel.imports.sax.SaxReadExcel; import org.jeecgframework.poi.excel.imports.sax.parse.ISaxRowRead; +import org.jeecgframework.poi.exception.excel.ExcelImportException; import org.jeecgframework.poi.handler.inter.IExcelReadRowHanlder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,20 +57,17 @@ public class ExcelImportUtil { */ public static List importExcel(File file, Class pojoClass, ImportParams params) { FileInputStream in = null; - List result = null; try { in = new FileInputStream(file); - result = new ExcelImportServer().importExcelByIs(in, pojoClass, params).getList(); + return new ExcelImportServer().importExcelByIs(in, pojoClass, params).getList(); + } catch (ExcelImportException e) { + throw new ExcelImportException(e.getType(), e); } catch (Exception e) { LOGGER.error(e.getMessage(), e); + throw new ExcelImportException(e.getMessage(), e); } finally { - try { - in.close(); - } catch (IOException e) { - LOGGER.error(e.getMessage(), e); - } + IOUtils.closeQuietly(in); } - return result; } /** @@ -116,16 +114,14 @@ public class ExcelImportUtil { try { in = new FileInputStream(file); return new ExcelImportServer().importExcelByIs(in, pojoClass, params); + } catch (ExcelImportException e) { + throw new ExcelImportException(e.getType(), e); } catch (Exception e) { LOGGER.error(e.getMessage(), e); + throw new ExcelImportException(e.getMessage(), e); } finally { - try { - in.close(); - } catch (IOException e) { - LOGGER.error(e.getMessage(), e); - } + IOUtils.closeQuietly(in); } - return null; } /** diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/ImportParams.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/ImportParams.java index c7b957e..54afe7b 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/ImportParams.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/ImportParams.java @@ -71,6 +71,10 @@ public class ImportParams extends ExcelBaseParams { * 最后的无效行数 */ private int lastOfInvalidRow = 0; + /** + * 导入时校验数据模板,是不是正确的Excel + */ + private String[] importFields; public int getHeadRows() { return headRows; @@ -160,4 +164,12 @@ public class ImportParams extends ExcelBaseParams { this.needVerfiy = needVerfiy; } + public String[] getImportFields() { + return importFields; + } + + public void setImportFields(String[] importFields) { + this.importFields = importFields; + } + } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelImportEntity.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelImportEntity.java index 53e385e..312ef41 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelImportEntity.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/params/ExcelImportEntity.java @@ -43,6 +43,10 @@ public class ExcelImportEntity extends ExcelBaseEntity { * 后缀 */ private String suffix; + /** + * 导入校验字段 + */ + private boolean importField; private List list; @@ -94,4 +98,12 @@ public class ExcelImportEntity extends ExcelBaseEntity { this.suffix = suffix; } + public boolean isImportField() { + return importField; + } + + public void setImportField(boolean importField) { + this.importField = importField; + } + } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExcelExportBase.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExcelExportBase.java index d1b7d28..c61cf32 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExcelExportBase.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExcelExportBase.java @@ -57,6 +57,7 @@ import org.slf4j.LoggerFactory; * @author JueYue * @date 2014年6月17日 下午6:15:13 */ +@SuppressWarnings("unchecked") public abstract class ExcelExportBase extends ExportBase { private static final Logger LOGGER = LoggerFactory.getLogger(ExcelExportBase.class); diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExportBase.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExportBase.java index cfd2c0b..a61d6cb 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExportBase.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExportBase.java @@ -44,6 +44,7 @@ import org.jeecgframework.poi.util.PoiReflectorUtil; * @author JueYue * @date 2014年8月9日 下午11:01:32 */ +@SuppressWarnings("rawtypes") public class ExportBase { protected IExcelDataHandler dataHanlder; @@ -123,8 +124,8 @@ public class ExportBase { getAllExcelField(exclusions, StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId, PoiPublicUtil.getClassFields(clz), list, clz, null); excelEntity = new ExcelExportEntity(); - excelEntity.setName(getExcelName(excel.name(), targetId)); - excelEntity.setOrderNum(getCellOrder(excel.orderNum(), targetId)); + excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId,null)); + excelEntity.setOrderNum(Integer.valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId,"0"))); excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); excelEntity.setList(list); excelParams.add(excelEntity); @@ -142,28 +143,6 @@ public class ExportBase { } } - /** - * 获取这个字段的顺序 - * - * @param orderNum - * @param targetId - * @return - */ - public int getCellOrder(String orderNum, String targetId) { - if (isInteger(orderNum) || targetId == null) { - return Integer.valueOf(orderNum); - } - String[] arr = orderNum.split(","); - String[] temp; - for (String str : arr) { - temp = str.split("_"); - if (targetId.equals(temp[1])) { - return Integer.valueOf(temp[0]); - } - } - return 0; - } - /** * 获取填如这个cell的值,提供一些附加功能 * @@ -172,6 +151,7 @@ public class ExportBase { * @return * @throws Exception */ + @SuppressWarnings("unchecked") public Object getCellValue(ExcelExportEntity entity, Object obj) throws Exception { Object value; if (obj instanceof Map) { @@ -224,14 +204,14 @@ public class ExportBase { */ private void getExcelField(String targetId, Field field, ExcelExportEntity excelEntity, Excel excel, Class pojoClass) throws Exception { - excelEntity.setName(getExcelName(excel.name(), targetId)); + excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null)); excelEntity.setWidth(excel.width()); excelEntity.setHeight(excel.height()); excelEntity.setNeedMerge(excel.needMerge()); excelEntity.setMergeVertical(excel.mergeVertical()); excelEntity.setMergeRely(excel.mergeRely()); excelEntity.setReplace(excel.replace()); - excelEntity.setOrderNum(getCellOrder(excel.orderNum(), targetId)); + excelEntity.setOrderNum(Integer.valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId, "0"))); excelEntity.setWrap(excel.isWrap()); excelEntity.setExportImageType(excel.imageType()); excelEntity.setSuffix(excel.suffix()); @@ -243,25 +223,6 @@ public class ExportBase { excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); } - /** - * 判断在这个单元格显示的名称 - * - * @param exportName - * @param targetId - * @return - */ - public String getExcelName(String exportName, String targetId) { - if (exportName.indexOf(",") < 0) { - return exportName; - } - String[] arr = exportName.split(","); - for (String str : arr) { - if (str.indexOf(targetId) != -1) { - return str.split("_")[0]; - } - } - return null; - } /** * 多个反射获取值 diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java index f84fcfa..6ad92f3 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java @@ -522,9 +522,9 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { while (true) { int colSpan = columns.get(columns.size() - 1) != null ? columns.get(columns.size() - 1).getColspan() : 1; - index += columns.get(columns.size() - 1).getColspan(); + index += colSpan; for (int i = 1; i < colSpan; i++) { - //添加跳过的数据 + //添加合并的单元格,这些单元可能不是空,但是没有值,所以也需要跳过 columns.add(null); continue; } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/CellValueServer.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/CellValueServer.java index 30790c7..86009ae 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/CellValueServer.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/CellValueServer.java @@ -242,7 +242,8 @@ public class CellValueServer { */ private Object getValueByType(String xclass, Object result, ExcelImportEntity entity) { try { - if(result == null){ + //过滤空和空字符串,如果基本类型null会在上层抛出,这里就不处理了 + if(result == null || StringUtils.isBlank(result.toString())){ return null; } if ("class java.util.Date".equals(xclass)) { diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java index 2029d3a..b60ce53 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java @@ -27,12 +27,6 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Set; - -import javax.validation.ConstraintViolation; -import javax.validation.Validation; -import javax.validation.Validator; -import javax.validation.ValidatorFactory; import org.apache.commons.lang3.StringUtils; import org.apache.poi.POIXMLDocument; @@ -62,6 +56,7 @@ import org.jeecgframework.poi.exception.excel.enums.ExcelImportEnum; import org.jeecgframework.poi.handler.inter.IExcelModel; import org.jeecgframework.poi.util.PoiPublicUtil; import org.jeecgframework.poi.util.PoiReflectorUtil; +import org.jeecgframework.poi.util.PoiValidationUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -76,13 +71,6 @@ public class ExcelImportServer extends ImportBaseService { private final static Logger LOGGER = LoggerFactory.getLogger(ExcelImportServer.class); - private final static Validator validator; - - static { - ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); - validator = factory.getValidator(); - } - private CellValueServer cellValueServer; private boolean verfiyFail = false; @@ -207,6 +195,7 @@ public class ExcelImportServer extends ImportBaseService { rows.next(); } Map titlemap = getTitleMap(rows, params, excelCollection); + checkIsValidTemplate(titlemap, excelParams, params,excelCollection); Row row = null; Object object = null; String picId; @@ -270,14 +259,13 @@ public class ExcelImportServer extends ImportBaseService { boolean isAdd = true; Cell cell = null; if (params.isNeedVerfiy()) { - Set> set = validator.validate(object); - if (set.size() > 0) { + String errorMsg = PoiValidationUtil.validation(object); + if (StringUtils.isNotEmpty(errorMsg)) { cell = row.createCell(row.getLastCellNum()); - String errMsg = getValidateErrMsg(set); - cell.setCellValue(errMsg); + cell.setCellValue(errorMsg); if (object instanceof IExcelModel) { IExcelModel model = (IExcelModel) object; - model.setErrorMsg(errMsg); + model.setErrorMsg(errorMsg); } else { isAdd = false; } @@ -304,14 +292,6 @@ public class ExcelImportServer extends ImportBaseService { return isAdd; } - private String getValidateErrMsg(Set> set) { - StringBuilder builder = new StringBuilder(); - for (ConstraintViolation constraintViolation : set) { - builder.append(constraintViolation.getMessage()).append(","); - } - return builder.substring(0, builder.length() - 1).toString(); - } - /** * 获取表格字段列名对应信息 * @param rows @@ -428,6 +408,41 @@ public class ExcelImportServer extends ImportBaseService { return new ExcelImportResult(result, verfiyFail, book); } + /** + * 检查是不是合法的模板 + * @param titlemap + * @param excelParams + * @param params + * @param excelCollection + */ + private void checkIsValidTemplate(Map titlemap, Map excelParams, ImportParams params, List excelCollection) { + + if(params.getImportFields() != null){ + for (int i = 0 ,le = params.getImportFields().length; i < le; i++) { + if(!titlemap.containsValue(params.getImportFields()[i])){ + throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE); + } + } + } else { + Collection collection = excelParams.values(); + for (ExcelImportEntity excelImportEntity : collection) { + if(excelImportEntity.isImportField() && !titlemap.containsValue(excelImportEntity.getName())){ + throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE); + } + } + + for (int i = 0, le = excelCollection.size(); i < le; i++) { + ExcelCollectionParams collectionparams = excelCollection.get(i); + collection = collectionparams.getExcelParams().values(); + for (ExcelImportEntity excelImportEntity : collection) { + if(excelImportEntity.isImportField() && !titlemap.containsValue(collectionparams.getExcelName()+"_"+excelImportEntity.getName())){ + throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE); + } + } + } + } + } + /** * 保存字段值(获取值,校验值,追加错误信息) * diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/base/ImportBaseService.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/base/ImportBaseService.java index 20278f6..b4fe426 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/base/ImportBaseService.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/base/ImportBaseService.java @@ -34,6 +34,7 @@ import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.IOUtils; import org.jeecgframework.poi.excel.annotation.Excel; import org.jeecgframework.poi.excel.annotation.ExcelCollection; +import org.jeecgframework.poi.excel.annotation.ExcelEntity; import org.jeecgframework.poi.excel.entity.ImportParams; import org.jeecgframework.poi.excel.entity.params.ExcelCollectionParams; import org.jeecgframework.poi.excel.entity.params.ExcelImportEntity; @@ -69,6 +70,8 @@ public class ImportBaseService { excelEntity.setReplace(excel.replace()); excelEntity.setDatabaseFormat(excel.databaseFormat()); excelEntity.setSuffix(excel.suffix()); + excelEntity.setImportField(Boolean + .valueOf(PoiPublicUtil.getValueByTargetId(excel.isImportField(), targetId, "false"))); getExcelField(targetId, field, excelEntity, excel, pojoClass); if (getMethods != null) { List newMethods = new ArrayList(); @@ -103,15 +106,18 @@ public class ImportBaseService { } if (PoiPublicUtil.isCollection(field.getType())) { // 集合对象设置属性 + ExcelCollection excel = field.getAnnotation(ExcelCollection.class); ExcelCollectionParams collection = new ExcelCollectionParams(); collection.setName(field.getName()); Map temp = new HashMap(); ParameterizedType pt = (ParameterizedType) field.getGenericType(); Class clz = (Class) pt.getActualTypeArguments()[0]; collection.setType(clz); - getExcelFieldList(targetId, PoiPublicUtil.getClassFields(clz), clz, temp, null); + getExcelFieldList(StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId, + PoiPublicUtil.getClassFields(clz), clz, temp, null); collection.setExcelParams(temp); - collection.setExcelName(field.getAnnotation(ExcelCollection.class).name()); + collection.setExcelName(PoiPublicUtil.getValueByTargetId( + field.getAnnotation(ExcelCollection.class).name(), targetId, null)); additionalCollectionName(collection); excelCollection.add(collection); } else if (PoiPublicUtil.isJavaClass(field)) { @@ -121,9 +127,12 @@ public class ImportBaseService { if (getMethods != null) { newMethods.addAll(getMethods); } + // newMethods.add(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); - getAllExcelField(targetId, PoiPublicUtil.getClassFields(field.getType()), - excelParams, excelCollection, field.getType(), newMethods); + ExcelEntity excel = field.getAnnotation(ExcelEntity.class); + getAllExcelField(StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId, + PoiPublicUtil.getClassFields(field.getType()), excelParams, excelCollection, + field.getType(), newMethods); } } } @@ -144,7 +153,7 @@ public class ImportBaseService { public void getExcelField(String targetId, Field field, ExcelImportEntity excelEntity, Excel excel, Class pojoClass) throws Exception { - excelEntity.setName(getExcelName(excel.name(), targetId)); + excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null)); String fieldname = field.getName(); excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getSetMethod(fieldname)); if (StringUtils.isNotEmpty(excel.importFormat())) { @@ -155,8 +164,8 @@ public class ImportBaseService { } public void getExcelFieldList(String targetId, Field[] fields, Class pojoClass, - Map temp, List getMethods) - throws Exception { + Map temp, + List getMethods) throws Exception { ExcelImportEntity excelEntity = null; for (int i = 0; i < fields.length; i++) { Field field = fields[i]; @@ -170,34 +179,15 @@ public class ImportBaseService { if (getMethods != null) { newMethods.addAll(getMethods); } - newMethods - .add(PoiReflectorUtil.fromCache(pojoClass).getSetMethod(field.getName())); - getExcelFieldList(targetId, PoiPublicUtil.getClassFields(field.getType()), - field.getType(), temp, newMethods); + ExcelEntity excel = field.getAnnotation(ExcelEntity.class); + newMethods.add(PoiReflectorUtil.fromCache(pojoClass).getSetMethod(field.getName())); + getExcelFieldList(StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId, + PoiPublicUtil.getClassFields(field.getType()), field.getType(), temp, + newMethods); } } } - /** - * 判断在这个单元格显示的名称 - * - * @param exportName - * @param targetId - * @return - */ - public String getExcelName(String exportName, String targetId) { - if (exportName.indexOf("_") < 0) { - return exportName; - } - String[] arr = exportName.split(","); - for (String str : arr) { - if (str.indexOf(targetId) != -1) { - return str.split("_")[0]; - } - } - return null; - } - public Object getFieldBySomeMethod(List list, Object t) throws Exception { Method m; for (int i = 0; i < list.size() - 1; i++) { @@ -215,8 +205,8 @@ public class ImportBaseService { savefile.mkdirs(); } SimpleDateFormat format = new SimpleDateFormat("yyyMMddHHmmss"); - FileOutputStream fos = new FileOutputStream(path + "/" + format.format(new Date()) + "_" - + Math.round(Math.random() * 100000) + FileOutputStream fos = new FileOutputStream( + path + "/" + format.format(new Date()) + "_" + Math.round(Math.random() * 100000) + (isXSSFWorkbook == true ? ".xlsx" : ".xls")); book.write(fos); IOUtils.closeQuietly(fos); @@ -245,8 +235,8 @@ public class ImportBaseService { * @param setMethods * @param object */ - public void setFieldBySomeMethod(List setMethods, Object object, Object value) - throws Exception { + public void setFieldBySomeMethod(List setMethods, Object object, + Object value) throws Exception { Object t = getFieldBySomeMethod(setMethods, object); setMethods.get(setMethods.size() - 1).invoke(t, value); } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/exception/excel/ExcelImportException.java b/easypoi-base/src/main/java/org/jeecgframework/poi/exception/excel/ExcelImportException.java index 887ab82..0bd1fdf 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/exception/excel/ExcelImportException.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/exception/excel/ExcelImportException.java @@ -26,7 +26,7 @@ public class ExcelImportException extends RuntimeException { private static final long serialVersionUID = 1L; - private ExcelImportEnum type; + private ExcelImportEnum type; public ExcelImportException() { super(); @@ -50,6 +50,10 @@ public class ExcelImportException extends RuntimeException { this.type = type; } + public ExcelImportException(String message, Throwable cause) { + super(message, cause); + } + public ExcelImportEnum getType() { return type; } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/exception/excel/enums/ExcelImportEnum.java b/easypoi-base/src/main/java/org/jeecgframework/poi/exception/excel/enums/ExcelImportEnum.java index d4dff15..d06dcc4 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/exception/excel/enums/ExcelImportEnum.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/exception/excel/enums/ExcelImportEnum.java @@ -22,7 +22,7 @@ package org.jeecgframework.poi.exception.excel.enums; */ public enum ExcelImportEnum { - GET_VALUE_ERROR("Excel 值获取失败"), VERIFY_ERROR("值校验失败"); + IS_NOT_A_VALID_TEMPLATE("不是合法的Excel模板"), GET_VALUE_ERROR("Excel 值获取失败"), VERIFY_ERROR("值校验失败"); private String msg; diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java b/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java index 0187989..6ce816b 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java @@ -31,6 +31,7 @@ import java.util.Map; import javax.imageio.ImageIO; +import org.apache.commons.lang3.StringUtils; import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFPicture; @@ -432,5 +433,26 @@ public final class PoiPublicUtil { } return temp; } + + /** + * 统一 key的获取规则 + * @param key + * @param targetId + * @return + */ + public static String getValueByTargetId(String key, String targetId, String defalut){ + if (StringUtils.isEmpty(targetId) || key.indexOf("_") < 0) { + return key; + } + String[] arr = key.split(","); + String[] tempArr; + for (String str : arr) { + tempArr = str.split("_"); + if (targetId.equals(tempArr[1])) { + return tempArr[0]; + } + } + return defalut; + } } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiValidationUtil.java b/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiValidationUtil.java new file mode 100644 index 0000000..433e8f6 --- /dev/null +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiValidationUtil.java @@ -0,0 +1,55 @@ +/** + * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jeecgframework.poi.util; + +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; + +/** + * HIBERNATE 校验工具类 + * @author JueYue + * @date 2015年11月11日 下午10:04:07 + */ +public class PoiValidationUtil { + + private final static Validator validator; + + static { + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); + validator = factory.getValidator(); + } + + public static String validation(Object obj) { + Set> set = validator.validate(obj); + if (set.size() > 0) { + return getValidateErrMsg(set); + } + return null; + } + + private static String getValidateErrMsg(Set> set) { + StringBuilder builder = new StringBuilder(); + for (ConstraintViolation constraintViolation : set) { + builder.append(constraintViolation.getMessage()).append(","); + } + return builder.substring(0, builder.length() - 1).toString(); + } + +}