@@ -4,7 +4,7 @@ | |||||
<parent> | <parent> | ||||
<groupId>cn.afterturn</groupId> | <groupId>cn.afterturn</groupId> | ||||
<artifactId>easypoi</artifactId> | <artifactId>easypoi</artifactId> | ||||
<version>3.1.0</version> | |||||
<version>3.1.1-SNAPSHOT</version> | |||||
</parent> | </parent> | ||||
<artifactId>easypoi-annotation</artifactId> | <artifactId>easypoi-annotation</artifactId> | ||||
<description>基础注解类,解耦合</description> | <description>基础注解类,解耦合</description> |
@@ -121,7 +121,7 @@ public @interface Excel { | |||||
* 导入路径,如果是图片可以填写,默认是upload/className/ IconEntity这个类对应的就是upload/Icon/ | * 导入路径,如果是图片可以填写,默认是upload/className/ IconEntity这个类对应的就是upload/Icon/ | ||||
* | * | ||||
*/ | */ | ||||
public String savePath() default "upload"; | |||||
public String savePath() default "/excel/upload/img"; | |||||
/** | /** | ||||
* 导出类型 1 是文本 2 是图片,3 是函数,10 是数字 默认是文本 | * 导出类型 1 是文本 2 是图片,3 是函数,10 是数字 默认是文本 | ||||
@@ -4,7 +4,7 @@ | |||||
<parent> | <parent> | ||||
<groupId>cn.afterturn</groupId> | <groupId>cn.afterturn</groupId> | ||||
<artifactId>easypoi</artifactId> | <artifactId>easypoi</artifactId> | ||||
<version>3.1.0</version> | |||||
<version>3.1.1-SNAPSHOT</version> | |||||
</parent> | </parent> | ||||
<artifactId>easypoi-base</artifactId> | <artifactId>easypoi-base</artifactId> | ||||
<dependencies> | <dependencies> | ||||
@@ -55,11 +55,6 @@ public class FileLoaderImpl implements IFileLoader { | |||||
} catch (FileNotFoundException e) { | } catch (FileNotFoundException e) { | ||||
//获取项目文件 | //获取项目文件 | ||||
fileis = FileLoaderImpl.class.getClassLoader().getResourceAsStream(url); | fileis = FileLoaderImpl.class.getClassLoader().getResourceAsStream(url); | ||||
if (fileis == null) { | |||||
//最后再拿相对文件路径 | |||||
String path = PoiPublicUtil.getWebRootPath(url); | |||||
fileis = new FileInputStream(path); | |||||
} | |||||
} | } | ||||
} | } | ||||
@@ -26,6 +26,9 @@ import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler; | |||||
*/ | */ | ||||
@SuppressWarnings("rawtypes") | @SuppressWarnings("rawtypes") | ||||
public class ImportParams extends ExcelBaseParams { | public class ImportParams extends ExcelBaseParams { | ||||
public static final String SAVE_URL = "/excel/upload/excelUpload"; | |||||
/** | /** | ||||
* 表格标题行数,默认0 | * 表格标题行数,默认0 | ||||
*/ | */ | ||||
@@ -73,7 +76,7 @@ public class ImportParams extends ExcelBaseParams { | |||||
* 保存上传的Excel目录,默认是 如 TestEntity这个类保存路径就是 | * 保存上传的Excel目录,默认是 如 TestEntity这个类保存路径就是 | ||||
* upload/excelUpload/Test/yyyyMMddHHmss_***** 保存名称上传时间_五位随机数 | * upload/excelUpload/Test/yyyyMMddHHmss_***** 保存名称上传时间_五位随机数 | ||||
*/ | */ | ||||
private String saveUrl = "upload/excelUpload"; | |||||
private String saveUrl = SAVE_URL; | |||||
/** | /** | ||||
* 最后的无效行数 | * 最后的无效行数 | ||||
*/ | */ | ||||
@@ -23,6 +23,8 @@ import java.util.List; | |||||
* @version 1.0 2013年8月24日 | * @version 1.0 2013年8月24日 | ||||
*/ | */ | ||||
public class ExcelImportEntity extends ExcelBaseEntity { | public class ExcelImportEntity extends ExcelBaseEntity { | ||||
public final static String IMG_SAVE_PATH = "/excel/upload/img"; | |||||
/** | /** | ||||
* 对应 Collection NAME | * 对应 Collection NAME | ||||
*/ | */ | ||||
@@ -278,7 +278,7 @@ public class ExcelToHtmlService { | |||||
/*String fileName = "pic" + Math.round(Math.random() * 100000000000L); | /*String fileName = "pic" + Math.round(Math.random() * 100000000000L); | ||||
fileName += "." + PoiPublicUtil.getFileExtendName(data); | fileName += "." + PoiPublicUtil.getFileExtendName(data); | ||||
if (!imageCachePath.startsWith("/") && !imageCachePath.contains(":")) { | if (!imageCachePath.startsWith("/") && !imageCachePath.contains(":")) { | ||||
imageCachePath = PoiPublicUtil.getWebRootPath(imageCachePath); | |||||
imageCachePath = FileUtilTest.getWebRootPath(imageCachePath); | |||||
} | } | ||||
File savefile = new File(imageCachePath + "/" + today); | File savefile = new File(imageCachePath + "/" + today); | ||||
if (!savefile.exists()) { | if (!savefile.exists()) { | ||||
@@ -309,7 +309,7 @@ public class CellValueService { | |||||
} | } | ||||
return String.valueOf(result); | return String.valueOf(result); | ||||
} | } | ||||
if (clazz.isEnum()) { | |||||
if (clazz != null && clazz.isEnum()) { | |||||
if (StringUtils.isNotEmpty(entity.getEnumImportMethod())) { | if (StringUtils.isNotEmpty(entity.getEnumImportMethod())) { | ||||
return PoiReflectorUtil.fromCache(clazz).execEnumStaticMethod(entity.getEnumImportMethod(),result); | return PoiReflectorUtil.fromCache(clazz).execEnumStaticMethod(entity.getEnumImportMethod(),result); | ||||
} else { | } else { | ||||
@@ -145,14 +145,14 @@ public class ExcelImportService extends ImportBaseService { | |||||
*/ | */ | ||||
private String getSaveUrl(ExcelImportEntity excelImportEntity, Object object) throws Exception { | private String getSaveUrl(ExcelImportEntity excelImportEntity, Object object) throws Exception { | ||||
String url = ""; | String url = ""; | ||||
if ("upload".equals(excelImportEntity.getSaveUrl())) { | |||||
if (ExcelImportEntity.IMG_SAVE_PATH.equals(excelImportEntity.getSaveUrl())) { | |||||
if (excelImportEntity.getMethods() != null | if (excelImportEntity.getMethods() != null | ||||
&& excelImportEntity.getMethods().size() > 0) { | && excelImportEntity.getMethods().size() > 0) { | ||||
object = getFieldBySomeMethod(excelImportEntity.getMethods(), object); | object = getFieldBySomeMethod(excelImportEntity.getMethods(), object); | ||||
} | } | ||||
url = object.getClass().getName() | url = object.getClass().getName() | ||||
.split("\\.")[object.getClass().getName().split("\\.").length - 1]; | .split("\\.")[object.getClass().getName().split("\\.").length - 1]; | ||||
return excelImportEntity.getSaveUrl() + "/" + url; | |||||
return excelImportEntity.getSaveUrl() + File.separator + url; | |||||
} | } | ||||
return excelImportEntity.getSaveUrl(); | return excelImportEntity.getSaveUrl(); | ||||
} | } | ||||
@@ -186,6 +186,10 @@ public class ExcelImportService extends ImportBaseService { | |||||
for (int i = 0; i < params.getStartRows(); i++) { | for (int i = 0; i < params.getStartRows(); i++) { | ||||
rows.next(); | rows.next(); | ||||
} | } | ||||
//判断index 和集合,集合情况默认为第一列 | |||||
if (excelCollection.size() > 0 && params.getKeyIndex() == null) { | |||||
params.setKeyIndex(0); | |||||
} | |||||
while (rows.hasNext() | while (rows.hasNext() | ||||
&& (row == null | && (row == null | ||||
|| sheet.getLastRowNum() - row.getRowNum() > params.getLastOfInvalidRow())) { | || sheet.getLastRowNum() - row.getRowNum() > params.getLastOfInvalidRow())) { | ||||
@@ -585,13 +589,12 @@ public class ExcelImportService extends ImportBaseService { | |||||
String fileName = "pic" + Math.round(Math.random() * 100000000000L); | String fileName = "pic" + Math.round(Math.random() * 100000000000L); | ||||
fileName += "." + PoiPublicUtil.getFileExtendName(data); | fileName += "." + PoiPublicUtil.getFileExtendName(data); | ||||
if (excelParams.get(titleString).getSaveType() == 1) { | 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); | File savefile = new File(path); | ||||
if (!savefile.exists()) { | if (!savefile.exists()) { | ||||
savefile.mkdirs(); | savefile.mkdirs(); | ||||
} | } | ||||
savefile = new File(path + "/" + fileName); | |||||
savefile = new File(path + File.separator + fileName); | |||||
FileOutputStream fos = new FileOutputStream(savefile); | FileOutputStream fos = new FileOutputStream(savefile); | ||||
try { | try { | ||||
fos.write(data); | fos.write(data); | ||||
@@ -599,7 +602,7 @@ public class ExcelImportService extends ImportBaseService { | |||||
IOUtils.closeQuietly(fos); | IOUtils.closeQuietly(fos); | ||||
} | } | ||||
setValues(excelParams.get(titleString), object, | setValues(excelParams.get(titleString), object, | ||||
getSaveUrl(excelParams.get(titleString), object) + "/" + fileName); | |||||
getSaveUrl(excelParams.get(titleString), object) + File.separator + fileName); | |||||
} else { | } else { | ||||
setValues(excelParams.get(titleString), object, data); | setValues(excelParams.get(titleString), object, data); | ||||
} | } | ||||
@@ -214,14 +214,14 @@ public class ImportBaseService { | |||||
public void saveThisExcel(ImportParams params, Class<?> pojoClass, boolean isXSSFWorkbook, | public void saveThisExcel(ImportParams params, Class<?> pojoClass, boolean isXSSFWorkbook, | ||||
Workbook book) throws Exception { | Workbook book) throws Exception { | ||||
String path = PoiPublicUtil.getWebRootPath(getSaveExcelUrl(params, pojoClass)); | |||||
String path = getSaveExcelUrl(params, pojoClass); | |||||
File savefile = new File(path); | File savefile = new File(path); | ||||
if (!savefile.exists()) { | if (!savefile.exists()) { | ||||
savefile.mkdirs(); | savefile.mkdirs(); | ||||
} | } | ||||
SimpleDateFormat format = new SimpleDateFormat("yyyMMddHHmmss"); | SimpleDateFormat format = new SimpleDateFormat("yyyMMddHHmmss"); | ||||
FileOutputStream fos = new FileOutputStream( | 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")); | + (isXSSFWorkbook == true ? ".xlsx" : ".xls")); | ||||
book.write(fos); | book.write(fos); | ||||
IOUtils.closeQuietly(fos); | IOUtils.closeQuietly(fos); | ||||
@@ -237,9 +237,9 @@ public class ImportBaseService { | |||||
*/ | */ | ||||
public String getSaveExcelUrl(ImportParams params, Class<?> pojoClass) throws Exception { | public String getSaveExcelUrl(ImportParams params, Class<?> pojoClass) throws Exception { | ||||
String url = ""; | String url = ""; | ||||
if ("upload/excelUpload".equals(params.getSaveUrl())) { | |||||
if (ImportParams.SAVE_URL.equals(params.getSaveUrl())) { | |||||
url = pojoClass.getName().split("\\.")[pojoClass.getName().split("\\.").length - 1]; | url = pojoClass.getName().split("\\.")[pojoClass.getName().split("\\.").length - 1]; | ||||
return params.getSaveUrl() + "/" + url; | |||||
return params.getSaveUrl() + File.separator + url; | |||||
} | } | ||||
return params.getSaveUrl(); | return params.getSaveUrl(); | ||||
} | } | ||||
@@ -129,7 +129,7 @@ public class SaxRowRead extends ImportBaseService implements ISaxRowRead { | |||||
*/ | */ | ||||
private void addListData(List<SaxReadCellEntity> datas) throws Exception { | private void addListData(List<SaxReadCellEntity> 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()))) | || StringUtils.isEmpty(String.valueOf(datas.get(params.getKeyIndex()).getValue()))) | ||||
&& object != null) { | && object != null) { | ||||
for (ExcelCollectionParams param : excelCollection) { | for (ExcelCollectionParams param : excelCollection) { | ||||
@@ -59,9 +59,34 @@ public final class PoiElUtil { | |||||
public static Object eval(String text, Map<String, Object> map) throws Exception { | public static Object eval(String text, Map<String, Object> map) throws Exception { | ||||
String tempText = new String(text); | String tempText = new String(text); | ||||
Object obj = innerEval(text, map); | 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<String, Object> 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; | return obj; | ||||
} | } | ||||
@@ -126,8 +151,8 @@ public final class PoiElUtil { | |||||
return Boolean.valueOf(PoiPublicUtil.getParamsValue(keys[0], map).toString()); | return Boolean.valueOf(PoiPublicUtil.getParamsValue(keys[0], map).toString()); | ||||
} | } | ||||
if (keys.length == 3) { | 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); | return PoiFunctionUtil.isTrue(first, keys[1], second); | ||||
} | } | ||||
throw new ExcelExportException("判断参数不对"); | throw new ExcelExportException("判断参数不对"); | ||||
@@ -242,19 +267,19 @@ public final class PoiElUtil { | |||||
for (int i = 1; i < keys.length-1; i++) { | for (int i = 1; i < keys.length-1; i++) { | ||||
trinocular += ":" + keys[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 { | } else { | ||||
first = eval(keys[0].trim(), map); | |||||
first = evalNoParse(keys[0].trim(), map); | |||||
String trinocular = keys[1]; | String trinocular = keys[1]; | ||||
for (int i = 2; i < keys.length; i++) { | for (int i = 2; i < keys.length; i++) { | ||||
trinocular += ":" + keys[i]; | trinocular += ":" + keys[i]; | ||||
} | } | ||||
second = eval(trinocular, map); | |||||
second = evalNoParse(trinocular, map); | |||||
} | } | ||||
}else{ | }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; | return isTrue(testText.split(" "), map) ? first : second; | ||||
} | } | ||||
@@ -220,17 +220,6 @@ public final class PoiPublicUtil { | |||||
return sheetIndexPicMap; | 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 | * @param currentText | ||||
*/ | */ | ||||
public static void setWordText(XWPFRun currentRun, String currentText) { | public static void setWordText(XWPFRun currentRun, String currentText) { | ||||
if (StringUtils.isNotBlank(currentText)) { | |||||
if (StringUtils.isNotEmpty(currentText)) { | |||||
String[] tempArr = currentText.split("\r\n"); | String[] tempArr = currentText.split("\r\n"); | ||||
for (int i = 0, le = tempArr.length - 1; i < le; i++) { | for (int i = 0, le = tempArr.length - 1; i < le; i++) { | ||||
currentRun.setText(tempArr[i], i); | currentRun.setText(tempArr[i], i); | ||||
@@ -4,7 +4,7 @@ | |||||
<parent> | <parent> | ||||
<groupId>cn.afterturn</groupId> | <groupId>cn.afterturn</groupId> | ||||
<artifactId>easypoi</artifactId> | <artifactId>easypoi</artifactId> | ||||
<version>3.1.0</version> | |||||
<version>3.1.1-SNAPSHOT</version> | |||||
</parent> | </parent> | ||||
<artifactId>easypoi-web</artifactId> | <artifactId>easypoi-web</artifactId> | ||||
<dependencies> | <dependencies> | ||||
@@ -3,7 +3,7 @@ | |||||
<modelVersion>4.0.0</modelVersion> | <modelVersion>4.0.0</modelVersion> | ||||
<groupId>cn.afterturn</groupId> | <groupId>cn.afterturn</groupId> | ||||
<artifactId>easypoi</artifactId> | <artifactId>easypoi</artifactId> | ||||
<version>3.1.0</version> | |||||
<version>3.1.1-SNAPSHOT</version> | |||||
<packaging>pom</packaging> | <packaging>pom</packaging> | ||||
<name>easypoi</name> | <name>easypoi</name> | ||||
<modules> | <modules> | ||||
@@ -45,7 +45,7 @@ | |||||
</organization> | </organization> | ||||
<properties> | <properties> | ||||
<sub.version>3.1.0</sub.version> | |||||
<sub.version>3.1.1-SNAPSHOT</sub.version> | |||||
<poi.version>3.15</poi.version> | <poi.version>3.15</poi.version> | ||||
<xerces.version>2.9.1</xerces.version> | <xerces.version>2.9.1</xerces.version> | ||||
<guava.version>16.0.1</guava.version> | <guava.version>16.0.1</guava.version> | ||||
@@ -9,7 +9,7 @@ | |||||
params.setDataHandler(new MapImportHandler()); | params.setDataHandler(new MapImportHandler()); | ||||
long start = new Date().getTime(); | long start = new Date().getTime(); | ||||
List<Map<String, Object>> list = ExcelImportUtil.importExcel( | List<Map<String, Object>> 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的构造时间 | 导入后,处理每个map,然后入库完美的解决了老师的需求,简单更快捷,和bean导入基础没有区别,省去了bean的构造时间 | ||||