| @@ -11,6 +11,7 @@ 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.handler.inter.IExcelReadRowHanlder; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| @@ -108,25 +109,11 @@ public class ExcelImportUtil { | |||
| 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 inputstream | |||
| * @param pojoClass | |||
| * @param params | |||
| * @return | |||
| @@ -134,7 +121,7 @@ public class ExcelImportUtil { | |||
| */ | |||
| public static <T> List<T> importExcelBySax(InputStream inputstream, Class<?> pojoClass, | |||
| ImportParams params) { | |||
| return new SaxReadExcel().readExcel(inputstream, pojoClass, params, null); | |||
| return new SaxReadExcel().readExcel(inputstream, pojoClass, params, null, null); | |||
| } | |||
| /** | |||
| @@ -146,8 +133,10 @@ public class ExcelImportUtil { | |||
| * @return | |||
| * @throws Exception | |||
| */ | |||
| public static <T> List<T> importExcelBySax(File file, ISaxRowRead rowRead) { | |||
| return new SaxReadExcel().readExcel(file, null, null, rowRead); | |||
| @SuppressWarnings("rawtypes") | |||
| public static void importExcelBySax(InputStream inputstream, Class<?> pojoClass, | |||
| ImportParams params, IExcelReadRowHanlder hanlder) { | |||
| new SaxReadExcel().readExcel(inputstream, pojoClass, params, null, hanlder); | |||
| } | |||
| /** | |||
| @@ -160,7 +149,7 @@ public class ExcelImportUtil { | |||
| * @throws Exception | |||
| */ | |||
| public static <T> List<T> importExcelBySax(InputStream inputstream, ISaxRowRead rowRead) { | |||
| return new SaxReadExcel().readExcel(inputstream, null, null, rowRead); | |||
| return new SaxReadExcel().readExcel(inputstream, null, null, rowRead, null); | |||
| } | |||
| } | |||
| @@ -5,9 +5,6 @@ import java.io.FileOutputStream; | |||
| import java.io.InputStream; | |||
| import java.io.PushbackInputStream; | |||
| 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.Collection; | |||
| import java.util.Date; | |||
| @@ -30,13 +27,10 @@ import org.apache.poi.ss.usermodel.Sheet; | |||
| import org.apache.poi.ss.usermodel.Workbook; | |||
| import org.apache.poi.xssf.usermodel.XSSFSheet; | |||
| import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |||
| import org.jeecgframework.poi.excel.annotation.Excel; | |||
| import org.jeecgframework.poi.excel.annotation.ExcelTarget; | |||
| 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.excel.entity.result.ExcelImportResult; | |||
| import org.jeecgframework.poi.excel.entity.result.ExcelVerifyHanlderResult; | |||
| import org.jeecgframework.poi.excel.imports.base.ImportBaseService; | |||
| @@ -1,6 +1,5 @@ | |||
| package org.jeecgframework.poi.excel.imports.sax; | |||
| import java.io.File; | |||
| import java.io.InputStream; | |||
| import java.util.Iterator; | |||
| import java.util.List; | |||
| @@ -12,6 +11,7 @@ import org.jeecgframework.poi.excel.entity.ImportParams; | |||
| import org.jeecgframework.poi.excel.imports.sax.parse.ISaxRowRead; | |||
| import org.jeecgframework.poi.excel.imports.sax.parse.SaxRowRead; | |||
| import org.jeecgframework.poi.exception.excel.ExcelImportException; | |||
| import org.jeecgframework.poi.handler.inter.IExcelReadRowHanlder; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.xml.sax.ContentHandler; | |||
| @@ -26,26 +26,16 @@ import org.xml.sax.helpers.XMLReaderFactory; | |||
| * @date 2014年12月29日 下午9:41:38 | |||
| * @version 1.0 | |||
| */ | |||
| @SuppressWarnings("rawtypes") | |||
| public class SaxReadExcel { | |||
| private static final Logger LOGGER = LoggerFactory.getLogger(SaxReadExcel.class); | |||
| public <T> List<T> readExcel(InputStream inputstream, Class<?> pojoClass, ImportParams params, | |||
| ISaxRowRead rowRead) { | |||
| ISaxRowRead rowRead, IExcelReadRowHanlder hanlder) { | |||
| try { | |||
| 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); | |||
| return readExcel(opcPackage, pojoClass, params, rowRead, hanlder); | |||
| } catch (Exception e) { | |||
| LOGGER.error(e.getMessage(), e); | |||
| throw new ExcelImportException(e.getMessage()); | |||
| @@ -53,12 +43,12 @@ public class SaxReadExcel { | |||
| } | |||
| private <T> List<T> readExcel(OPCPackage opcPackage, Class<?> pojoClass, ImportParams params, | |||
| ISaxRowRead rowRead) { | |||
| ISaxRowRead rowRead, IExcelReadRowHanlder hanlder) { | |||
| try { | |||
| XSSFReader xssfReader = new XSSFReader(opcPackage); | |||
| SharedStringsTable sst = xssfReader.getSharedStringsTable(); | |||
| if (rowRead == null) { | |||
| rowRead = new SaxRowRead(pojoClass, params); | |||
| rowRead = new SaxRowRead(pojoClass, params, hanlder); | |||
| } | |||
| XMLReader parser = fetchSheetParser(sst, rowRead); | |||
| Iterator<InputStream> sheets = xssfReader.getSheetsData(); | |||
| @@ -16,6 +16,7 @@ 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.handler.inter.IExcelReadRowHanlder; | |||
| import org.jeecgframework.poi.util.POIPublicUtil; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| @@ -52,11 +53,14 @@ public class SaxRowRead extends ImportBaseService implements ISaxRowRead { | |||
| private CellValueServer cellValueServer; | |||
| public SaxRowRead(Class<?> pojoClass, ImportParams params) { | |||
| private IExcelReadRowHanlder hanlder; | |||
| public SaxRowRead(Class<?> pojoClass, ImportParams params, IExcelReadRowHanlder hanlder) { | |||
| list = Lists.newArrayList(); | |||
| this.params = params; | |||
| this.pojoClass = pojoClass; | |||
| cellValueServer = new CellValueServer(); | |||
| this.hanlder = hanlder; | |||
| initParams(pojoClass, params); | |||
| } | |||
| @@ -115,6 +119,9 @@ public class SaxRowRead extends ImportBaseService implements ISaxRowRead { | |||
| addListContinue(object, param, datas, titlemap, targetId, params); | |||
| } | |||
| } else { | |||
| if (object != null && hanlder != null) { | |||
| hanlder.hanlder(object); | |||
| } | |||
| object = POIPublicUtil.createObject(pojoClass, targetId); | |||
| SaxReadCellEntity entity; | |||
| for (int i = 0, le = datas.size(); i < le; i++) { | |||
| @@ -127,7 +134,9 @@ public class SaxRowRead extends ImportBaseService implements ISaxRowRead { | |||
| for (ExcelCollectionParams param : excelCollection) { | |||
| addListContinue(object, param, datas, titlemap, targetId, params); | |||
| } | |||
| list.add(object); | |||
| if (hanlder == null) { | |||
| list.add(object); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,16 @@ | |||
| package org.jeecgframework.poi.handler.inter; | |||
| /** | |||
| * 接口自定义处理类 | |||
| * @author JueYue | |||
| * @date 2015年1月16日 下午8:06:26 | |||
| * @param <T> | |||
| */ | |||
| public interface IExcelReadRowHanlder<T> { | |||
| /** | |||
| * 处理解析对象 | |||
| * @param t | |||
| */ | |||
| public void hanlder(T t); | |||
| } | |||
| @@ -1,6 +1,8 @@ | |||
| package org.jeecgframework.poi.excel; | |||
| import java.io.File; | |||
| import java.io.FileInputStream; | |||
| import java.io.FileNotFoundException; | |||
| import java.util.Date; | |||
| import java.util.List; | |||
| @@ -16,17 +18,22 @@ public class ExcelImportUtilTest { | |||
| @Test | |||
| public void test() { | |||
| ImportParams params = new ImportParams(); | |||
| params.setTitleRows(1); | |||
| long start = new Date().getTime(); | |||
| 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)); | |||
| try { | |||
| ImportParams params = new ImportParams(); | |||
| params.setTitleRows(1); | |||
| long start = new Date().getTime(); | |||
| List<StatisticEntity> list = ExcelImportUtil.importExcelBySax(new FileInputStream( | |||
| 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)); | |||
| } catch (FileNotFoundException e) { | |||
| // TODO Auto-generated catch block | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| // @Test | |||
| @@ -161,4 +161,15 @@ | |||
| </plugins> | |||
| </build> | |||
| <distributionManagement> | |||
| <repository> | |||
| <id>postaop-common-release</id> | |||
| <url>http://192.168.1.99:8081/nexus/content/repositories/postaop-common-release/</url> | |||
| </repository> | |||
| <snapshotRepository> | |||
| <id>postaop-common-snapshot</id> | |||
| <url>http://192.168.1.99:8081/nexus/content/repositories/postaop-common-snapshot/</url> | |||
| </snapshotRepository> | |||
| </distributionManagement> | |||
| </project> | |||