From 93f2c26c1a01c41e63ee2b5619be1f5a00097430 Mon Sep 17 00:00:00 2001 From: jueyue Date: Tue, 18 Oct 2016 10:08:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BE=9B=E4=BA=86=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=96=B0=E7=9A=84=E5=AF=BC=E5=87=BA=E6=96=B9=E6=B3=95,?= =?UTF-8?q?=E9=92=88=E5=AF=B9=E5=A4=A7=E6=95=B0=E6=8D=AE=E9=87=8F=E7=9A=84?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E5=AF=BC=E5=87=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 + easypoi-annotation/pom.xml | 2 +- easypoi-base/pom.xml | 2 +- .../excel/export/ExcelBatchExportServer.java | 3 + .../poi/excel/imports/ExcelImportServer.java | 881 +++++++++--------- .../poi/handler/inter/IExcelExportServer.java | 14 + easypoi-web/pom.xml | 2 +- .../excel/entity/vo/BigExcelConstants.java | 37 + .../poi/excel/view/BigExcelExportView.java | 83 ++ pom.xml | 48 +- 10 files changed, 588 insertions(+), 488 deletions(-) create mode 100644 easypoi-base/src/main/java/org/jeecgframework/poi/handler/inter/IExcelExportServer.java create mode 100644 easypoi-web/src/main/java/org/jeecgframework/poi/excel/entity/vo/BigExcelConstants.java create mode 100644 easypoi-web/src/main/java/org/jeecgframework/poi/excel/view/BigExcelExportView.java diff --git a/README.md b/README.md index 56d3753..4a009e2 100644 --- a/README.md +++ b/README.md @@ -406,3 +406,7 @@ EasyPoi导出实例 List> list = ExcelImportUtil.importExcel(new File( "d:/tt.xls"), Map.class, params); ``` +10.大数据量Excel导出 + exportBigExcel 的方法 ,最后可以关闭closeExportBigExcel 也可以不关闭 + + diff --git a/easypoi-annotation/pom.xml b/easypoi-annotation/pom.xml index 0edd834..11a899a 100644 --- a/easypoi-annotation/pom.xml +++ b/easypoi-annotation/pom.xml @@ -4,7 +4,7 @@ org.jeecg easypoi - 2.3.1 + 2.3.2-SNAPSHOT easypoi-annotation 基础注解类,解耦合 diff --git a/easypoi-base/pom.xml b/easypoi-base/pom.xml index df47a18..5409275 100644 --- a/easypoi-base/pom.xml +++ b/easypoi-base/pom.xml @@ -4,7 +4,7 @@ org.jeecg easypoi - 2.3.1 + 2.3.2-SNAPSHOT easypoi-base diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelBatchExportServer.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelBatchExportServer.java index 6c93044..649d497 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelBatchExportServer.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelBatchExportServer.java @@ -70,6 +70,7 @@ public class ExcelBatchExportServer extends ExcelExportServer { String targetId = etarget == null ? null : etarget.value(); getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, null); + sortAllParams(excelParams); try { sheet = workbook.createSheet(entity.getSheetName()); } catch (Exception e) { @@ -87,6 +88,7 @@ public class ExcelBatchExportServer extends ExcelExportServer { sheet = workbook.createSheet(); index = 0; } + Iterator its = dataSet.iterator(); while (its.hasNext()) { Object t = its.next(); @@ -147,6 +149,7 @@ public class ExcelBatchExportServer extends ExcelExportServer { mergeCells(sheet, excelParams, titleHeight); // 创建合计信息 addStatisticsRow(getExcelExportStyler().getStyles(true, null), sheet); + THREAD_LOCAL.remove(); } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java index e3fd23c..ffb1a5f 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/ExcelImportServer.java @@ -70,463 +70,464 @@ import org.slf4j.LoggerFactory; @SuppressWarnings({ "rawtypes", "unchecked", "hiding" }) public class ExcelImportServer extends ImportBaseService { - private final static Logger LOGGER = LoggerFactory.getLogger(ExcelImportServer.class); + private final static Logger LOGGER = LoggerFactory.getLogger(ExcelImportServer.class); - private CellValueServer cellValueServer; + private CellValueServer cellValueServer; - private boolean verfiyFail = false; - /** - * 异常数据styler - */ - private CellStyle errorCellStyle; + private boolean verfiyFail = false; + /** + * 异常数据styler + */ + private CellStyle errorCellStyle; - public ExcelImportServer() { - this.cellValueServer = new CellValueServer(); - } + public ExcelImportServer() { + this.cellValueServer = new CellValueServer(); + } - /*** - * 向List里面继续添加元素 - * - * @param object - * @param param - * @param row - * @param titlemap - * @param targetId - * @param pictures - * @param params - */ - private void addListContinue(Object object, ExcelCollectionParams param, Row row, - Map titlemap, String targetId, - Map pictures, - ImportParams params) throws Exception { - Collection collection = (Collection) PoiReflectorUtil.fromCache(object.getClass()) - .getValue(object, param.getName()); - Object entity = PoiPublicUtil.createObject(param.getType(), targetId); - String picId; - boolean isUsed = false;// 是否需要加上这个对象 - for (int i = row.getFirstCellNum(); i < param.getExcelParams().size(); i++) { - Cell cell = row.getCell(i); - String titleString = (String) titlemap.get(i); - if (param.getExcelParams().containsKey(titleString)) { - if (param.getExcelParams().get(titleString).getType() == 2) { - picId = row.getRowNum() + "_" + i; - saveImage(object, picId, param.getExcelParams(), titleString, pictures, params); - } else { - saveFieldValue(params, entity, cell, param.getExcelParams(), titleString, row); - } - isUsed = true; - } - } - if (isUsed) { - collection.add(entity); - } - } + /*** + * 向List里面继续添加元素 + * + * @param object + * @param param + * @param row + * @param titlemap + * @param targetId + * @param pictures + * @param params + */ + private void addListContinue(Object object, ExcelCollectionParams param, Row row, + Map titlemap, String targetId, + Map pictures, + ImportParams params) throws Exception { + Collection collection = (Collection) PoiReflectorUtil.fromCache(object.getClass()) + .getValue(object, param.getName()); + Object entity = PoiPublicUtil.createObject(param.getType(), targetId); + String picId; + boolean isUsed = false;// 是否需要加上这个对象 + for (int i = row.getFirstCellNum(); i < param.getExcelParams().size(); i++) { + Cell cell = row.getCell(i); + String titleString = (String) titlemap.get(i); + if (param.getExcelParams().containsKey(titleString)) { + if (param.getExcelParams().get(titleString).getType() == 2) { + picId = row.getRowNum() + "_" + i; + saveImage(object, picId, param.getExcelParams(), titleString, pictures, params); + } else { + saveFieldValue(params, entity, cell, param.getExcelParams(), titleString, row); + } + isUsed = true; + } + } + if (isUsed) { + collection.add(entity); + } + } - /** - * 获取key的值,针对不同类型获取不同的值 - * - * @author JueYue - * 2013-11-21 - * @param cell - * @return - */ - private String getKeyValue(Cell cell) { - Object obj = null; - switch (cell.getCellType()) { - case Cell.CELL_TYPE_STRING: - obj = cell.getStringCellValue(); - break; - case Cell.CELL_TYPE_BOOLEAN: - obj = cell.getBooleanCellValue(); - break; - case Cell.CELL_TYPE_NUMERIC: - obj = cell.getNumericCellValue(); - break; - case Cell.CELL_TYPE_FORMULA: - obj = cell.getCellFormula(); - break; - default: - cell.setCellType(Cell.CELL_TYPE_STRING); - obj = cell.getStringCellValue(); - } - return obj == null ? null : obj.toString().trim(); - } + /** + * 获取key的值,针对不同类型获取不同的值 + * + * @author JueYue + * 2013-11-21 + * @param cell + * @return + */ + private String getKeyValue(Cell cell) { + Object obj = null; + switch (cell.getCellType()) { + case Cell.CELL_TYPE_STRING: + obj = cell.getStringCellValue(); + break; + case Cell.CELL_TYPE_BOOLEAN: + obj = cell.getBooleanCellValue(); + break; + case Cell.CELL_TYPE_NUMERIC: + obj = cell.getNumericCellValue(); + break; + case Cell.CELL_TYPE_FORMULA: + obj = cell.getCellFormula(); + break; + default: + cell.setCellType(Cell.CELL_TYPE_STRING); + obj = cell.getStringCellValue(); + } + return obj == null ? null : obj.toString().trim(); + } - /** - * 获取保存的真实路径 - * - * @param excelImportEntity - * @param object - * @return - * @throws Exception - */ - private String getSaveUrl(ExcelImportEntity excelImportEntity, Object object) throws Exception { - String url = ""; - if (excelImportEntity.getSaveUrl().equals("upload")) { - 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(); - } + /** + * 获取保存的真实路径 + * + * @param excelImportEntity + * @param object + * @return + * @throws Exception + */ + private String getSaveUrl(ExcelImportEntity excelImportEntity, Object object) throws Exception { + String url = ""; + if (excelImportEntity.getSaveUrl().equals("upload")) { + 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(); + } - private List importExcel(Collection result, Sheet sheet, Class pojoClass, - ImportParams params, - Map pictures) throws Exception { - List collection = new ArrayList(); - Map excelParams = new HashMap(); - List excelCollection = new ArrayList(); - String targetId = null; - if (!Map.class.equals(pojoClass)) { - Field fileds[] = PoiPublicUtil.getClassFields(pojoClass); - ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); - if (etarget != null) { - targetId = etarget.value(); - } - getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, null); - } - Iterator rows = sheet.rowIterator(); - for (int j = 0; j < params.getTitleRows(); j++) { - rows.next(); - } - Map titlemap = getTitleMap(rows, params, excelCollection); - checkIsValidTemplate(titlemap, excelParams, params, excelCollection); - Row row = null; - Object object = null; - String picId; - while (rows.hasNext() - && (row == null - || sheet.getLastRowNum() - row.getRowNum() > params.getLastOfInvalidRow())) { - row = rows.next(); - // 判断是集合元素还是不是集合元素,如果是就继续加入这个集合,不是就创建新的对象 - // keyIndex 如果为空就不处理,仍然处理这一行 - if (params.getKeyIndex() != null - && (row.getCell(params.getKeyIndex()) == null - || StringUtils.isEmpty(getKeyValue(row.getCell(params.getKeyIndex())))) - && object != null) { - for (ExcelCollectionParams param : excelCollection) { - addListContinue(object, param, row, titlemap, targetId, pictures, params); - } - } else { - object = PoiPublicUtil.createObject(pojoClass, targetId); - try { - for (int i = row.getFirstCellNum(), le = titlemap.size(); i < le; i++) { - Cell cell = row.getCell(i); - String titleString = (String) titlemap.get(i); - if (excelParams.containsKey(titleString) || Map.class.equals(pojoClass)) { - if (excelParams.get(titleString) != null - && excelParams.get(titleString).getType() == 2) { - picId = row.getRowNum() + "_" + i; - saveImage(object, picId, excelParams, titleString, pictures, - params); - } else { - saveFieldValue(params, object, cell, excelParams, titleString, row); - } - } - } + private List importExcel(Collection result, Sheet sheet, Class pojoClass, + ImportParams params, + Map pictures) throws Exception { + List collection = new ArrayList(); + Map excelParams = new HashMap(); + List excelCollection = new ArrayList(); + String targetId = null; + if (!Map.class.equals(pojoClass)) { + Field fileds[] = PoiPublicUtil.getClassFields(pojoClass); + ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); + if (etarget != null) { + targetId = etarget.value(); + } + getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, null); + } + Iterator rows = sheet.rowIterator(); + for (int j = 0; j < params.getTitleRows(); j++) { + rows.next(); + } + Map titlemap = getTitleMap(rows, params, excelCollection); + checkIsValidTemplate(titlemap, excelParams, params, excelCollection); + Row row = null; + Object object = null; + String picId; + while (rows.hasNext() + && (row == null + || sheet.getLastRowNum() - row.getRowNum() > params.getLastOfInvalidRow())) { + row = rows.next(); + // 判断是集合元素还是不是集合元素,如果是就继续加入这个集合,不是就创建新的对象 + // keyIndex 如果为空就不处理,仍然处理这一行 + if (params.getKeyIndex() != null + && (row.getCell(params.getKeyIndex()) == null + || StringUtils.isEmpty(getKeyValue(row.getCell(params.getKeyIndex())))) + && object != null) { + for (ExcelCollectionParams param : excelCollection) { + addListContinue(object, param, row, titlemap, targetId, pictures, params); + } + } else { + object = PoiPublicUtil.createObject(pojoClass, targetId); + try { + for (int i = row.getFirstCellNum(), le = titlemap.size(); i < le; i++) { + Cell cell = row.getCell(i); + String titleString = (String) titlemap.get(i); + if (excelParams.containsKey(titleString) || Map.class.equals(pojoClass)) { + if (excelParams.get(titleString) != null + && excelParams.get(titleString).getType() == 2) { + picId = row.getRowNum() + "_" + i; + saveImage(object, picId, excelParams, titleString, pictures, + params); + } else { + saveFieldValue(params, object, cell, excelParams, titleString, row); + } + } + } - for (ExcelCollectionParams param : excelCollection) { - addListContinue(object, param, row, titlemap, targetId, pictures, params); - } - if (verifyingDataValidity(object, row, params, pojoClass)) { - collection.add(object); - } - } catch (ExcelImportException e) { - if (!e.getType().equals(ExcelImportEnum.VERIFY_ERROR)) { - throw new ExcelImportException(e.getType(), e); - } - } - } - } - return collection; - } + for (ExcelCollectionParams param : excelCollection) { + addListContinue(object, param, row, titlemap, targetId, pictures, params); + } + if (verifyingDataValidity(object, row, params, pojoClass)) { + collection.add(object); + } + } catch (ExcelImportException e) { + if (!e.getType().equals(ExcelImportEnum.VERIFY_ERROR)) { + throw new ExcelImportException(e.getType(), e); + } + } + } + } + return collection; + } - /** - * 校验数据合法性 - * @param object - * @param row - * @param params - * @param pojoClass - * @return - */ - private boolean verifyingDataValidity(Object object, Row row, ImportParams params, - Class pojoClass) { - boolean isAdd = true; - Cell cell = null; - if (params.isNeedVerfiy()) { - String errorMsg = PoiValidationUtil.validation(object); - if (StringUtils.isNotEmpty(errorMsg)) { - cell = row.createCell(row.getLastCellNum()); - cell.setCellValue(errorMsg); - if (object instanceof IExcelModel) { - IExcelModel model = (IExcelModel) object; - model.setErrorMsg(errorMsg); - } else { - isAdd = false; - } - verfiyFail = true; - } - } - if (params.getVerifyHanlder() != null) { - ExcelVerifyHanlderResult result = params.getVerifyHanlder().verifyHandler(object); - if (!result.isSuccess()) { - if (cell == null) - cell = row.createCell(row.getLastCellNum()); - cell.setCellValue((StringUtils.isNoneBlank(cell.getStringCellValue()) - ? cell.getStringCellValue() + "," : "") + result.getMsg()); - if (object instanceof IExcelModel) { - IExcelModel model = (IExcelModel) object; - model.setErrorMsg((StringUtils.isNoneBlank(model.getErrorMsg()) - ? model.getErrorMsg() + "," : "") + result.getMsg()); - } else { - isAdd = false; - } - verfiyFail = true; - } - } - if (cell != null) - cell.setCellStyle(errorCellStyle); - return isAdd; - } + /** + * 校验数据合法性 + * @param object + * @param row + * @param params + * @param pojoClass + * @return + */ + private boolean verifyingDataValidity(Object object, Row row, ImportParams params, + Class pojoClass) { + boolean isAdd = true; + Cell cell = null; + if (params.isNeedVerfiy()) { + String errorMsg = PoiValidationUtil.validation(object); + if (StringUtils.isNotEmpty(errorMsg)) { + cell = row.createCell(row.getLastCellNum()); + cell.setCellValue(errorMsg); + if (object instanceof IExcelModel) { + IExcelModel model = (IExcelModel) object; + model.setErrorMsg(errorMsg); + } else { + isAdd = false; + } + verfiyFail = true; + } + } + if (params.getVerifyHanlder() != null) { + ExcelVerifyHanlderResult result = params.getVerifyHanlder().verifyHandler(object); + if (!result.isSuccess()) { + if (cell == null) + cell = row.createCell(row.getLastCellNum()); + cell.setCellValue((StringUtils.isNoneBlank(cell.getStringCellValue()) + ? cell.getStringCellValue() + "," : "") + result.getMsg()); + if (object instanceof IExcelModel) { + IExcelModel model = (IExcelModel) object; + model.setErrorMsg((StringUtils.isNoneBlank(model.getErrorMsg()) + ? model.getErrorMsg() + "," : "") + result.getMsg()); + } else { + isAdd = false; + } + verfiyFail = true; + } + } + if (cell != null) + cell.setCellStyle(errorCellStyle); + return isAdd; + } - /** - * 获取表格字段列名对应信息 - * @param rows - * @param params - * @param excelCollection - * @return - */ - private Map getTitleMap(Iterator rows, ImportParams params, - List excelCollection) { - Map titlemap = new HashMap(); - Iterator cellTitle; - String collectionName = null; - ExcelCollectionParams collectionParams = null; - Row row = null; - for (int j = 0; j < params.getHeadRows(); j++) { - row = rows.next(); - if (row == null) { - continue; - } - cellTitle = row.cellIterator(); - while (cellTitle.hasNext()) { - Cell cell = cellTitle.next(); - String value = getKeyValue(cell); - int i = cell.getColumnIndex(); - //用以支持重名导入 - if (StringUtils.isNotEmpty(value)) { - if (titlemap.containsKey(i)) { - collectionName = titlemap.get(i); - collectionParams = getCollectionParams(excelCollection, collectionName); - titlemap.put(i, collectionName + "_" + value); - } else if (StringUtils.isNotEmpty(collectionName) && collectionParams - .getExcelParams().containsKey(collectionName + "_" + value)) { - titlemap.put(i, collectionName + "_" + value); - } else { - collectionName = null; - collectionParams = null; - } - if (StringUtils.isEmpty(collectionName)) { - titlemap.put(i, value); - } - } - } - } - return titlemap; - } + /** + * 获取表格字段列名对应信息 + * @param rows + * @param params + * @param excelCollection + * @return + */ + private Map getTitleMap(Iterator rows, ImportParams params, + List excelCollection) { + Map titlemap = new HashMap(); + Iterator cellTitle; + String collectionName = null; + ExcelCollectionParams collectionParams = null; + Row row = null; + for (int j = 0; j < params.getHeadRows(); j++) { + row = rows.next(); + if (row == null) { + continue; + } + cellTitle = row.cellIterator(); + while (cellTitle.hasNext()) { + Cell cell = cellTitle.next(); + String value = getKeyValue(cell); + value = value.replace("\n", ""); + int i = cell.getColumnIndex(); + //用以支持重名导入 + if (StringUtils.isNotEmpty(value)) { + if (titlemap.containsKey(i)) { + collectionName = titlemap.get(i); + collectionParams = getCollectionParams(excelCollection, collectionName); + titlemap.put(i, collectionName + "_" + value); + } else if (StringUtils.isNotEmpty(collectionName) && collectionParams != null + && collectionParams.getExcelParams() + .containsKey(collectionName + "_" + value)) { + titlemap.put(i, collectionName + "_" + value); + } else { + collectionName = null; + collectionParams = null; + } + if (StringUtils.isEmpty(collectionName)) { + titlemap.put(i, value); + } + } + } + } + return titlemap; + } - /** - * 获取这个名称对应的集合信息 - * @param excelCollection - * @param collectionName - * @return - */ - private ExcelCollectionParams getCollectionParams(List excelCollection, - String collectionName) { - for (ExcelCollectionParams excelCollectionParams : excelCollection) { - if (collectionName.equals(excelCollectionParams.getExcelName())) { - return excelCollectionParams; - } - } - return null; - } + /** + * 获取这个名称对应的集合信息 + * @param excelCollection + * @param collectionName + * @return + */ + private ExcelCollectionParams getCollectionParams(List excelCollection, + String collectionName) { + for (ExcelCollectionParams excelCollectionParams : excelCollection) { + if (collectionName.equals(excelCollectionParams.getExcelName())) { + return excelCollectionParams; + } + } + return null; + } - /** - * Excel 导入 field 字段类型 Integer,Long,Double,Date,String,Boolean - * - * @param inputstream - * @param pojoClass - * @param params - * @return - * @throws Exception - */ - public ExcelImportResult importExcelByIs(InputStream inputstream, Class pojoClass, - ImportParams params) throws Exception { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Excel import start ,class is {}", pojoClass); - } - List result = new ArrayList(); - Workbook book = null; - boolean isXSSFWorkbook = true; - if (!(inputstream.markSupported())) { - inputstream = new PushbackInputStream(inputstream, 8); - } - if (POIFSFileSystem.hasPOIFSHeader(inputstream)) { - book = new HSSFWorkbook(inputstream); - isXSSFWorkbook = false; - } else if (POIXMLDocument.hasOOXMLHeader(inputstream)) { - book = new XSSFWorkbook(OPCPackage.open(inputstream)); - } - createErrorCellStyle(book); - Map pictures; - for (int i = params.getStartSheetIndex(); i < params.getStartSheetIndex() - + params.getSheetNum(); i++) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(" start to read excel by is ,startTime is {}", new Date().getTime()); - } - if (isXSSFWorkbook) { - pictures = PoiPublicUtil.getSheetPictrues07((XSSFSheet) book.getSheetAt(i), - (XSSFWorkbook) book); - } else { - pictures = PoiPublicUtil.getSheetPictrues03((HSSFSheet) book.getSheetAt(i), - (HSSFWorkbook) book); - } - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(" end to read excel by is ,endTime is {}", new Date().getTime()); - } - result.addAll(importExcel(result, book.getSheetAt(i), pojoClass, params, pictures)); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(" end to read excel list by pos ,endTime is {}", new Date().getTime()); - } - } - if (params.isNeedSave()) { - saveThisExcel(params, pojoClass, isXSSFWorkbook, book); - } - return new ExcelImportResult(result, verfiyFail, book); - } + /** + * Excel 导入 field 字段类型 Integer,Long,Double,Date,String,Boolean + * + * @param inputstream + * @param pojoClass + * @param params + * @return + * @throws Exception + */ + public ExcelImportResult importExcelByIs(InputStream inputstream, Class pojoClass, + ImportParams params) throws Exception { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Excel import start ,class is {}", pojoClass); + } + List result = new ArrayList(); + Workbook book = null; + boolean isXSSFWorkbook = true; + if (!(inputstream.markSupported())) { + inputstream = new PushbackInputStream(inputstream, 8); + } + if (POIFSFileSystem.hasPOIFSHeader(inputstream)) { + book = new HSSFWorkbook(inputstream); + isXSSFWorkbook = false; + } else if (POIXMLDocument.hasOOXMLHeader(inputstream)) { + book = new XSSFWorkbook(OPCPackage.open(inputstream)); + } + createErrorCellStyle(book); + Map pictures; + for (int i = params.getStartSheetIndex(); i < params.getStartSheetIndex() + + params.getSheetNum(); i++) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(" start to read excel by is ,startTime is {}", new Date().getTime()); + } + if (isXSSFWorkbook) { + pictures = PoiPublicUtil.getSheetPictrues07((XSSFSheet) book.getSheetAt(i), + (XSSFWorkbook) book); + } else { + pictures = PoiPublicUtil.getSheetPictrues03((HSSFSheet) book.getSheetAt(i), + (HSSFWorkbook) book); + } + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(" end to read excel by is ,endTime is {}", new Date().getTime()); + } + result.addAll(importExcel(result, book.getSheetAt(i), pojoClass, params, pictures)); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(" end to read excel list by pos ,endTime is {}", new Date().getTime()); + } + } + if (params.isNeedSave()) { + saveThisExcel(params, pojoClass, isXSSFWorkbook, book); + } + return new ExcelImportResult(result, verfiyFail, book); + } - /** - * 检查是不是合法的模板 - * @param titlemap - * @param excelParams - * @param params - * @param excelCollection - */ - private void checkIsValidTemplate(Map titlemap, - Map excelParams, - ImportParams params, - List excelCollection) { + /** + * 检查是不是合法的模板 + * @param titlemap + * @param excelParams + * @param params + * @param excelCollection + */ + private void checkIsValidTemplate(Map titlemap, + Map excelParams, + ImportParams params, + List excelCollection) { - if (params.getImportFields() != null) { - for (int i = 0, le = params.getImportFields().length; i < le; i++) { - if (!titlemap.containsValue(params.getImportFields()[i])) { - throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE); - } - } - } else { - Collection collection = excelParams.values(); - for (ExcelImportEntity excelImportEntity : collection) { - if (excelImportEntity.isImportField() - && !titlemap.containsValue(excelImportEntity.getName())) { - LOGGER.error(excelImportEntity.getName() + "必须有,但是没找到"); - throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE); - } - } + if (params.getImportFields() != null) { + for (int i = 0, le = params.getImportFields().length; i < le; i++) { + if (!titlemap.containsValue(params.getImportFields()[i])) { + throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE); + } + } + } else { + Collection collection = excelParams.values(); + for (ExcelImportEntity excelImportEntity : collection) { + if (excelImportEntity.isImportField() + && !titlemap.containsValue(excelImportEntity.getName())) { + LOGGER.error(excelImportEntity.getName() + "必须有,但是没找到"); + throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE); + } + } - for (int i = 0, le = excelCollection.size(); i < le; i++) { - ExcelCollectionParams collectionparams = excelCollection.get(i); - collection = collectionparams.getExcelParams().values(); - for (ExcelImportEntity excelImportEntity : collection) { - if (excelImportEntity.isImportField() && !titlemap.containsValue( - collectionparams.getExcelName() + "_" + excelImportEntity.getName())) { - throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE); - } - } - } - } - } + for (int i = 0, le = excelCollection.size(); i < le; i++) { + ExcelCollectionParams collectionparams = excelCollection.get(i); + collection = collectionparams.getExcelParams().values(); + for (ExcelImportEntity excelImportEntity : collection) { + if (excelImportEntity.isImportField() && !titlemap.containsValue( + collectionparams.getExcelName() + "_" + excelImportEntity.getName())) { + throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE); + } + } + } + } + } - /** - * 保存字段值(获取值,校验值,追加错误信息) - * - * @param params - * @param object - * @param cell - * @param excelParams - * @param titleString - * @param row - * @throws Exception - */ - private void saveFieldValue(ImportParams params, Object object, Cell cell, - Map excelParams, String titleString, - Row row) throws Exception { - Object value = cellValueServer.getValue(params.getDataHanlder(), object, cell, excelParams, - titleString); - if (object instanceof Map) { - if (params.getDataHanlder() != null) { - params.getDataHanlder().setMapValue((Map) object, titleString, value); - } else { - ((Map) object).put(titleString, value); - } - } else { - setValues(excelParams.get(titleString), object, value); - } - } + /** + * 保存字段值(获取值,校验值,追加错误信息) + * + * @param params + * @param object + * @param cell + * @param excelParams + * @param titleString + * @param row + * @throws Exception + */ + private void saveFieldValue(ImportParams params, Object object, Cell cell, + Map excelParams, String titleString, + Row row) throws Exception { + Object value = cellValueServer.getValue(params.getDataHanlder(), object, cell, excelParams, + titleString); + if (object instanceof Map) { + if (params.getDataHanlder() != null) { + params.getDataHanlder().setMapValue((Map) object, titleString, value); + } else { + ((Map) object).put(titleString, value); + } + } else { + setValues(excelParams.get(titleString), object, value); + } + } - /** - * - * @param object - * @param picId - * @param excelParams - * @param titleString - * @param pictures - * @param params - * @throws Exception - */ - private void saveImage(Object object, String picId, Map excelParams, - String titleString, Map pictures, - ImportParams params) throws Exception { - if (pictures == null) { - return; - } - PictureData image = pictures.get(picId); - if(image == null) { - return; - } - byte[] data = image.getData(); - 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)); - File savefile = new File(path); - if (!savefile.exists()) { - savefile.mkdirs(); - } - savefile = new File(path + "/" + fileName); - FileOutputStream fos = new FileOutputStream(savefile); - try { - fos.write(data); - } finally { - IOUtils.closeQuietly(fos); - } - setValues(excelParams.get(titleString), object, - getSaveUrl(excelParams.get(titleString), object) + "/" + fileName); - } else { - setValues(excelParams.get(titleString), object, data); - } - } + /** + * + * @param object + * @param picId + * @param excelParams + * @param titleString + * @param pictures + * @param params + * @throws Exception + */ + private void saveImage(Object object, String picId, Map excelParams, + String titleString, Map pictures, + ImportParams params) throws Exception { + if (pictures == null) { + return; + } + PictureData image = pictures.get(picId); + if (image == null) { + return; + } + byte[] data = image.getData(); + 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)); + File savefile = new File(path); + if (!savefile.exists()) { + savefile.mkdirs(); + } + savefile = new File(path + "/" + fileName); + FileOutputStream fos = new FileOutputStream(savefile); + try { + fos.write(data); + } finally { + IOUtils.closeQuietly(fos); + } + setValues(excelParams.get(titleString), object, + getSaveUrl(excelParams.get(titleString), object) + "/" + fileName); + } else { + setValues(excelParams.get(titleString), object, data); + } + } - private void createErrorCellStyle(Workbook workbook) { - errorCellStyle = workbook.createCellStyle(); - Font font = workbook.createFont(); - font.setColor(Font.COLOR_RED); - errorCellStyle.setFont(font); - } + private void createErrorCellStyle(Workbook workbook) { + errorCellStyle = workbook.createCellStyle(); + Font font = workbook.createFont(); + font.setColor(Font.COLOR_RED); + errorCellStyle.setFont(font); + } } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/handler/inter/IExcelExportServer.java b/easypoi-base/src/main/java/org/jeecgframework/poi/handler/inter/IExcelExportServer.java new file mode 100644 index 0000000..e15104b --- /dev/null +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/handler/inter/IExcelExportServer.java @@ -0,0 +1,14 @@ +package org.jeecgframework.poi.handler.inter; + +import java.util.List; + +/** + * 导出数据接口 + * @author JueYue + * 2016年9月8日 + */ +public interface IExcelExportServer { + + public List selectListForExcelExport(Object obj, int page); + +} diff --git a/easypoi-web/pom.xml b/easypoi-web/pom.xml index b62df07..caed046 100644 --- a/easypoi-web/pom.xml +++ b/easypoi-web/pom.xml @@ -4,7 +4,7 @@ org.jeecg easypoi - 2.3.1 + 2.3.2-SNAPSHOT easypoi-web diff --git a/easypoi-web/src/main/java/org/jeecgframework/poi/excel/entity/vo/BigExcelConstants.java b/easypoi-web/src/main/java/org/jeecgframework/poi/excel/entity/vo/BigExcelConstants.java new file mode 100644 index 0000000..646dbd1 --- /dev/null +++ b/easypoi-web/src/main/java/org/jeecgframework/poi/excel/entity/vo/BigExcelConstants.java @@ -0,0 +1,37 @@ +/** + * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jeecgframework.poi.excel.entity.vo; + +/** + * 正常导出Excel + * @author JueYue on 14-3-8. + * 静态常量 + */ +public interface BigExcelConstants extends BasePOIConstants { + /** + * 单Sheet导出 + */ + public final static String BIG_EXCEL_VIEW = "bigExcelView"; + /** + * 查询参数 + */ + public final static String DATA_PARAMS = "dataParams"; + /** + * 查询函数 + */ + public final static String DATA_INTER = "dataInterf"; + +} diff --git a/easypoi-web/src/main/java/org/jeecgframework/poi/excel/view/BigExcelExportView.java b/easypoi-web/src/main/java/org/jeecgframework/poi/excel/view/BigExcelExportView.java new file mode 100644 index 0000000..a60c88c --- /dev/null +++ b/easypoi-web/src/main/java/org/jeecgframework/poi/excel/view/BigExcelExportView.java @@ -0,0 +1,83 @@ +/** + * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jeecgframework.poi.excel.view; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; +import org.jeecgframework.poi.excel.ExcelExportUtil; +import org.jeecgframework.poi.excel.entity.ExportParams; +import org.jeecgframework.poi.excel.entity.vo.BigExcelConstants; +import org.jeecgframework.poi.handler.inter.IExcelExportServer; +import org.springframework.stereotype.Controller; + +/** + * @author JueYue on 14-3-8. Excel 生成解析器,减少用户操作 + */ +@Controller(BigExcelConstants.BIG_EXCEL_VIEW) +public class BigExcelExportView extends MiniAbstractExcelView { + + public BigExcelExportView() { + super(); + } + + @Override + protected void renderMergedOutputModel(Map model, HttpServletRequest request, + HttpServletResponse response) throws Exception { + String codedFileName = "临时文件"; + Workbook workbook = ExcelExportUtil.exportBigExcel( + (ExportParams) model.get(BigExcelConstants.PARAMS), + (Class) model.get(BigExcelConstants.CLASS), Collections.EMPTY_LIST); + IExcelExportServer server = (IExcelExportServer) model.get(BigExcelConstants.DATA_INTER); + int page = 1; + List list = server + .selectListForExcelExport(model.get(BigExcelConstants.DATA_PARAMS), page++); + while (list != null && list.size() > 0) { + workbook = ExcelExportUtil.exportBigExcel( + (ExportParams) model.get(BigExcelConstants.PARAMS), + (Class) model.get(BigExcelConstants.CLASS), list); + list = server.selectListForExcelExport(model.get(BigExcelConstants.DATA_PARAMS), + page++); + } + ExcelExportUtil.closeExportBigExcel(); + if (model.containsKey(BigExcelConstants.FILE_NAME)) { + codedFileName = (String) model.get(BigExcelConstants.FILE_NAME); + } + if (workbook instanceof HSSFWorkbook) { + codedFileName += HSSF; + } else { + codedFileName += XSSF; + } + if (isIE(request)) { + codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8"); + } else { + codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1"); + } + response.setHeader("content-disposition", "attachment;filename=" + codedFileName); + ServletOutputStream out = response.getOutputStream(); + workbook.write(out); + out.flush(); + } +} diff --git a/pom.xml b/pom.xml index b916945..8c3c94d 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.jeecg easypoi - 2.3.1 + 2.3.2-SNAPSHOT pom easypoi @@ -38,7 +38,7 @@ - 2.3.1 + 2.3.2-SNAPSHOT 3.10-FINAL 2.9.1 16.0.1 @@ -206,50 +206,8 @@ UTF-8 - - org.apache.maven.plugins - maven-source-plugin - 2.2.1 - - - attach-sources - - jar-no-fork - - - - - UTF-8 - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9 - - - attach-javadocs - - jar - - - - - UTF-8 - - - - - oss-release - https://oss.sonatype.org/service/local/staging/deploy/maven2 - - - oss-snapshot - https://oss.sonatype.org/content/repositories/snapshots - - - + \ No newline at end of file