From 416c90ea8eae3816a70b1319d76bbbd26a7da3da Mon Sep 17 00:00:00 2001 From: jueyue Date: Thu, 26 Apr 2018 14:36:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=B8=80=E4=B8=AA=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E9=80=9A=E8=BF=87=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- easypoi-annotation/pom.xml | 2 +- .../easypoi/excel/annotation/Excel.java | 2 +- easypoi-base/pom.xml | 2 +- .../easypoi/cache/manager/FileLoaderImpl.java | 5 -- .../easypoi/excel/entity/ImportParams.java | 5 +- .../entity/params/ExcelImportEntity.java | 2 + .../excel/html/ExcelToHtmlService.java | 2 +- .../excel/imports/CellValueService.java | 2 +- .../excel/imports/ExcelImportService.java | 15 +++--- .../excel/imports/base/ImportBaseService.java | 8 ++-- .../excel/imports/sax/parse/SaxRowRead.java | 2 +- .../cn/afterturn/easypoi/util/PoiElUtil.java | 47 ++++++++++++++----- .../afterturn/easypoi/util/PoiPublicUtil.java | 13 +---- easypoi-web/pom.xml | 2 +- pom.xml | 4 +- .../2.5 Map导入,自由发挥 | 2 +- 16 files changed, 66 insertions(+), 49 deletions(-) diff --git a/easypoi-annotation/pom.xml b/easypoi-annotation/pom.xml index bd88952..8ed33fe 100644 --- a/easypoi-annotation/pom.xml +++ b/easypoi-annotation/pom.xml @@ -4,7 +4,7 @@ cn.afterturn easypoi - 3.1.0 + 3.1.1-SNAPSHOT easypoi-annotation 基础注解类,解耦合 diff --git a/easypoi-annotation/src/main/java/cn/afterturn/easypoi/excel/annotation/Excel.java b/easypoi-annotation/src/main/java/cn/afterturn/easypoi/excel/annotation/Excel.java index 02c6cf2..8e4f485 100644 --- a/easypoi-annotation/src/main/java/cn/afterturn/easypoi/excel/annotation/Excel.java +++ b/easypoi-annotation/src/main/java/cn/afterturn/easypoi/excel/annotation/Excel.java @@ -121,7 +121,7 @@ public @interface Excel { * 导入路径,如果是图片可以填写,默认是upload/className/ IconEntity这个类对应的就是upload/Icon/ * */ - public String savePath() default "upload"; + public String savePath() default "/excel/upload/img"; /** * 导出类型 1 是文本 2 是图片,3 是函数,10 是数字 默认是文本 diff --git a/easypoi-base/pom.xml b/easypoi-base/pom.xml index d855bd4..c468ce1 100644 --- a/easypoi-base/pom.xml +++ b/easypoi-base/pom.xml @@ -4,7 +4,7 @@ cn.afterturn easypoi - 3.1.0 + 3.1.1-SNAPSHOT easypoi-base diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/manager/FileLoaderImpl.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/manager/FileLoaderImpl.java index f7edb89..1936361 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/manager/FileLoaderImpl.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/manager/FileLoaderImpl.java @@ -55,11 +55,6 @@ public class FileLoaderImpl implements IFileLoader { } catch (FileNotFoundException e) { //获取项目文件 fileis = FileLoaderImpl.class.getClassLoader().getResourceAsStream(url); - if (fileis == null) { - //最后再拿相对文件路径 - String path = PoiPublicUtil.getWebRootPath(url); - fileis = new FileInputStream(path); - } } } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ImportParams.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ImportParams.java index a1dff24..8f9fc6e 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ImportParams.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ImportParams.java @@ -26,6 +26,9 @@ import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler; */ @SuppressWarnings("rawtypes") public class ImportParams extends ExcelBaseParams { + + public static final String SAVE_URL = "/excel/upload/excelUpload"; + /** * 表格标题行数,默认0 */ @@ -73,7 +76,7 @@ public class ImportParams extends ExcelBaseParams { * 保存上传的Excel目录,默认是 如 TestEntity这个类保存路径就是 * upload/excelUpload/Test/yyyyMMddHHmss_***** 保存名称上传时间_五位随机数 */ - private String saveUrl = "upload/excelUpload"; + private String saveUrl = SAVE_URL; /** * 最后的无效行数 */ diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelImportEntity.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelImportEntity.java index b8b2824..57a10c8 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelImportEntity.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelImportEntity.java @@ -23,6 +23,8 @@ import java.util.List; * @version 1.0 2013年8月24日 */ public class ExcelImportEntity extends ExcelBaseEntity { + + public final static String IMG_SAVE_PATH = "/excel/upload/img"; /** * 对应 Collection NAME */ diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/html/ExcelToHtmlService.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/html/ExcelToHtmlService.java index 0529546..675371e 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/html/ExcelToHtmlService.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/html/ExcelToHtmlService.java @@ -278,7 +278,7 @@ public class ExcelToHtmlService { /*String fileName = "pic" + Math.round(Math.random() * 100000000000L); fileName += "." + PoiPublicUtil.getFileExtendName(data); if (!imageCachePath.startsWith("/") && !imageCachePath.contains(":")) { - imageCachePath = PoiPublicUtil.getWebRootPath(imageCachePath); + imageCachePath = FileUtilTest.getWebRootPath(imageCachePath); } File savefile = new File(imageCachePath + "/" + today); if (!savefile.exists()) { diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/CellValueService.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/CellValueService.java index 7e511c7..ae3e323 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/CellValueService.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/CellValueService.java @@ -309,7 +309,7 @@ public class CellValueService { } return String.valueOf(result); } - if (clazz.isEnum()) { + if (clazz != null && clazz.isEnum()) { if (StringUtils.isNotEmpty(entity.getEnumImportMethod())) { return PoiReflectorUtil.fromCache(clazz).execEnumStaticMethod(entity.getEnumImportMethod(),result); } else { diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/ExcelImportService.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/ExcelImportService.java index e6433ac..84f6db8 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/ExcelImportService.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/ExcelImportService.java @@ -145,14 +145,14 @@ public class ExcelImportService extends ImportBaseService { */ private String getSaveUrl(ExcelImportEntity excelImportEntity, Object object) throws Exception { String url = ""; - if ("upload".equals(excelImportEntity.getSaveUrl())) { + if (ExcelImportEntity.IMG_SAVE_PATH.equals(excelImportEntity.getSaveUrl())) { if (excelImportEntity.getMethods() != null && excelImportEntity.getMethods().size() > 0) { object = getFieldBySomeMethod(excelImportEntity.getMethods(), object); } url = object.getClass().getName() .split("\\.")[object.getClass().getName().split("\\.").length - 1]; - return excelImportEntity.getSaveUrl() + "/" + url; + return excelImportEntity.getSaveUrl() + File.separator + url; } return excelImportEntity.getSaveUrl(); } @@ -186,6 +186,10 @@ public class ExcelImportService extends ImportBaseService { for (int i = 0; i < params.getStartRows(); i++) { rows.next(); } + //判断index 和集合,集合情况默认为第一列 + if (excelCollection.size() > 0 && params.getKeyIndex() == null) { + params.setKeyIndex(0); + } while (rows.hasNext() && (row == null || sheet.getLastRowNum() - row.getRowNum() > params.getLastOfInvalidRow())) { @@ -585,13 +589,12 @@ public class ExcelImportService extends ImportBaseService { String fileName = "pic" + Math.round(Math.random() * 100000000000L); fileName += "." + PoiPublicUtil.getFileExtendName(data); if (excelParams.get(titleString).getSaveType() == 1) { - String path = PoiPublicUtil - .getWebRootPath(getSaveUrl(excelParams.get(titleString), object)); + String path = getSaveUrl(excelParams.get(titleString), object); File savefile = new File(path); if (!savefile.exists()) { savefile.mkdirs(); } - savefile = new File(path + "/" + fileName); + savefile = new File(path + File.separator + fileName); FileOutputStream fos = new FileOutputStream(savefile); try { fos.write(data); @@ -599,7 +602,7 @@ public class ExcelImportService extends ImportBaseService { IOUtils.closeQuietly(fos); } setValues(excelParams.get(titleString), object, - getSaveUrl(excelParams.get(titleString), object) + "/" + fileName); + getSaveUrl(excelParams.get(titleString), object) + File.separator + fileName); } else { setValues(excelParams.get(titleString), object, data); } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java index e29c7a2..4b7078c 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java @@ -214,14 +214,14 @@ public class ImportBaseService { public void saveThisExcel(ImportParams params, Class pojoClass, boolean isXSSFWorkbook, Workbook book) throws Exception { - String path = PoiPublicUtil.getWebRootPath(getSaveExcelUrl(params, pojoClass)); + String path = getSaveExcelUrl(params, pojoClass); File savefile = new File(path); if (!savefile.exists()) { savefile.mkdirs(); } SimpleDateFormat format = new SimpleDateFormat("yyyMMddHHmmss"); FileOutputStream fos = new FileOutputStream( - path + "/" + format.format(new Date()) + "_" + Math.round(Math.random() * 100000) + path + File.separator + format.format(new Date()) + "_" + Math.round(Math.random() * 100000) + (isXSSFWorkbook == true ? ".xlsx" : ".xls")); book.write(fos); IOUtils.closeQuietly(fos); @@ -237,9 +237,9 @@ public class ImportBaseService { */ public String getSaveExcelUrl(ImportParams params, Class pojoClass) throws Exception { String url = ""; - if ("upload/excelUpload".equals(params.getSaveUrl())) { + if (ImportParams.SAVE_URL.equals(params.getSaveUrl())) { url = pojoClass.getName().split("\\.")[pojoClass.getName().split("\\.").length - 1]; - return params.getSaveUrl() + "/" + url; + return params.getSaveUrl() + File.separator + url; } return params.getSaveUrl(); } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/sax/parse/SaxRowRead.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/sax/parse/SaxRowRead.java index 2990a6e..5c3beb1 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/sax/parse/SaxRowRead.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/sax/parse/SaxRowRead.java @@ -129,7 +129,7 @@ public class SaxRowRead extends ImportBaseService implements ISaxRowRead { */ private void addListData(List datas) throws Exception { // 判断是集合元素还是不是集合元素,如果是就继续加入这个集合,不是就创建新的对象 - if ((datas.get(params.getKeyIndex()) == null + if (params.getKeyIndex() != null && (datas.get(params.getKeyIndex()) == null || StringUtils.isEmpty(String.valueOf(datas.get(params.getKeyIndex()).getValue()))) && object != null) { for (ExcelCollectionParams param : excelCollection) { diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiElUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiElUtil.java index cd7cf8e..3833fc5 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiElUtil.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiElUtil.java @@ -59,9 +59,34 @@ public final class PoiElUtil { public static Object eval(String text, Map map) throws Exception { String tempText = new String(text); Object obj = innerEval(text, map); - //如果没有被处理而且这个值找map中存在就处理这个值 - if (tempText.equals(obj.toString()) && map.containsKey(tempText.split("\\.")[0])) { - return PoiPublicUtil.getParamsValue(tempText, map); + //如果没有被处理而且这个值找map中存在就处理这个值,找不到就返回空字符串 + if (tempText.equals(obj.toString())) { + if(map.containsKey(tempText.split("\\.")[0])){ + return PoiPublicUtil.getParamsValue(tempText, map); + }else{ + return ""; + } + } + return obj; + } + + /** + * 解析字符串,支持 le,fd,fn,!if,三目 找不到返回原值 + * @param text + * @param map + * @return + * @throws Exception + */ + public static Object evalNoParse(String text, Map map) throws Exception { + String tempText = new String(text); + Object obj = innerEval(text, map); + //如果没有被处理而且这个值找map中存在就处理这个值,找不到就返回空字符串 + if (tempText.equals(obj.toString())) { + if(map.containsKey(tempText.split("\\.")[0])){ + return PoiPublicUtil.getParamsValue(tempText, map); + }else{ + return obj; + } } return obj; } @@ -126,8 +151,8 @@ public final class PoiElUtil { return Boolean.valueOf(PoiPublicUtil.getParamsValue(keys[0], map).toString()); } if (keys.length == 3) { - Object first = eval(keys[0], map); - Object second = eval(keys[2], map); + Object first = evalNoParse(keys[0], map); + Object second = evalNoParse(keys[2], map); return PoiFunctionUtil.isTrue(first, keys[1], second); } throw new ExcelExportException("判断参数不对"); @@ -242,19 +267,19 @@ public final class PoiElUtil { for (int i = 1; i < keys.length-1; i++) { trinocular += ":" + keys[i]; } - first = eval(trinocular, map); - second = eval(keys[keys.length-1].trim(), map); + first = evalNoParse(trinocular, map); + second = evalNoParse(keys[keys.length-1].trim(), map); } else { - first = eval(keys[0].trim(), map); + first = evalNoParse(keys[0].trim(), map); String trinocular = keys[1]; for (int i = 2; i < keys.length; i++) { trinocular += ":" + keys[i]; } - second = eval(trinocular, map); + second = evalNoParse(trinocular, map); } }else{ - first = eval(keys[0].trim(), map); - second = eval(keys[1].trim(), map); + first = evalNoParse(keys[0].trim(), map); + second = evalNoParse(keys[1].trim(), map); } return isTrue(testText.split(" "), map) ? first : second; } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiPublicUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiPublicUtil.java index 5c3b7e8..4bc172d 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiPublicUtil.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiPublicUtil.java @@ -220,17 +220,6 @@ public final class PoiPublicUtil { return sheetIndexPicMap; } - public static String getWebRootPath(String filePath) { - try { - String path = PoiPublicUtil.class.getClassLoader().getResource("").toURI().getPath(); - path = path.replace("WEB-INF/classes/", ""); - path = path.replace("file:/", ""); - return path + filePath; - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } - } - /** * 判断是不是集合的实现类 * @@ -460,7 +449,7 @@ public final class PoiPublicUtil { * @param currentText */ public static void setWordText(XWPFRun currentRun, String currentText) { - if (StringUtils.isNotBlank(currentText)) { + if (StringUtils.isNotEmpty(currentText)) { String[] tempArr = currentText.split("\r\n"); for (int i = 0, le = tempArr.length - 1; i < le; i++) { currentRun.setText(tempArr[i], i); diff --git a/easypoi-web/pom.xml b/easypoi-web/pom.xml index f424060..2d3f0be 100644 --- a/easypoi-web/pom.xml +++ b/easypoi-web/pom.xml @@ -4,7 +4,7 @@ cn.afterturn easypoi - 3.1.0 + 3.1.1-SNAPSHOT easypoi-web diff --git a/pom.xml b/pom.xml index 1a6820f..a274ac7 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 cn.afterturn easypoi - 3.1.0 + 3.1.1-SNAPSHOT pom easypoi @@ -45,7 +45,7 @@ - 3.1.0 + 3.1.1-SNAPSHOT 3.15 2.9.1 16.0.1 diff --git a/文档/2. Excel 注解版/2.5 Map导入,自由发挥 b/文档/2. Excel 注解版/2.5 Map导入,自由发挥 index 42fcf20..21d219e 100644 --- a/文档/2. Excel 注解版/2.5 Map导入,自由发挥 +++ b/文档/2. Excel 注解版/2.5 Map导入,自由发挥 @@ -9,7 +9,7 @@ params.setDataHandler(new MapImportHandler()); long start = new Date().getTime(); List> list = ExcelImportUtil.importExcel( - new File(PoiPublicUtil.getWebRootPath("import/check.xls")), Map.class, params); + new File(FileUtilTest.getWebRootPath("import/check.xls")), Map.class, params); ``` 导入后,处理每个map,然后入库完美的解决了老师的需求,简单更快捷,和bean导入基础没有区别,省去了bean的构造时间