但是不支持图片,同时工解析,让大家可以自己解析数据4.1.3.A
@@ -25,6 +25,10 @@ | |||||
<groupId>org.apache.poi</groupId> | <groupId>org.apache.poi</groupId> | ||||
<artifactId>poi-scratchpad</artifactId> | <artifactId>poi-scratchpad</artifactId> | ||||
</dependency> | </dependency> | ||||
<dependency> | |||||
<groupId>xerces</groupId> | |||||
<artifactId>xerces</artifactId> | |||||
</dependency> | |||||
<!-- google 工具类 --> | <!-- google 工具类 --> | ||||
<dependency> | <dependency> | ||||
@@ -9,6 +9,8 @@ import java.util.List; | |||||
import org.jeecgframework.poi.excel.entity.ImportParams; | import org.jeecgframework.poi.excel.entity.ImportParams; | ||||
import org.jeecgframework.poi.excel.entity.result.ExcelImportResult; | import org.jeecgframework.poi.excel.entity.result.ExcelImportResult; | ||||
import org.jeecgframework.poi.excel.imports.ExcelImportServer; | 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.slf4j.Logger; | import org.slf4j.Logger; | ||||
import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
@@ -40,12 +42,12 @@ public class ExcelImportUtil { | |||||
in = new FileInputStream(file); | in = new FileInputStream(file); | ||||
result = new ExcelImportServer().importExcelByIs(in, pojoClass, params).getList(); | result = new ExcelImportServer().importExcelByIs(in, pojoClass, params).getList(); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
LOGGER.error(e.getMessage(),e); | |||||
LOGGER.error(e.getMessage(), e); | |||||
} finally { | } finally { | ||||
try { | try { | ||||
in.close(); | in.close(); | ||||
} catch (IOException e) { | } catch (IOException e) { | ||||
LOGGER.error(e.getMessage(),e); | |||||
LOGGER.error(e.getMessage(), e); | |||||
} | } | ||||
} | } | ||||
return result; | return result; | ||||
@@ -60,8 +62,8 @@ public class ExcelImportUtil { | |||||
* @return | * @return | ||||
* @throws Exception | * @throws Exception | ||||
*/ | */ | ||||
public static <T> List<T> importExcelByIs(InputStream inputstream, Class<?> pojoClass, | |||||
ImportParams params) throws Exception { | |||||
public static <T> List<T> importExcel(InputStream inputstream, Class<?> pojoClass, | |||||
ImportParams params) throws Exception { | |||||
return new ExcelImportServer().importExcelByIs(inputstream, pojoClass, params).getList(); | return new ExcelImportServer().importExcelByIs(inputstream, pojoClass, params).getList(); | ||||
} | } | ||||
@@ -74,9 +76,8 @@ public class ExcelImportUtil { | |||||
* @return | * @return | ||||
* @throws Exception | * @throws Exception | ||||
*/ | */ | ||||
public static ExcelImportResult importExcelByIsAndVerify(InputStream inputstream, | |||||
Class<?> pojoClass, ImportParams params) | |||||
throws Exception { | |||||
public static ExcelImportResult importExcelVerify(InputStream inputstream, Class<?> pojoClass, | |||||
ImportParams params) throws Exception { | |||||
return new ExcelImportServer().importExcelByIs(inputstream, pojoClass, params); | return new ExcelImportServer().importExcelByIs(inputstream, pojoClass, params); | ||||
} | } | ||||
@@ -96,15 +97,75 @@ public class ExcelImportUtil { | |||||
in = new FileInputStream(file); | in = new FileInputStream(file); | ||||
return new ExcelImportServer().importExcelByIs(in, pojoClass, params); | return new ExcelImportServer().importExcelByIs(in, pojoClass, params); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
LOGGER.error(e.getMessage(),e); | |||||
LOGGER.error(e.getMessage(), e); | |||||
} finally { | } finally { | ||||
try { | try { | ||||
in.close(); | in.close(); | ||||
} catch (IOException e) { | } catch (IOException e) { | ||||
LOGGER.error(e.getMessage(),e); | |||||
LOGGER.error(e.getMessage(), e); | |||||
} | } | ||||
} | } | ||||
return null; | return null; | ||||
} | } | ||||
/** | |||||
* Excel 通过SAX解析方法,适合大数据导入,不支持图片 | |||||
* 导入 数据源本地文件,不返回校验结果 导入 字 段类型 Integer,Long,Double,Date,String,Boolean | |||||
* | |||||
* @param file | |||||
* @param pojoClass | |||||
* @param params | |||||
* @return | |||||
* @throws Exception | |||||
*/ | |||||
public static <T> List<T> importExcelBySax(File file, Class<?> pojoClass, ImportParams params) { | |||||
return new SaxReadExcel().readExcel(file, pojoClass, params, null); | |||||
} | |||||
/** | |||||
* Excel 通过SAX解析方法,适合大数据导入,不支持图片 | |||||
* 导入 数据源IO流,不返回校验结果 导入 字段类型 Integer,Long,Double,Date,String,Boolean | |||||
* | |||||
* @param file | |||||
* @param pojoClass | |||||
* @param params | |||||
* @return | |||||
* @throws Exception | |||||
*/ | |||||
public static <T> List<T> importExcelBySax(InputStream inputstream, Class<?> pojoClass, | |||||
ImportParams params) throws Exception { | |||||
return new SaxReadExcel().readExcel(inputstream, pojoClass, params, null); | |||||
} | |||||
/** | |||||
* Excel 通过SAX解析方法,适合大数据导入,不支持图片 | |||||
* 导入 数据源本地文件,不返回校验结果 导入 字 段类型 Integer,Long,Double,Date,String,Boolean | |||||
* | |||||
* @param file | |||||
* @param pojoClass | |||||
* @param params | |||||
* @return | |||||
* @throws Exception | |||||
*/ | |||||
public static <T> List<T> importExcelBySax(File file, Class<?> pojoClass, ImportParams params, | |||||
ISaxRowRead rowRead) { | |||||
return new SaxReadExcel().readExcel(file, pojoClass, params, rowRead); | |||||
} | |||||
/** | |||||
* Excel 通过SAX解析方法,适合大数据导入,不支持图片 | |||||
* 导入 数据源IO流,不返回校验结果 导入 字段类型 Integer,Long,Double,Date,String,Boolean | |||||
* | |||||
* @param file | |||||
* @param pojoClass | |||||
* @param params | |||||
* @return | |||||
* @throws Exception | |||||
*/ | |||||
public static <T> List<T> importExcelBySax(InputStream inputstream, Class<?> pojoClass, | |||||
ImportParams params, ISaxRowRead rowRead) | |||||
throws Exception { | |||||
return new SaxReadExcel().readExcel(inputstream, pojoClass, params, rowRead); | |||||
} | |||||
} | } |
@@ -38,4 +38,9 @@ public class SaxReadCellEntity { | |||||
this.value = value; | this.value = value; | ||||
} | } | ||||
@Override | |||||
public String toString() { | |||||
return "[type=" + cellType.toString() + ",value=" + value + "]"; | |||||
} | |||||
} | } |
@@ -20,7 +20,6 @@ import org.jeecgframework.poi.excel.entity.enmus.ExcelType; | |||||
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | ||||
import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | ||||
import org.jeecgframework.poi.excel.export.base.ExcelExportBase; | import org.jeecgframework.poi.excel.export.base.ExcelExportBase; | ||||
import org.jeecgframework.poi.excel.export.styler.ExcelExportStylerDefaultImpl; | |||||
import org.jeecgframework.poi.excel.export.styler.IExcelExportStyler; | import org.jeecgframework.poi.excel.export.styler.IExcelExportStyler; | ||||
import org.jeecgframework.poi.exception.excel.ExcelExportException; | import org.jeecgframework.poi.exception.excel.ExcelExportException; | ||||
import org.jeecgframework.poi.exception.excel.enums.ExcelExportEnum; | import org.jeecgframework.poi.exception.excel.enums.ExcelExportEnum; | ||||
@@ -13,6 +13,7 @@ import java.util.Map; | |||||
import org.apache.commons.lang.StringUtils; | import org.apache.commons.lang.StringUtils; | ||||
import org.apache.poi.ss.usermodel.Cell; | import org.apache.poi.ss.usermodel.Cell; | ||||
import org.jeecgframework.poi.excel.entity.params.ExcelImportEntity; | import org.jeecgframework.poi.excel.entity.params.ExcelImportEntity; | ||||
import org.jeecgframework.poi.excel.entity.sax.SaxReadCellEntity; | |||||
import org.jeecgframework.poi.exception.excel.ExcelImportException; | import org.jeecgframework.poi.exception.excel.ExcelImportException; | ||||
import org.jeecgframework.poi.exception.excel.enums.ExcelImportEnum; | import org.jeecgframework.poi.exception.excel.enums.ExcelImportEnum; | ||||
import org.jeecgframework.poi.handler.inter.IExcelDataHandler; | import org.jeecgframework.poi.handler.inter.IExcelDataHandler; | ||||
@@ -28,7 +29,7 @@ import org.slf4j.LoggerFactory; | |||||
*/ | */ | ||||
public class CellValueServer { | public class CellValueServer { | ||||
private static final Logger logger = LoggerFactory.getLogger(CellValueServer.class); | |||||
private static final Logger LOGGER = LoggerFactory.getLogger(CellValueServer.class); | |||||
private List<String> hanlderList = null; | private List<String> hanlderList = null; | ||||
@@ -79,7 +80,7 @@ public class CellValueServer { | |||||
try { | try { | ||||
return format.parse(value); | return format.parse(value); | ||||
} catch (ParseException e) { | } catch (ParseException e) { | ||||
logger.error("时间格式化失败,格式化:{},值:{}", entity.getFormat(), value); | |||||
LOGGER.error("时间格式化失败,格式化:{},值:{}", entity.getFormat(), value); | |||||
throw new ExcelImportException(ExcelImportEnum.GET_VALUE_ERROR); | throw new ExcelImportException(ExcelImportEnum.GET_VALUE_ERROR); | ||||
} | } | ||||
} | } | ||||
@@ -108,6 +109,29 @@ public class CellValueServer { | |||||
return getValueByType(xclass, result); | return getValueByType(xclass, result); | ||||
} | } | ||||
/** | |||||
* 获取cell值 | |||||
* @param dataHanlder | |||||
* @param object | |||||
* @param entity | |||||
* @param excelParams | |||||
* @param titleString | |||||
* @return | |||||
*/ | |||||
public Object getValue(IExcelDataHandler dataHanlder, Object object, | |||||
SaxReadCellEntity cellEntity, | |||||
Map<String, ExcelImportEntity> excelParams, String titleString) { | |||||
ExcelImportEntity entity = excelParams.get(titleString); | |||||
Method setMethod = entity.getMethods() != null && entity.getMethods().size() > 0 ? entity | |||||
.getMethods().get(entity.getMethods().size() - 1) : entity.getMethod(); | |||||
Type[] ts = setMethod.getGenericParameterTypes(); | |||||
String xclass = ts[0].toString(); | |||||
Object result = cellEntity.getValue(); | |||||
result = replaceValue(entity.getReplace(), result); | |||||
result = hanlderValue(dataHanlder, object, result, titleString); | |||||
return getValueByType(xclass, result); | |||||
} | |||||
/** | /** | ||||
* 根据返回类型获取返回值 | * 根据返回类型获取返回值 | ||||
* | * | ||||
@@ -140,6 +164,7 @@ public class CellValueServer { | |||||
} | } | ||||
return result; | return result; | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
LOGGER.error(e.getMessage(),e); | |||||
throw new ExcelImportException(ExcelImportEnum.GET_VALUE_ERROR); | throw new ExcelImportException(ExcelImportEnum.GET_VALUE_ERROR); | ||||
} | } | ||||
} | } | ||||
@@ -39,6 +39,7 @@ import org.jeecgframework.poi.excel.entity.params.ExcelImportEntity; | |||||
import org.jeecgframework.poi.excel.entity.params.ExcelVerifyEntity; | import org.jeecgframework.poi.excel.entity.params.ExcelVerifyEntity; | ||||
import org.jeecgframework.poi.excel.entity.result.ExcelImportResult; | import org.jeecgframework.poi.excel.entity.result.ExcelImportResult; | ||||
import org.jeecgframework.poi.excel.entity.result.ExcelVerifyHanlderResult; | import org.jeecgframework.poi.excel.entity.result.ExcelVerifyHanlderResult; | ||||
import org.jeecgframework.poi.excel.imports.base.ImportBaseService; | |||||
import org.jeecgframework.poi.excel.imports.verifys.VerifyHandlerServer; | import org.jeecgframework.poi.excel.imports.verifys.VerifyHandlerServer; | ||||
import org.jeecgframework.poi.util.POIPublicUtil; | import org.jeecgframework.poi.util.POIPublicUtil; | ||||
import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
@@ -51,7 +52,7 @@ import org.slf4j.LoggerFactory; | |||||
* @date 2014年6月26日 下午9:20:51 | * @date 2014年6月26日 下午9:20:51 | ||||
*/ | */ | ||||
@SuppressWarnings({ "rawtypes", "unchecked", "hiding" }) | @SuppressWarnings({ "rawtypes", "unchecked", "hiding" }) | ||||
public class ExcelImportServer { | |||||
public class ExcelImportServer extends ImportBaseService { | |||||
private final static Logger LOGGER = LoggerFactory.getLogger(ExcelImportServer.class); | private final static Logger LOGGER = LoggerFactory.getLogger(ExcelImportServer.class); | ||||
@@ -66,39 +67,6 @@ public class ExcelImportServer { | |||||
this.verifyHandlerServer = new VerifyHandlerServer(); | this.verifyHandlerServer = new VerifyHandlerServer(); | ||||
} | } | ||||
/** | |||||
* 把这个注解解析放到类型对象中 | |||||
* | |||||
* @param targetId | |||||
* @param field | |||||
* @param excelEntity | |||||
* @param pojoClass | |||||
* @param getMethods | |||||
* @param temp | |||||
* @throws Exception | |||||
*/ | |||||
private void addEntityToMap(String targetId, Field field, ExcelImportEntity excelEntity, | |||||
Class<?> pojoClass, List<Method> getMethods, | |||||
Map<String, ExcelImportEntity> temp) throws Exception { | |||||
Excel excel = field.getAnnotation(Excel.class); | |||||
excelEntity = new ExcelImportEntity(); | |||||
excelEntity.setType(excel.type()); | |||||
excelEntity.setSaveUrl(excel.savePath()); | |||||
excelEntity.setSaveType(excel.imageType()); | |||||
excelEntity.setReplace(excel.replace()); | |||||
excelEntity.setDatabaseFormat(excel.databaseFormat()); | |||||
excelEntity.setVerify(getImportVerify(field)); | |||||
getExcelField(targetId, field, excelEntity, excel, pojoClass); | |||||
if (getMethods != null) { | |||||
List<Method> newMethods = new ArrayList<Method>(); | |||||
newMethods.addAll(getMethods); | |||||
newMethods.add(excelEntity.getMethod()); | |||||
excelEntity.setMethods(newMethods); | |||||
} | |||||
temp.put(excelEntity.getName(), excelEntity); | |||||
} | |||||
/*** | /*** | ||||
* 向List里面继续添加元素 | * 向List里面继续添加元素 | ||||
* | * | ||||
@@ -138,141 +106,6 @@ public class ExcelImportServer { | |||||
} | } | ||||
} | } | ||||
/** | |||||
* 获取需要导出的全部字段 | |||||
* | |||||
* | |||||
* @param exclusions | |||||
* @param targetId | |||||
* 目标ID | |||||
* @param fields | |||||
* @param excelCollection | |||||
* @throws Exception | |||||
*/ | |||||
private void getAllExcelField(String targetId, Field[] fields, | |||||
Map<String, ExcelImportEntity> excelParams, | |||||
List<ExcelCollectionParams> excelCollection, Class<?> pojoClass, | |||||
List<Method> getMethods) throws Exception { | |||||
ExcelImportEntity excelEntity = null; | |||||
for (int i = 0; i < fields.length; i++) { | |||||
Field field = fields[i]; | |||||
if (POIPublicUtil.isNotUserExcelUserThis(null, field, targetId)) { | |||||
continue; | |||||
} | |||||
if (POIPublicUtil.isCollection(field.getType())) { | |||||
// 集合对象设置属性 | |||||
ExcelCollectionParams collection = new ExcelCollectionParams(); | |||||
collection.setName(field.getName()); | |||||
Map<String, ExcelImportEntity> temp = new HashMap<String, ExcelImportEntity>(); | |||||
ParameterizedType pt = (ParameterizedType) field.getGenericType(); | |||||
Class<?> clz = (Class<?>) pt.getActualTypeArguments()[0]; | |||||
collection.setType(clz); | |||||
getExcelFieldList(targetId, POIPublicUtil.getClassFields(clz), clz, temp, null); | |||||
collection.setExcelParams(temp); | |||||
excelCollection.add(collection); | |||||
} else if (POIPublicUtil.isJavaClass(field)) { | |||||
addEntityToMap(targetId, field, excelEntity, pojoClass, getMethods, excelParams); | |||||
} else { | |||||
List<Method> newMethods = new ArrayList<Method>(); | |||||
if (getMethods != null) { | |||||
newMethods.addAll(getMethods); | |||||
} | |||||
newMethods.add(POIPublicUtil.getMethod(field.getName(), pojoClass)); | |||||
getAllExcelField(targetId, POIPublicUtil.getClassFields(field.getType()), | |||||
excelParams, excelCollection, field.getType(), newMethods); | |||||
} | |||||
} | |||||
} | |||||
private void getExcelField(String targetId, Field field, ExcelImportEntity excelEntity, | |||||
Excel excel, Class<?> pojoClass) throws Exception { | |||||
excelEntity.setName(getExcelName(excel.name(), targetId)); | |||||
String fieldname = field.getName(); | |||||
excelEntity.setMethod(POIPublicUtil.getMethod(fieldname, pojoClass, field.getType())); | |||||
if (StringUtils.isEmpty(excel.importFormat())) { | |||||
excelEntity.setFormat(excel.format()); | |||||
} else { | |||||
excelEntity.setFormat(excel.importFormat()); | |||||
} | |||||
} | |||||
private void getExcelFieldList(String targetId, Field[] fields, Class<?> pojoClass, | |||||
Map<String, ExcelImportEntity> temp, List<Method> getMethods) | |||||
throws Exception { | |||||
ExcelImportEntity excelEntity = null; | |||||
for (int i = 0; i < fields.length; i++) { | |||||
Field field = fields[i]; | |||||
if (POIPublicUtil.isNotUserExcelUserThis(null, field, targetId)) { | |||||
continue; | |||||
} | |||||
if (POIPublicUtil.isJavaClass(field)) { | |||||
addEntityToMap(targetId, field, excelEntity, pojoClass, getMethods, temp); | |||||
} else { | |||||
List<Method> newMethods = new ArrayList<Method>(); | |||||
if (getMethods != null) { | |||||
newMethods.addAll(getMethods); | |||||
} | |||||
newMethods | |||||
.add(POIPublicUtil.getMethod(field.getName(), pojoClass, field.getType())); | |||||
getExcelFieldList(targetId, POIPublicUtil.getClassFields(field.getType()), | |||||
field.getType(), temp, newMethods); | |||||
} | |||||
} | |||||
} | |||||
/** | |||||
* 判断在这个单元格显示的名称 | |||||
* | |||||
* @param exportName | |||||
* @param targetId | |||||
* @return | |||||
*/ | |||||
private 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; | |||||
} | |||||
private Object getFieldBySomeMethod(List<Method> list, Object t) throws Exception { | |||||
Method m; | |||||
for (int i = 0; i < list.size() - 1; i++) { | |||||
m = list.get(i); | |||||
t = m.invoke(t, new Object[] {}); | |||||
} | |||||
return t; | |||||
} | |||||
/** | |||||
* 获取导入校验参数 | |||||
* | |||||
* @param field | |||||
* @return | |||||
*/ | |||||
private ExcelVerifyEntity getImportVerify(Field field) { | |||||
ExcelVerify verify = field.getAnnotation(ExcelVerify.class); | |||||
if (verify != null) { | |||||
ExcelVerifyEntity entity = new ExcelVerifyEntity(); | |||||
entity.setEmail(verify.isEmail()); | |||||
entity.setInterHandler(verify.interHandler()); | |||||
entity.setMaxLength(verify.maxLength()); | |||||
entity.setMinLength(verify.minLength()); | |||||
entity.setMobile(verify.isMobile()); | |||||
entity.setNotNull(verify.notNull()); | |||||
entity.setRegex(verify.regex()); | |||||
entity.setRegexTip(verify.regexTip()); | |||||
entity.setTel(verify.isTel()); | |||||
return entity; | |||||
} | |||||
return null; | |||||
} | |||||
/** | /** | ||||
* 获取key的值,针对不同类型获取不同的值 | * 获取key的值,针对不同类型获取不同的值 | ||||
* | * | ||||
@@ -297,23 +130,6 @@ public class ExcelImportServer { | |||||
return obj == null ? null : obj.toString(); | return obj == null ? null : obj.toString(); | ||||
} | } | ||||
/** | |||||
* 获取保存的Excel 的真实路径 | |||||
* | |||||
* @param params | |||||
* @param pojoClass | |||||
* @return | |||||
* @throws Exception | |||||
*/ | |||||
private String getSaveExcelUrl(ImportParams params, Class<?> pojoClass) throws Exception { | |||||
String url = ""; | |||||
if (params.getSaveUrl().equals("upload/excelUpload")) { | |||||
url = pojoClass.getName().split("\\.")[pojoClass.getName().split("\\.").length - 1]; | |||||
return params.getSaveUrl() + "/" + url; | |||||
} | |||||
return params.getSaveUrl(); | |||||
} | |||||
/** | /** | ||||
* 获取保存的真实路径 | * 获取保存的真实路径 | ||||
* | * | ||||
@@ -520,46 +336,4 @@ public class ExcelImportServer { | |||||
} | } | ||||
} | } | ||||
private void saveThisExcel(ImportParams params, Class<?> pojoClass, boolean isXSSFWorkbook, | |||||
Workbook book) throws Exception { | |||||
String path = POIPublicUtil.getWebRootPath(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) | |||||
+ (isXSSFWorkbook == true ? ".xlsx" : ".xls")); | |||||
book.write(fos); | |||||
fos.close(); | |||||
} | |||||
/** | |||||
* 多个get 最后再set | |||||
* | |||||
* @param setMethods | |||||
* @param object | |||||
*/ | |||||
private void setFieldBySomeMethod(List<Method> setMethods, Object object, Object value) | |||||
throws Exception { | |||||
Object t = getFieldBySomeMethod(setMethods, object); | |||||
setMethods.get(setMethods.size() - 1).invoke(t, value); | |||||
} | |||||
/** | |||||
* | |||||
* @param entity | |||||
* @param object | |||||
* @param value | |||||
* @throws Exception | |||||
*/ | |||||
private void setValues(ExcelImportEntity entity, Object object, Object value) throws Exception { | |||||
if (entity.getMethods() != null) { | |||||
setFieldBySomeMethod(entity.getMethods(), object, value); | |||||
} else { | |||||
entity.getMethod().invoke(object, value); | |||||
} | |||||
} | |||||
} | } |
@@ -0,0 +1,259 @@ | |||||
package org.jeecgframework.poi.excel.imports.base; | |||||
import java.io.File; | |||||
import java.io.FileOutputStream; | |||||
import java.lang.reflect.Field; | |||||
import java.lang.reflect.Method; | |||||
import java.lang.reflect.ParameterizedType; | |||||
import java.text.SimpleDateFormat; | |||||
import java.util.ArrayList; | |||||
import java.util.Date; | |||||
import java.util.HashMap; | |||||
import java.util.List; | |||||
import java.util.Map; | |||||
import org.apache.commons.lang.StringUtils; | |||||
import org.apache.poi.ss.usermodel.Workbook; | |||||
import org.jeecgframework.poi.excel.annotation.Excel; | |||||
import org.jeecgframework.poi.excel.annotation.ExcelVerify; | |||||
import org.jeecgframework.poi.excel.entity.ImportParams; | |||||
import org.jeecgframework.poi.excel.entity.params.ExcelCollectionParams; | |||||
import org.jeecgframework.poi.excel.entity.params.ExcelImportEntity; | |||||
import org.jeecgframework.poi.excel.entity.params.ExcelVerifyEntity; | |||||
import org.jeecgframework.poi.util.POIPublicUtil; | |||||
/** | |||||
* 导入基础和,普通方法和Sax共用 | |||||
* @author JueYue | |||||
* @date 2015年1月9日 下午10:25:53 | |||||
*/ | |||||
public class ImportBaseService { | |||||
/** | |||||
* 把这个注解解析放到类型对象中 | |||||
* | |||||
* @param targetId | |||||
* @param field | |||||
* @param excelEntity | |||||
* @param pojoClass | |||||
* @param getMethods | |||||
* @param temp | |||||
* @throws Exception | |||||
*/ | |||||
public void addEntityToMap(String targetId, Field field, ExcelImportEntity excelEntity, | |||||
Class<?> pojoClass, List<Method> getMethods, | |||||
Map<String, ExcelImportEntity> temp) throws Exception { | |||||
Excel excel = field.getAnnotation(Excel.class); | |||||
excelEntity = new ExcelImportEntity(); | |||||
excelEntity.setType(excel.type()); | |||||
excelEntity.setSaveUrl(excel.savePath()); | |||||
excelEntity.setSaveType(excel.imageType()); | |||||
excelEntity.setReplace(excel.replace()); | |||||
excelEntity.setDatabaseFormat(excel.databaseFormat()); | |||||
excelEntity.setVerify(getImportVerify(field)); | |||||
getExcelField(targetId, field, excelEntity, excel, pojoClass); | |||||
if (getMethods != null) { | |||||
List<Method> newMethods = new ArrayList<Method>(); | |||||
newMethods.addAll(getMethods); | |||||
newMethods.add(excelEntity.getMethod()); | |||||
excelEntity.setMethods(newMethods); | |||||
} | |||||
temp.put(excelEntity.getName(), excelEntity); | |||||
} | |||||
/** | |||||
* 获取导入校验参数 | |||||
* | |||||
* @param field | |||||
* @return | |||||
*/ | |||||
public ExcelVerifyEntity getImportVerify(Field field) { | |||||
ExcelVerify verify = field.getAnnotation(ExcelVerify.class); | |||||
if (verify != null) { | |||||
ExcelVerifyEntity entity = new ExcelVerifyEntity(); | |||||
entity.setEmail(verify.isEmail()); | |||||
entity.setInterHandler(verify.interHandler()); | |||||
entity.setMaxLength(verify.maxLength()); | |||||
entity.setMinLength(verify.minLength()); | |||||
entity.setMobile(verify.isMobile()); | |||||
entity.setNotNull(verify.notNull()); | |||||
entity.setRegex(verify.regex()); | |||||
entity.setRegexTip(verify.regexTip()); | |||||
entity.setTel(verify.isTel()); | |||||
return entity; | |||||
} | |||||
return null; | |||||
} | |||||
/** | |||||
* 获取需要导出的全部字段 | |||||
* | |||||
* | |||||
* @param exclusions | |||||
* @param targetId | |||||
* 目标ID | |||||
* @param fields | |||||
* @param excelCollection | |||||
* @throws Exception | |||||
*/ | |||||
public void getAllExcelField(String targetId, Field[] fields, | |||||
Map<String, ExcelImportEntity> excelParams, | |||||
List<ExcelCollectionParams> excelCollection, Class<?> pojoClass, | |||||
List<Method> getMethods) throws Exception { | |||||
ExcelImportEntity excelEntity = null; | |||||
for (int i = 0; i < fields.length; i++) { | |||||
Field field = fields[i]; | |||||
if (POIPublicUtil.isNotUserExcelUserThis(null, field, targetId)) { | |||||
continue; | |||||
} | |||||
if (POIPublicUtil.isCollection(field.getType())) { | |||||
// 集合对象设置属性 | |||||
ExcelCollectionParams collection = new ExcelCollectionParams(); | |||||
collection.setName(field.getName()); | |||||
Map<String, ExcelImportEntity> temp = new HashMap<String, ExcelImportEntity>(); | |||||
ParameterizedType pt = (ParameterizedType) field.getGenericType(); | |||||
Class<?> clz = (Class<?>) pt.getActualTypeArguments()[0]; | |||||
collection.setType(clz); | |||||
getExcelFieldList(targetId, POIPublicUtil.getClassFields(clz), clz, temp, null); | |||||
collection.setExcelParams(temp); | |||||
excelCollection.add(collection); | |||||
} else if (POIPublicUtil.isJavaClass(field)) { | |||||
addEntityToMap(targetId, field, excelEntity, pojoClass, getMethods, excelParams); | |||||
} else { | |||||
List<Method> newMethods = new ArrayList<Method>(); | |||||
if (getMethods != null) { | |||||
newMethods.addAll(getMethods); | |||||
} | |||||
newMethods.add(POIPublicUtil.getMethod(field.getName(), pojoClass)); | |||||
getAllExcelField(targetId, POIPublicUtil.getClassFields(field.getType()), | |||||
excelParams, excelCollection, field.getType(), newMethods); | |||||
} | |||||
} | |||||
} | |||||
public void getExcelField(String targetId, Field field, ExcelImportEntity excelEntity, | |||||
Excel excel, Class<?> pojoClass) throws Exception { | |||||
excelEntity.setName(getExcelName(excel.name(), targetId)); | |||||
String fieldname = field.getName(); | |||||
excelEntity.setMethod(POIPublicUtil.getMethod(fieldname, pojoClass, field.getType())); | |||||
if (StringUtils.isEmpty(excel.importFormat())) { | |||||
excelEntity.setFormat(excel.format()); | |||||
} else { | |||||
excelEntity.setFormat(excel.importFormat()); | |||||
} | |||||
} | |||||
public void getExcelFieldList(String targetId, Field[] fields, Class<?> pojoClass, | |||||
Map<String, ExcelImportEntity> temp, List<Method> getMethods) | |||||
throws Exception { | |||||
ExcelImportEntity excelEntity = null; | |||||
for (int i = 0; i < fields.length; i++) { | |||||
Field field = fields[i]; | |||||
if (POIPublicUtil.isNotUserExcelUserThis(null, field, targetId)) { | |||||
continue; | |||||
} | |||||
if (POIPublicUtil.isJavaClass(field)) { | |||||
addEntityToMap(targetId, field, excelEntity, pojoClass, getMethods, temp); | |||||
} else { | |||||
List<Method> newMethods = new ArrayList<Method>(); | |||||
if (getMethods != null) { | |||||
newMethods.addAll(getMethods); | |||||
} | |||||
newMethods | |||||
.add(POIPublicUtil.getMethod(field.getName(), pojoClass, field.getType())); | |||||
getExcelFieldList(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<Method> list, Object t) throws Exception { | |||||
Method m; | |||||
for (int i = 0; i < list.size() - 1; i++) { | |||||
m = list.get(i); | |||||
t = m.invoke(t, new Object[] {}); | |||||
} | |||||
return t; | |||||
} | |||||
public void saveThisExcel(ImportParams params, Class<?> pojoClass, boolean isXSSFWorkbook, | |||||
Workbook book) throws Exception { | |||||
String path = POIPublicUtil.getWebRootPath(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) | |||||
+ (isXSSFWorkbook == true ? ".xlsx" : ".xls")); | |||||
book.write(fos); | |||||
fos.close(); | |||||
} | |||||
/** | |||||
* 获取保存的Excel 的真实路径 | |||||
* | |||||
* @param params | |||||
* @param pojoClass | |||||
* @return | |||||
* @throws Exception | |||||
*/ | |||||
public String getSaveExcelUrl(ImportParams params, Class<?> pojoClass) throws Exception { | |||||
String url = ""; | |||||
if (params.getSaveUrl().equals("upload/excelUpload")) { | |||||
url = pojoClass.getName().split("\\.")[pojoClass.getName().split("\\.").length - 1]; | |||||
return params.getSaveUrl() + "/" + url; | |||||
} | |||||
return params.getSaveUrl(); | |||||
} | |||||
/** | |||||
* 多个get 最后再set | |||||
* | |||||
* @param setMethods | |||||
* @param object | |||||
*/ | |||||
public void setFieldBySomeMethod(List<Method> setMethods, Object object, Object value) | |||||
throws Exception { | |||||
Object t = getFieldBySomeMethod(setMethods, object); | |||||
setMethods.get(setMethods.size() - 1).invoke(t, value); | |||||
} | |||||
/** | |||||
* | |||||
* @param entity | |||||
* @param object | |||||
* @param value | |||||
* @throws Exception | |||||
*/ | |||||
public void setValues(ExcelImportEntity entity, Object object, Object value) throws Exception { | |||||
if (entity.getMethods() != null) { | |||||
setFieldBySomeMethod(entity.getMethods(), object, value); | |||||
} else { | |||||
entity.getMethod().invoke(object, value); | |||||
} | |||||
} | |||||
} |
@@ -1,18 +1,19 @@ | |||||
package org.jeecgframework.poi.excel.imports.sax; | package org.jeecgframework.poi.excel.imports.sax; | ||||
import java.io.IOException; | |||||
import java.io.File; | |||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.util.Iterator; | import java.util.Iterator; | ||||
import java.util.List; | import java.util.List; | ||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; | |||||
import org.apache.poi.openxml4j.exceptions.OpenXML4JException; | |||||
import org.apache.poi.openxml4j.opc.OPCPackage; | import org.apache.poi.openxml4j.opc.OPCPackage; | ||||
import org.apache.poi.xssf.eventusermodel.XSSFReader; | import org.apache.poi.xssf.eventusermodel.XSSFReader; | ||||
import org.apache.poi.xssf.model.SharedStringsTable; | import org.apache.poi.xssf.model.SharedStringsTable; | ||||
import org.jeecgframework.poi.excel.entity.ImportParams; | import org.jeecgframework.poi.excel.entity.ImportParams; | ||||
import org.jeecgframework.poi.excel.imports.sax.parse.ISaxRowRead; | import org.jeecgframework.poi.excel.imports.sax.parse.ISaxRowRead; | ||||
import org.jeecgframework.poi.excel.imports.sax.parse.SaxRowRead; | import org.jeecgframework.poi.excel.imports.sax.parse.SaxRowRead; | ||||
import org.jeecgframework.poi.exception.excel.ExcelImportException; | |||||
import org.slf4j.Logger; | |||||
import org.slf4j.LoggerFactory; | |||||
import org.xml.sax.ContentHandler; | import org.xml.sax.ContentHandler; | ||||
import org.xml.sax.InputSource; | import org.xml.sax.InputSource; | ||||
import org.xml.sax.SAXException; | import org.xml.sax.SAXException; | ||||
@@ -27,12 +28,38 @@ import org.xml.sax.helpers.XMLReaderFactory; | |||||
*/ | */ | ||||
public class SaxReadExcel { | public class SaxReadExcel { | ||||
public <T> List<T> readExcel(InputStream inputstream, Class<?> pojoClass, ImportParams params) { | |||||
private static final Logger LOGGER = LoggerFactory.getLogger(SaxReadExcel.class); | |||||
public <T> List<T> readExcel(InputStream inputstream, Class<?> pojoClass, ImportParams params, | |||||
ISaxRowRead rowRead) { | |||||
try { | try { | ||||
OPCPackage opcPackage = OPCPackage.open(inputstream); | OPCPackage opcPackage = OPCPackage.open(inputstream); | ||||
return readExcel(opcPackage, pojoClass, params, rowRead); | |||||
} catch (Exception e) { | |||||
LOGGER.error(e.getMessage(), e); | |||||
throw new ExcelImportException(e.getMessage()); | |||||
} | |||||
} | |||||
public <T> List<T> readExcel(File file, Class<?> pojoClass, ImportParams params, | |||||
ISaxRowRead rowRead) { | |||||
try { | |||||
OPCPackage opcPackage = OPCPackage.open(file); | |||||
return readExcel(opcPackage, pojoClass, params, rowRead); | |||||
} catch (Exception e) { | |||||
LOGGER.error(e.getMessage(), e); | |||||
throw new ExcelImportException(e.getMessage()); | |||||
} | |||||
} | |||||
private <T> List<T> readExcel(OPCPackage opcPackage, Class<?> pojoClass, ImportParams params, | |||||
ISaxRowRead rowRead) { | |||||
try { | |||||
XSSFReader xssfReader = new XSSFReader(opcPackage); | XSSFReader xssfReader = new XSSFReader(opcPackage); | ||||
SharedStringsTable sst = xssfReader.getSharedStringsTable(); | SharedStringsTable sst = xssfReader.getSharedStringsTable(); | ||||
ISaxRowRead rowRead = new SaxRowRead(); | |||||
if (rowRead == null) { | |||||
rowRead = new SaxRowRead(pojoClass, params); | |||||
} | |||||
XMLReader parser = fetchSheetParser(sst, rowRead); | XMLReader parser = fetchSheetParser(sst, rowRead); | ||||
Iterator<InputStream> sheets = xssfReader.getSheetsData(); | Iterator<InputStream> sheets = xssfReader.getSheetsData(); | ||||
int sheetIndex = 0; | int sheetIndex = 0; | ||||
@@ -45,13 +72,13 @@ public class SaxReadExcel { | |||||
} | } | ||||
return rowRead.getList(); | return rowRead.getList(); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
LOGGER.error(e.getMessage(), e); | |||||
throw new ExcelImportException("SAX导入数据失败"); | |||||
} | } | ||||
return null; | |||||
} | } | ||||
public XMLReader fetchSheetParser(SharedStringsTable sst, ISaxRowRead rowRead) | |||||
throws SAXException { | |||||
private XMLReader fetchSheetParser(SharedStringsTable sst, ISaxRowRead rowRead) | |||||
throws SAXException { | |||||
XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); | XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); | ||||
ContentHandler handler = new SheetHandler(sst, rowRead); | ContentHandler handler = new SheetHandler(sst, rowRead); | ||||
parser.setContentHandler(handler); | parser.setContentHandler(handler); | ||||
@@ -99,16 +99,15 @@ public class SheetHandler extends DefaultHandler { | |||||
} else if (CellValueType.Number.equals(type)) { | } else if (CellValueType.Number.equals(type)) { | ||||
BigDecimal bd = new BigDecimal(value); | BigDecimal bd = new BigDecimal(value); | ||||
rowlist.add(curCol, new SaxReadCellEntity(CellValueType.Number, bd)); | rowlist.add(curCol, new SaxReadCellEntity(CellValueType.Number, bd)); | ||||
} else if (CellValueType.String.equals(type)) { | |||||
rowlist.add(curCol, new SaxReadCellEntity(CellValueType.String, value)); | |||||
} | } | ||||
curCol++; | curCol++; | ||||
} else { | |||||
//如果标签名称为 row ,这说明已到行尾,调用 optRows() 方法 | |||||
if (name.equals("row")) { | |||||
read.parse(curRow, rowlist); | |||||
rowlist.clear(); | |||||
curRow++; | |||||
curCol = 0; | |||||
} | |||||
} else if (name.equals("row")) {//如果标签名称为 row ,这说明已到行尾,调用 optRows() 方法 | |||||
read.parse(curRow, rowlist); | |||||
rowlist.clear(); | |||||
curRow++; | |||||
curCol = 0; | |||||
} | } | ||||
} | } | ||||
@@ -1,8 +1,24 @@ | |||||
package org.jeecgframework.poi.excel.imports.sax.parse; | package org.jeecgframework.poi.excel.imports.sax.parse; | ||||
import java.lang.reflect.Field; | |||||
import java.util.ArrayList; | |||||
import java.util.Collection; | |||||
import java.util.HashMap; | |||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | |||||
import org.apache.commons.lang.StringUtils; | |||||
import org.jeecgframework.poi.excel.annotation.ExcelTarget; | |||||
import org.jeecgframework.poi.excel.entity.ImportParams; | |||||
import org.jeecgframework.poi.excel.entity.params.ExcelCollectionParams; | |||||
import org.jeecgframework.poi.excel.entity.params.ExcelImportEntity; | |||||
import org.jeecgframework.poi.excel.entity.sax.SaxReadCellEntity; | import org.jeecgframework.poi.excel.entity.sax.SaxReadCellEntity; | ||||
import org.jeecgframework.poi.excel.imports.CellValueServer; | |||||
import org.jeecgframework.poi.excel.imports.base.ImportBaseService; | |||||
import org.jeecgframework.poi.exception.excel.ExcelImportException; | |||||
import org.jeecgframework.poi.util.POIPublicUtil; | |||||
import org.slf4j.Logger; | |||||
import org.slf4j.LoggerFactory; | |||||
import com.google.common.collect.Lists; | import com.google.common.collect.Lists; | ||||
@@ -12,13 +28,52 @@ import com.google.common.collect.Lists; | |||||
* @param <T> | * @param <T> | ||||
* @date 2015年1月1日 下午7:59:39 | * @date 2015年1月1日 下午7:59:39 | ||||
*/ | */ | ||||
@SuppressWarnings("rawtypes") | |||||
public class SaxRowRead implements ISaxRowRead { | |||||
@SuppressWarnings({ "rawtypes", "unchecked" }) | |||||
public class SaxRowRead extends ImportBaseService implements ISaxRowRead { | |||||
private List list; | |||||
private static final Logger LOGGER = LoggerFactory | |||||
.getLogger(SaxRowRead.class); | |||||
/** 需要返回的数据 **/ | |||||
private List list; | |||||
/** 导出的对象 **/ | |||||
private Class<?> pojoClass; | |||||
/** 导入参数 **/ | |||||
private ImportParams params; | |||||
/** 列表头对应关系 **/ | |||||
private Map<Integer, String> titlemap = new HashMap<Integer, String>(); | |||||
/** 当前的对象**/ | |||||
private Object object = null; | |||||
public SaxRowRead() { | |||||
private Map<String, ExcelImportEntity> excelParams = new HashMap<String, ExcelImportEntity>(); | |||||
private List<ExcelCollectionParams> excelCollection = new ArrayList<ExcelCollectionParams>(); | |||||
private String targetId; | |||||
private CellValueServer cellValueServer; | |||||
public SaxRowRead(Class<?> pojoClass, ImportParams params) { | |||||
list = Lists.newArrayList(); | list = Lists.newArrayList(); | ||||
this.params = params; | |||||
this.pojoClass = pojoClass; | |||||
cellValueServer = new CellValueServer(); | |||||
initParams(pojoClass, params); | |||||
} | |||||
private void initParams(Class<?> pojoClass, ImportParams params) { | |||||
try { | |||||
Field fileds[] = POIPublicUtil.getClassFields(pojoClass); | |||||
ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); | |||||
if (etarget != null) { | |||||
targetId = etarget.value(); | |||||
} | |||||
getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, null); | |||||
} catch (Exception e) { | |||||
LOGGER.error(e.getMessage(), e); | |||||
throw new ExcelImportException(e.getMessage()); | |||||
} | |||||
} | } | ||||
@Override | @Override | ||||
@@ -28,7 +83,111 @@ public class SaxRowRead implements ISaxRowRead { | |||||
@Override | @Override | ||||
public void parse(int index, List<SaxReadCellEntity> datas) { | public void parse(int index, List<SaxReadCellEntity> datas) { | ||||
try { | |||||
if (datas == null || datas.size() == 0) { | |||||
return; | |||||
} | |||||
//标题行跳过 | |||||
if (index < params.getTitleRows()) { | |||||
return; | |||||
} | |||||
//表头行 | |||||
if (index < params.getTitleRows() + params.getHeadRows()) { | |||||
addHeadData(datas); | |||||
} else { | |||||
addListData(datas); | |||||
} | |||||
} catch (Exception e) { | |||||
LOGGER.error(e.getMessage(), e); | |||||
throw new ExcelImportException(e.getMessage()); | |||||
} | |||||
} | |||||
/** | |||||
* 集合元素处理 | |||||
* @param datas | |||||
*/ | |||||
private void addListData(List<SaxReadCellEntity> datas) throws Exception { | |||||
// 判断是集合元素还是不是集合元素,如果是就继续加入这个集合,不是就创建新的对象 | |||||
if ((datas.get(params.getKeyIndex()) == null || StringUtils.isEmpty(String.valueOf(datas | |||||
.get(params.getKeyIndex()).getValue()))) && object != null) { | |||||
for (ExcelCollectionParams param : excelCollection) { | |||||
addListContinue(object, param, datas, titlemap, targetId, params); | |||||
} | |||||
} else { | |||||
object = POIPublicUtil.createObject(pojoClass, targetId); | |||||
SaxReadCellEntity entity; | |||||
for (int i = 0, le = datas.size(); i < le; i++) { | |||||
entity = datas.get(i); | |||||
String titleString = (String) titlemap.get(i); | |||||
if (excelParams.containsKey(titleString)) { | |||||
saveFieldValue(params, object, entity, excelParams, titleString); | |||||
} | |||||
} | |||||
for (ExcelCollectionParams param : excelCollection) { | |||||
addListContinue(object, param, datas, titlemap, targetId, params); | |||||
} | |||||
list.add(object); | |||||
} | |||||
} | |||||
/*** | |||||
* 向List里面继续添加元素 | |||||
* | |||||
* @param exclusions | |||||
* @param object | |||||
* @param param | |||||
* @param datas | |||||
* @param titlemap | |||||
* @param targetId | |||||
* @param params | |||||
*/ | |||||
private void addListContinue(Object object, ExcelCollectionParams param, | |||||
List<SaxReadCellEntity> datas, Map<Integer, String> titlemap, | |||||
String targetId, ImportParams params) throws Exception { | |||||
Collection collection = (Collection) POIPublicUtil.getMethod(param.getName(), | |||||
object.getClass()).invoke(object, new Object[] {}); | |||||
Object entity = POIPublicUtil.createObject(param.getType(), targetId); | |||||
boolean isUsed = false;// 是否需要加上这个对象 | |||||
for (int i = 0; i < datas.size(); i++) { | |||||
String titleString = (String) titlemap.get(i); | |||||
if (param.getExcelParams().containsKey(titleString)) { | |||||
saveFieldValue(params, entity, datas.get(i), param.getExcelParams(), titleString); | |||||
isUsed = true; | |||||
} | |||||
} | |||||
if (isUsed) { | |||||
collection.add(entity); | |||||
} | |||||
} | } | ||||
/** | |||||
* 设置值 | |||||
* @param params | |||||
* @param object | |||||
* @param entity | |||||
* @param excelParams | |||||
* @param titleString | |||||
* @throws Exception | |||||
*/ | |||||
private void saveFieldValue(ImportParams params, Object object, SaxReadCellEntity entity, | |||||
Map<String, ExcelImportEntity> excelParams, String titleString) | |||||
throws Exception { | |||||
Object value = cellValueServer.getValue(params.getDataHanlder(), object, entity, | |||||
excelParams, titleString); | |||||
setValues(excelParams.get(titleString), object, value); | |||||
} | |||||
/** | |||||
* put 表头数据 | |||||
* @param datas | |||||
*/ | |||||
private void addHeadData(List<SaxReadCellEntity> datas) { | |||||
for (int i = 0; i < datas.size(); i++) { | |||||
if (StringUtils.isNotEmpty(String.valueOf(datas.get(i).getValue()))) { | |||||
titlemap.put(i, String.valueOf(datas.get(i).getValue())); | |||||
} | |||||
} | |||||
} | |||||
} | } |
@@ -317,7 +317,7 @@ public class POIPublicUtil { | |||||
* 返回流和图片类型 | * 返回流和图片类型 | ||||
*@Author JueYue | *@Author JueYue | ||||
*@date 2013-11-20 | *@date 2013-11-20 | ||||
*@param obj | |||||
*@param entity | |||||
*@return (byte[]) isAndType[0],(Integer)isAndType[1] | *@return (byte[]) isAndType[0],(Integer)isAndType[1] | ||||
* @throws Exception | * @throws Exception | ||||
*/ | */ | ||||
@@ -395,9 +395,6 @@ public class POIPublicUtil { | |||||
* @param index | * @param index | ||||
* @return | * @return | ||||
* @throws Exception | * @throws Exception | ||||
* @throws InvocationTargetException | |||||
* @throws IllegalAccessException | |||||
* @throws IllegalArgumentException | |||||
*/ | */ | ||||
@SuppressWarnings("rawtypes") | @SuppressWarnings("rawtypes") | ||||
public static Object getValueDoWhile(Object object, String[] paramsArr, int index) | public static Object getValueDoWhile(Object object, String[] paramsArr, int index) | ||||
@@ -5,27 +5,31 @@ import java.util.Date; | |||||
import java.util.List; | import java.util.List; | ||||
import org.apache.commons.lang.StringUtils; | import org.apache.commons.lang.StringUtils; | ||||
import org.apache.commons.lang.builder.ReflectionToStringBuilder; | |||||
import org.jeecgframework.poi.entity.CourseEntity; | import org.jeecgframework.poi.entity.CourseEntity; | ||||
import org.jeecgframework.poi.entity.TestEntity; | import org.jeecgframework.poi.entity.TestEntity; | ||||
import org.jeecgframework.poi.entity.statistics.StatisticEntity; | |||||
import org.jeecgframework.poi.excel.entity.ImportParams; | import org.jeecgframework.poi.excel.entity.ImportParams; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
public class ExcelImportUtilTest { | public class ExcelImportUtilTest { | ||||
//@Test | |||||
@Test | |||||
public void test() { | public void test() { | ||||
ImportParams params = new ImportParams(); | ImportParams params = new ImportParams(); | ||||
params.setTitleRows(2); | |||||
params.setHeadRows(2); | |||||
//params.setSheetNum(9); | |||||
params.setNeedSave(true); | |||||
params.setTitleRows(1); | |||||
long start = new Date().getTime(); | long start = new Date().getTime(); | ||||
List<CourseEntity> list = ExcelImportUtil.importExcel(new File("d:/tt.xlsx"), | |||||
CourseEntity.class, params); | |||||
List<StatisticEntity> list = ExcelImportUtil.importExcelBySax(new File("d:/tt.xlsx"), | |||||
StatisticEntity.class, params); | |||||
// List<StatisticEntity> list = ExcelImportUtil.importExcelBySax(new File("d:/tt.xlsx"), | |||||
// StatisticEntity.class, params); | |||||
/*for (int i = 0; i < list.size(); i++) { | |||||
System.out.println(ReflectionToStringBuilder.toString(list.get(i))); | |||||
}*/ | |||||
System.out.println(list.size() + "-----" + (new Date().getTime() - start)); | System.out.println(list.size() + "-----" + (new Date().getTime() - start)); | ||||
} | } | ||||
@Test | |||||
// @Test | |||||
public void test2() { | public void test2() { | ||||
ImportParams params = new ImportParams(); | ImportParams params = new ImportParams(); | ||||
params.setTitleRows(1); | params.setTitleRows(1); | ||||
@@ -21,7 +21,7 @@ public class ExcelExportStatisticTest { | |||||
public void test() throws Exception { | public void test() throws Exception { | ||||
List<StatisticEntity> list = new ArrayList<StatisticEntity>(); | List<StatisticEntity> list = new ArrayList<StatisticEntity>(); | ||||
for (int i = 0; i < 20; i++) { | |||||
for (int i = 0; i < 20000; i++) { | |||||
StatisticEntity client = new StatisticEntity(); | StatisticEntity client = new StatisticEntity(); | ||||
client.setName("index" + i); | client.setName("index" + i); | ||||
client.setIntTest(1 + i); | client.setIntTest(1 + i); | ||||
@@ -32,8 +32,7 @@ public class ExcelExportStatisticTest { | |||||
list.add(client); | list.add(client); | ||||
} | } | ||||
Date start = new Date(); | Date start = new Date(); | ||||
ExportParams params = new ExportParams("2412312", "测试", ExcelType.XSSF); | |||||
params.setStyle(ExcelStyleType.COLOR.getClazz()); | |||||
ExportParams params = new ExportParams("2412312", "测试",ExcelType.XSSF); | |||||
Workbook workbook = ExcelExportUtil.exportExcel(params, StatisticEntity.class, list); | Workbook workbook = ExcelExportUtil.exportExcel(params, StatisticEntity.class, list); | ||||
System.out.println(new Date().getTime() - start.getTime()); | System.out.println(new Date().getTime() - start.getTime()); | ||||
File savefile = new File("d:/"); | File savefile = new File("d:/"); | ||||
@@ -24,9 +24,9 @@ | |||||
<properties> | <properties> | ||||
<sub.version>2.0.7-SNAPSHOT</sub.version> | <sub.version>2.0.7-SNAPSHOT</sub.version> | ||||
<poi.version>3.11</poi.version> | |||||
<poi.version>3.9</poi.version> | |||||
<xerces.version>2.4.0</xerces.version> | |||||
<guava.version>16.0.1</guava.version> | <guava.version>16.0.1</guava.version> | ||||
<reflectasm.version>1.09</reflectasm.version> | |||||
<commons-lang.version>2.6</commons-lang.version> | <commons-lang.version>2.6</commons-lang.version> | ||||
<slf4j.version>1.6.1</slf4j.version> | <slf4j.version>1.6.1</slf4j.version> | ||||
<spring.version>3.1.1.RELEASE</spring.version> | <spring.version>3.1.1.RELEASE</spring.version> | ||||
@@ -100,12 +100,18 @@ | |||||
<artifactId>spring-core</artifactId> | <artifactId>spring-core</artifactId> | ||||
<version>${spring.version}</version> | <version>${spring.version}</version> | ||||
</dependency> | </dependency> | ||||
<dependency> | |||||
<groupId>xerces</groupId> | |||||
<artifactId>xerces</artifactId> | |||||
<version>${xerces.version}</version> | |||||
</dependency> | |||||
<dependency> | <dependency> | ||||
<groupId>javax.servlet</groupId> | <groupId>javax.servlet</groupId> | ||||
<artifactId>servlet-api</artifactId> | <artifactId>servlet-api</artifactId> | ||||
<version>2.5</version> | <version>2.5</version> | ||||
<scope>provided</scope> | <scope>provided</scope> | ||||
<optional>true</optional> | |||||
</dependency> | </dependency> | ||||
<!-- test --> | <!-- test --> | ||||