| @@ -55,7 +55,7 @@ public @interface Excel { | |||||
| /** | /** | ||||
| * 导出时在excel中每个列的高度 单位为字符,一个汉字=2个字符 | * 导出时在excel中每个列的高度 单位为字符,一个汉字=2个字符 | ||||
| * 优先选择@ExcelEntity中的 height | |||||
| * 优先选择@ExportParams中的 height | |||||
| */ | */ | ||||
| @Deprecated | @Deprecated | ||||
| public double height() default 10; | public double height() default 10; | ||||
| @@ -91,6 +91,13 @@ public @interface Excel { | |||||
| */ | */ | ||||
| public String name(); | public String name(); | ||||
| /** | |||||
| * 导出时,表头双行显示,聚合,排序以最小的值参与总体排序再内部排序 | |||||
| * 导出排序跟定义了annotation的字段的顺序有关 可以使用a_id,b_id来确实是否使用 | |||||
| * 优先弱与 @ExcelEntity 的name和show属性 | |||||
| */ | |||||
| public String groupName() default ""; | |||||
| /** | /** | ||||
| * 是否需要纵向合并单元格(用于含有list中,单个的单元格,合并list创建的多个row) | * 是否需要纵向合并单元格(用于含有list中,单个的单元格,合并list创建的多个row) | ||||
| */ | */ | ||||
| @@ -42,14 +42,9 @@ public @interface ExcelEntity { | |||||
| /** | /** | ||||
| * 如果等于true,name必须有值, Excel的表头会变成两行,同时改Excel内部数据不参与总排序,排序用下面这个来代替,内部再排序 | * 如果等于true,name必须有值, Excel的表头会变成两行,同时改Excel内部数据不参与总排序,排序用下面这个来代替,内部再排序 | ||||
| * 排序取当中最小值排序 | |||||
| * @return | * @return | ||||
| */ | */ | ||||
| public boolean show() default false; | public boolean show() default false; | ||||
| /** | |||||
| * 展示到第几个同样可以使用a_id,b_id | |||||
| * | |||||
| */ | |||||
| public String orderNum() default "0"; | |||||
| } | } | ||||
| @@ -34,14 +34,4 @@ public @interface ExcelTarget { | |||||
| */ | */ | ||||
| public String value(); | public String value(); | ||||
| /** | |||||
| * 导出时在excel中每个列的高度 单位为字符,一个汉字=2个字符 | |||||
| * 全局设置,优先使用 | |||||
| */ | |||||
| public double height() default 10; | |||||
| /** | |||||
| * 导出时在excel中每个列的高度 单位为字符,一个汉字=2个字符 | |||||
| * 全局设置,优先使用 | |||||
| */ | |||||
| public short fontSize() default 11; | |||||
| } | } | ||||
| @@ -75,18 +75,21 @@ public class ExcelExportUtil { | |||||
| */ | */ | ||||
| public static Workbook exportExcel(ExportParams entity, Class<?> pojoClass, | public static Workbook exportExcel(ExportParams entity, Class<?> pojoClass, | ||||
| Collection<?> dataSet) { | Collection<?> dataSet) { | ||||
| Workbook workbook; | |||||
| if (ExcelType.HSSF.equals(entity.getType())) { | |||||
| workbook = new HSSFWorkbook(); | |||||
| } else if (dataSet.size() < 10000) { | |||||
| workbook = new XSSFWorkbook(); | |||||
| } else { | |||||
| workbook = new SXSSFWorkbook(); | |||||
| } | |||||
| Workbook workbook = getWorkbook(entity.getType(),dataSet.size()); | |||||
| new ExcelExportServer().createSheet(workbook, entity, pojoClass, dataSet); | new ExcelExportServer().createSheet(workbook, entity, pojoClass, dataSet); | ||||
| return workbook; | return workbook; | ||||
| } | } | ||||
| private static Workbook getWorkbook(ExcelType type, int size) { | |||||
| if (ExcelType.HSSF.equals(type)) { | |||||
| return new HSSFWorkbook(); | |||||
| } else if (size < 100000) { | |||||
| return new XSSFWorkbook(); | |||||
| } else { | |||||
| return new SXSSFWorkbook(); | |||||
| } | |||||
| } | |||||
| /** | /** | ||||
| * 根据Map创建对应的Excel | * 根据Map创建对应的Excel | ||||
| * @param entity | * @param entity | ||||
| @@ -98,14 +101,7 @@ public class ExcelExportUtil { | |||||
| */ | */ | ||||
| public static Workbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList, | public static Workbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList, | ||||
| Collection<? extends Map<?, ?>> dataSet) { | Collection<? extends Map<?, ?>> dataSet) { | ||||
| Workbook workbook; | |||||
| if (ExcelType.HSSF.equals(entity.getType())) { | |||||
| workbook = new HSSFWorkbook(); | |||||
| } else if (dataSet.size() < 10000) { | |||||
| workbook = new XSSFWorkbook(); | |||||
| } else { | |||||
| workbook = new SXSSFWorkbook(); | |||||
| } | |||||
| Workbook workbook = getWorkbook(entity.getType(),dataSet.size());; | |||||
| new ExcelExportServer().createSheetForMap(workbook, entity, entityList, dataSet); | new ExcelExportServer().createSheetForMap(workbook, entity, entityList, dataSet); | ||||
| return workbook; | return workbook; | ||||
| } | } | ||||
| @@ -119,12 +115,7 @@ public class ExcelExportUtil { | |||||
| * @return | * @return | ||||
| */ | */ | ||||
| public static Workbook exportExcel(List<Map<String, Object>> list, ExcelType type) { | public static Workbook exportExcel(List<Map<String, Object>> list, ExcelType type) { | ||||
| Workbook workbook; | |||||
| if (ExcelType.HSSF.equals(type)) { | |||||
| workbook = new HSSFWorkbook(); | |||||
| } else { | |||||
| workbook = new XSSFWorkbook(); | |||||
| } | |||||
| Workbook workbook = getWorkbook(type,0); | |||||
| for (Map<String, Object> map : list) { | for (Map<String, Object> map : list) { | ||||
| ExcelExportServer server = new ExcelExportServer(); | ExcelExportServer server = new ExcelExportServer(); | ||||
| server.createSheet(workbook, (ExportParams) map.get("title"), | server.createSheet(workbook, (ExportParams) map.get("title"), | ||||
| @@ -101,6 +101,12 @@ public class ExportParams extends ExcelBaseParams { | |||||
| */ | */ | ||||
| private int maxNum = 0; | private int maxNum = 0; | ||||
| /** | |||||
| * 导出时在excel中每个列的高度 单位为字符,一个汉字=2个字符 | |||||
| * 全局设置,优先使用 | |||||
| */ | |||||
| public short height = 0; | |||||
| public ExportParams() { | public ExportParams() { | ||||
| } | } | ||||
| @@ -258,4 +264,12 @@ public class ExportParams extends ExcelBaseParams { | |||||
| this.maxNum = maxNum; | this.maxNum = maxNum; | ||||
| } | } | ||||
| public short getHeight() { | |||||
| return (short) (height * 50); | |||||
| } | |||||
| public void setHeight(short height) { | |||||
| this.height = height; | |||||
| } | |||||
| } | } | ||||
| @@ -30,6 +30,10 @@ public class ExcelBaseEntity { | |||||
| * 对应name | * 对应name | ||||
| */ | */ | ||||
| protected String name; | protected String name; | ||||
| /** | |||||
| * 对应groupName | |||||
| */ | |||||
| protected String groupName; | |||||
| /** | /** | ||||
| * 对应type | * 对应type | ||||
| */ | */ | ||||
| @@ -117,6 +121,15 @@ public class ExcelBaseEntity { | |||||
| return hyperlink; | return hyperlink; | ||||
| } | } | ||||
| public String getGroupName() { | |||||
| return groupName; | |||||
| } | |||||
| public void setGroupName(String groupName) { | |||||
| this.groupName = groupName; | |||||
| } | |||||
| public void setHyperlink(boolean hyperlink) { | public void setHyperlink(boolean hyperlink) { | ||||
| this.hyperlink = hyperlink; | this.hyperlink = hyperlink; | ||||
| } | } | ||||
| @@ -72,7 +72,7 @@ public class ExcelBatchExportServer extends ExcelExportServer { | |||||
| ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); | ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); | ||||
| String targetId = etarget == null ? null : etarget.value(); | String targetId = etarget == null ? null : etarget.value(); | ||||
| getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, | getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, | ||||
| null); | |||||
| null, null); | |||||
| sortAllParams(excelParams); | sortAllParams(excelParams); | ||||
| try { | try { | ||||
| sheet = workbook.createSheet(entity.getSheetName()); | sheet = workbook.createSheet(entity.getSheetName()); | ||||
| @@ -81,8 +81,7 @@ public class ExcelBatchExportServer extends ExcelExportServer { | |||||
| sheet = workbook.createSheet(); | sheet = workbook.createSheet(); | ||||
| } | } | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| LOGGER.error(e.getMessage(), e); | |||||
| throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause()); | |||||
| throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e); | |||||
| } | } | ||||
| } | } | ||||
| @@ -1,13 +1,13 @@ | |||||
| /** | /** | ||||
| * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) | * 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 | |||||
| * 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 | |||||
| * | * | ||||
| * Unless required by applicable law or agreed to in writing, software | |||||
| * 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, | * distributed under the License is distributed on an "AS IS" BASIS, | ||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| * See the License for the specific language governing permissions and | * See the License for the specific language governing permissions and | ||||
| @@ -34,7 +34,7 @@ import cn.afterturn.easypoi.excel.annotation.ExcelTarget; | |||||
| import cn.afterturn.easypoi.excel.entity.ExportParams; | import cn.afterturn.easypoi.excel.entity.ExportParams; | ||||
| import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; | import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; | ||||
| import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | ||||
| import cn.afterturn.easypoi.excel.export.base.ExcelExportBase; | |||||
| import cn.afterturn.easypoi.excel.export.base.ExportBaseServer; | |||||
| import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; | import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; | ||||
| import cn.afterturn.easypoi.exception.excel.ExcelExportException; | import cn.afterturn.easypoi.exception.excel.ExcelExportException; | ||||
| import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum; | import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum; | ||||
| @@ -43,20 +43,19 @@ import cn.afterturn.easypoi.util.PoiPublicUtil; | |||||
| /** | /** | ||||
| * Excel导出服务 | * Excel导出服务 | ||||
| * | |||||
| * @author JueYue | |||||
| * 2014年6月17日 下午5:30:54 | |||||
| * | |||||
| * @author JueYue 2014年6月17日 下午5:30:54 | |||||
| */ | */ | ||||
| public class ExcelExportServer extends ExcelExportBase { | |||||
| public class ExcelExportServer extends ExportBaseServer { | |||||
| // 最大行数,超过自动多Sheet | // 最大行数,超过自动多Sheet | ||||
| private int MAX_NUM = 60000; | |||||
| private int MAX_NUM = 60000; | |||||
| protected int createHeaderAndTitle(ExportParams entity, Sheet sheet, Workbook workbook, | protected int createHeaderAndTitle(ExportParams entity, Sheet sheet, Workbook workbook, | ||||
| List<ExcelExportEntity> excelParams) { | |||||
| int rows = 0, feildWidth = getFieldLength(excelParams); | |||||
| List<ExcelExportEntity> excelParams) { | |||||
| int rows = 0, fieldLength = getFieldLength(excelParams); | |||||
| if (entity.getTitle() != null) { | if (entity.getTitle() != null) { | ||||
| rows += createHeaderRow(entity, sheet, workbook, feildWidth); | |||||
| rows += createHeaderRow(entity, sheet, workbook, fieldLength); | |||||
| } | } | ||||
| rows += createTitleRow(entity, sheet, workbook, rows, excelParams); | rows += createTitleRow(entity, sheet, workbook, rows, excelParams); | ||||
| sheet.createFreezePane(0, rows, 0, rows); | sheet.createFreezePane(0, rows, 0, rows); | ||||
| @@ -65,35 +64,30 @@ public class ExcelExportServer extends ExcelExportBase { | |||||
| /** | /** | ||||
| * 创建 表头改变 | * 创建 表头改变 | ||||
| * | |||||
| * @param entity | |||||
| * @param sheet | |||||
| * @param workbook | |||||
| * @param feildWidth | |||||
| */ | */ | ||||
| public int createHeaderRow(ExportParams entity, Sheet sheet, Workbook workbook, | public int createHeaderRow(ExportParams entity, Sheet sheet, Workbook workbook, | ||||
| int feildWidth) { | |||||
| int fieldWidth) { | |||||
| Row row = sheet.createRow(0); | Row row = sheet.createRow(0); | ||||
| row.setHeight(entity.getTitleHeight()); | row.setHeight(entity.getTitleHeight()); | ||||
| createStringCell(row, 0, entity.getTitle(), | createStringCell(row, 0, entity.getTitle(), | ||||
| getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); | |||||
| for (int i = 1; i <= feildWidth; i++) { | |||||
| createStringCell(row, i, "", | |||||
| getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); | getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); | ||||
| for (int i = 1; i <= fieldWidth; i++) { | |||||
| createStringCell(row, i, "", | |||||
| getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); | |||||
| } | } | ||||
| sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, feildWidth)); | |||||
| sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, fieldWidth)); | |||||
| if (entity.getSecondTitle() != null) { | if (entity.getSecondTitle() != null) { | ||||
| row = sheet.createRow(1); | row = sheet.createRow(1); | ||||
| row.setHeight(entity.getSecondTitleHeight()); | row.setHeight(entity.getSecondTitleHeight()); | ||||
| CellStyle style = workbook.createCellStyle(); | CellStyle style = workbook.createCellStyle(); | ||||
| style.setAlignment(CellStyle.ALIGN_RIGHT); | style.setAlignment(CellStyle.ALIGN_RIGHT); | ||||
| createStringCell(row, 0, entity.getSecondTitle(), style, null); | createStringCell(row, 0, entity.getSecondTitle(), style, null); | ||||
| for (int i = 1; i <= feildWidth; i++) { | |||||
| for (int i = 1; i <= fieldWidth; i++) { | |||||
| createStringCell(row, i, "", | createStringCell(row, i, "", | ||||
| getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); | |||||
| getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null); | |||||
| } | } | ||||
| sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, feildWidth)); | |||||
| sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, fieldWidth)); | |||||
| return 2; | return 2; | ||||
| } | } | ||||
| return 1; | return 1; | ||||
| @@ -104,7 +98,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||||
| if (LOGGER.isDebugEnabled()) { | if (LOGGER.isDebugEnabled()) { | ||||
| LOGGER.debug("Excel export start ,class is {}", pojoClass); | LOGGER.debug("Excel export start ,class is {}", pojoClass); | ||||
| LOGGER.debug("Excel version is {}", | LOGGER.debug("Excel version is {}", | ||||
| entity.getType().equals(ExcelType.HSSF) ? "03" : "07"); | |||||
| entity.getType().equals(ExcelType.HSSF) ? "03" : "07"); | |||||
| } | } | ||||
| if (workbook == null || entity == null || pojoClass == null || dataSet == null) { | if (workbook == null || entity == null || pojoClass == null || dataSet == null) { | ||||
| throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); | throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); | ||||
| @@ -112,11 +106,11 @@ public class ExcelExportServer extends ExcelExportBase { | |||||
| try { | try { | ||||
| List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | ||||
| // 得到所有字段 | // 得到所有字段 | ||||
| Field fileds[] = PoiPublicUtil.getClassFields(pojoClass); | |||||
| Field[] fileds = PoiPublicUtil.getClassFields(pojoClass); | |||||
| ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); | ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); | ||||
| String targetId = etarget == null ? null : etarget.value(); | String targetId = etarget == null ? null : etarget.value(); | ||||
| getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, | getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, | ||||
| null); | |||||
| null, null); | |||||
| //获取所有参数后,后面的逻辑判断就一致了 | //获取所有参数后,后面的逻辑判断就一致了 | ||||
| createSheetForMap(workbook, entity, excelParams, dataSet); | createSheetForMap(workbook, entity, excelParams, dataSet); | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| @@ -129,7 +123,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||||
| List<ExcelExportEntity> entityList, Collection<?> dataSet) { | List<ExcelExportEntity> entityList, Collection<?> dataSet) { | ||||
| if (LOGGER.isDebugEnabled()) { | if (LOGGER.isDebugEnabled()) { | ||||
| LOGGER.debug("Excel version is {}", | LOGGER.debug("Excel version is {}", | ||||
| entity.getType().equals(ExcelType.HSSF) ? "03" : "07"); | |||||
| entity.getType().equals(ExcelType.HSSF) ? "03" : "07"); | |||||
| } | } | ||||
| if (workbook == null || entity == null || entityList == null || dataSet == null) { | if (workbook == null || entity == null || entityList == null || dataSet == null) { | ||||
| throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); | throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); | ||||
| @@ -138,8 +132,8 @@ public class ExcelExportServer extends ExcelExportBase { | |||||
| if (type.equals(ExcelType.XSSF)) { | if (type.equals(ExcelType.XSSF)) { | ||||
| MAX_NUM = 1000000; | MAX_NUM = 1000000; | ||||
| } | } | ||||
| if( entity.getMaxNum() >0) { | |||||
| MAX_NUM = entity.getMaxNum(); | |||||
| if (entity.getMaxNum() > 0) { | |||||
| MAX_NUM = entity.getMaxNum(); | |||||
| } | } | ||||
| Sheet sheet = null; | Sheet sheet = null; | ||||
| try { | try { | ||||
| @@ -148,12 +142,12 @@ public class ExcelExportServer extends ExcelExportBase { | |||||
| // 重复遍历,出现了重名现象,创建非指定的名称Sheet | // 重复遍历,出现了重名现象,创建非指定的名称Sheet | ||||
| sheet = workbook.createSheet(); | sheet = workbook.createSheet(); | ||||
| } | } | ||||
| insertDataToSheet(workbook, entity, entityList, dataSet,sheet); | |||||
| insertDataToSheet(workbook, entity, entityList, dataSet, sheet); | |||||
| } | } | ||||
| protected void insertDataToSheet(Workbook workbook, ExportParams entity, | protected void insertDataToSheet(Workbook workbook, ExportParams entity, | ||||
| List<ExcelExportEntity> entityList, Collection<?> dataSet, | |||||
| Sheet sheet) { | |||||
| List<ExcelExportEntity> entityList, Collection<?> dataSet, | |||||
| Sheet sheet) { | |||||
| try { | try { | ||||
| dataHanlder = entity.getDataHanlder(); | dataHanlder = entity.getDataHanlder(); | ||||
| if (dataHanlder != null && dataHanlder.getNeedHandlerFields() != null) { | if (dataHanlder != null && dataHanlder.getNeedHandlerFields() != null) { | ||||
| @@ -161,7 +155,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||||
| } | } | ||||
| // 创建表格样式 | // 创建表格样式 | ||||
| setExcelExportStyler((IExcelExportStyler) entity.getStyle() | setExcelExportStyler((IExcelExportStyler) entity.getStyle() | ||||
| .getConstructor(Workbook.class).newInstance(workbook)); | |||||
| .getConstructor(Workbook.class).newInstance(workbook)); | |||||
| Drawing patriarch = PoiExcelGraphDataUtil.getDrawingPatriarch(sheet); | Drawing patriarch = PoiExcelGraphDataUtil.getDrawingPatriarch(sheet); | ||||
| List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | ||||
| if (entity.isAddIndex()) { | if (entity.isAddIndex()) { | ||||
| @@ -170,10 +164,10 @@ public class ExcelExportServer extends ExcelExportBase { | |||||
| excelParams.addAll(entityList); | excelParams.addAll(entityList); | ||||
| sortAllParams(excelParams); | sortAllParams(excelParams); | ||||
| int index = entity.isCreateHeadRows() | int index = entity.isCreateHeadRows() | ||||
| ? createHeaderAndTitle(entity, sheet, workbook, excelParams) : 0; | |||||
| ? createHeaderAndTitle(entity, sheet, workbook, excelParams) : 0; | |||||
| int titleHeight = index; | int titleHeight = index; | ||||
| setCellWith(excelParams, sheet); | setCellWith(excelParams, sheet); | ||||
| short rowHeight = getRowHeight(excelParams); | |||||
| short rowHeight = entity.getHeight() > 0? entity.getHeight() : getRowHeight(excelParams); | |||||
| setCurrentIndex(1); | setCurrentIndex(1); | ||||
| Iterator<?> its = dataSet.iterator(); | Iterator<?> its = dataSet.iterator(); | ||||
| List<Object> tempList = new ArrayList<Object>(); | List<Object> tempList = new ArrayList<Object>(); | ||||
| @@ -195,6 +189,10 @@ public class ExcelExportServer extends ExcelExportBase { | |||||
| its.next(); | its.next(); | ||||
| its.remove(); | its.remove(); | ||||
| } | } | ||||
| if (LOGGER.isDebugEnabled()) { | |||||
| LOGGER.debug("List data more than max ,data size is {}", | |||||
| dataSet.size()); | |||||
| } | |||||
| // 发现还有剩余list 继续循环创建Sheet | // 发现还有剩余list 继续循环创建Sheet | ||||
| if (dataSet.size() > 0) { | if (dataSet.size() > 0) { | ||||
| createSheetForMap(workbook, entity, entityList, dataSet); | createSheetForMap(workbook, entity, entityList, dataSet); | ||||
| @@ -211,9 +209,6 @@ public class ExcelExportServer extends ExcelExportBase { | |||||
| /** | /** | ||||
| * 创建表头 | * 创建表头 | ||||
| * | |||||
| * @param title | |||||
| * @param index | |||||
| */ | */ | ||||
| private int createTitleRow(ExportParams title, Sheet sheet, Workbook workbook, int index, | private int createTitleRow(ExportParams title, Sheet sheet, Workbook workbook, int index, | ||||
| List<ExcelExportEntity> excelParams) { | List<ExcelExportEntity> excelParams) { | ||||
| @@ -226,30 +221,41 @@ public class ExcelExportServer extends ExcelExportBase { | |||||
| listRow.setHeight((short) 450); | listRow.setHeight((short) 450); | ||||
| } | } | ||||
| int cellIndex = 0; | int cellIndex = 0; | ||||
| int groupCellLength = 0; | |||||
| CellStyle titleStyle = getExcelExportStyler().getTitleStyle(title.getColor()); | CellStyle titleStyle = getExcelExportStyler().getTitleStyle(title.getColor()); | ||||
| for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) { | for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) { | ||||
| ExcelExportEntity entity = excelParams.get(i); | ExcelExportEntity entity = excelParams.get(i); | ||||
| if (StringUtils.isNotBlank(entity.getName())) { | |||||
| if (StringUtils.isNotBlank(entity.getGroupName())) { | |||||
| createStringCell(row, cellIndex, entity.getGroupName(), titleStyle, entity); | |||||
| createStringCell(listRow, cellIndex, entity.getName(), titleStyle, entity); | |||||
| groupCellLength++; | |||||
| } else if (StringUtils.isNotBlank(entity.getName())) { | |||||
| if(groupCellLength > 0){ | |||||
| sheet.addMergedRegion(new CellRangeAddress(index, index, cellIndex, cellIndex - groupCellLength)); | |||||
| groupCellLength = 0; | |||||
| } | |||||
| createStringCell(row, cellIndex, entity.getName(), titleStyle, entity); | createStringCell(row, cellIndex, entity.getName(), titleStyle, entity); | ||||
| } | } | ||||
| if (entity.getList() != null) { | if (entity.getList() != null) { | ||||
| List<ExcelExportEntity> sTitel = entity.getList(); | List<ExcelExportEntity> sTitel = entity.getList(); | ||||
| if (StringUtils.isNotBlank(entity.getName())) { | if (StringUtils.isNotBlank(entity.getName())) { | ||||
| sheet.addMergedRegion(new CellRangeAddress(index, index, cellIndex, | |||||
| cellIndex + sTitel.size() - 1)); | |||||
| sheet.addMergedRegion(new CellRangeAddress(index, index, cellIndex, cellIndex + sTitel.size() - 1)); | |||||
| } | } | ||||
| for (int j = 0, size = sTitel.size(); j < size; j++) { | for (int j = 0, size = sTitel.size(); j < size; j++) { | ||||
| createStringCell(rows == 2 ? listRow : row, cellIndex, sTitel.get(j).getName(), | createStringCell(rows == 2 ? listRow : row, cellIndex, sTitel.get(j).getName(), | ||||
| titleStyle, entity); | |||||
| titleStyle, entity); | |||||
| cellIndex++; | cellIndex++; | ||||
| } | } | ||||
| cellIndex--; | cellIndex--; | ||||
| } else if (rows == 2) { | |||||
| } else if (rows == 2 && StringUtils.isBlank(entity.getGroupName())) { | |||||
| createStringCell(listRow, cellIndex, "", titleStyle, entity); | createStringCell(listRow, cellIndex, "", titleStyle, entity); | ||||
| sheet.addMergedRegion(new CellRangeAddress(index, index + 1, cellIndex, cellIndex)); | sheet.addMergedRegion(new CellRangeAddress(index, index + 1, cellIndex, cellIndex)); | ||||
| } | } | ||||
| cellIndex++; | cellIndex++; | ||||
| } | } | ||||
| if(groupCellLength > 0){ | |||||
| sheet.addMergedRegion(new CellRangeAddress(index, index, cellIndex - groupCellLength, cellIndex - 1)); | |||||
| } | |||||
| return rows; | return rows; | ||||
| } | } | ||||
| @@ -1,394 +1,407 @@ | |||||
| /** | |||||
| * 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 cn.afterturn.easypoi.excel.export.base; | |||||
| import org.apache.commons.lang3.StringUtils; | |||||
| import org.apache.poi.hssf.usermodel.HSSFClientAnchor; | |||||
| import org.apache.poi.hssf.usermodel.HSSFRichTextString; | |||||
| import org.apache.poi.ss.usermodel.Cell; | |||||
| import org.apache.poi.ss.usermodel.CellStyle; | |||||
| import org.apache.poi.ss.usermodel.ClientAnchor; | |||||
| import org.apache.poi.ss.usermodel.Drawing; | |||||
| import org.apache.poi.ss.usermodel.RichTextString; | |||||
| import org.apache.poi.ss.usermodel.Row; | |||||
| import org.apache.poi.ss.usermodel.Sheet; | |||||
| import org.apache.poi.ss.usermodel.Workbook; | |||||
| import org.apache.poi.ss.util.CellRangeAddress; | |||||
| import org.apache.poi.xssf.usermodel.XSSFClientAnchor; | |||||
| import org.apache.poi.xssf.usermodel.XSSFRichTextString; | |||||
| import java.text.DecimalFormat; | |||||
| import java.util.Collection; | |||||
| import java.util.HashMap; | |||||
| import java.util.List; | |||||
| import java.util.Map; | |||||
| import java.util.Set; | |||||
| import cn.afterturn.easypoi.cache.ImageCache; | |||||
| import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; | |||||
| import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | |||||
| import cn.afterturn.easypoi.excel.entity.vo.BaseEntityTypeConstants; | |||||
| import cn.afterturn.easypoi.excel.entity.vo.PoiBaseConstants; | |||||
| import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; | |||||
| import cn.afterturn.easypoi.util.PoiExcelGraphDataUtil; | |||||
| import cn.afterturn.easypoi.util.PoiMergeCellUtil; | |||||
| import cn.afterturn.easypoi.util.PoiPublicUtil; | |||||
| /** | |||||
| * 提供POI基础操作服务 | |||||
| * | |||||
| * @author JueYue 2014年6月17日 下午6:15:13 | |||||
| */ | |||||
| @SuppressWarnings("unchecked") | |||||
| public abstract class ExcelExportBase extends ExportBase { | |||||
| private int currentIndex = 0; | |||||
| protected ExcelType type = ExcelType.HSSF; | |||||
| private Map<Integer, Double> statistics = new HashMap<Integer, Double>(); | |||||
| private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00"); | |||||
| protected IExcelExportStyler excelExportStyler; | |||||
| /** | |||||
| * 创建 最主要的 Cells | |||||
| */ | |||||
| public int createCells(Drawing patriarch, int index, Object t, | |||||
| List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook, | |||||
| short rowHeight) throws Exception { | |||||
| ExcelExportEntity entity; | |||||
| Row row = sheet.createRow(index); | |||||
| row.setHeight(rowHeight); | |||||
| int maxHeight = 1, cellNum = 0; | |||||
| int indexKey = createIndexCell(row, index, excelParams.get(0)); | |||||
| cellNum += indexKey; | |||||
| for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { | |||||
| entity = excelParams.get(k); | |||||
| if (entity.getList() != null) { | |||||
| Collection<?> list = getListCellValue(entity, t); | |||||
| int listC = 0; | |||||
| if (list != null && list.size() > 0) { | |||||
| for (Object obj : list) { | |||||
| createListCells(patriarch, index + listC, cellNum, obj, entity.getList(), | |||||
| sheet, workbook); | |||||
| listC++; | |||||
| } | |||||
| } | |||||
| cellNum += entity.getList().size(); | |||||
| if (list != null && list.size() > maxHeight) { | |||||
| maxHeight = list.size(); | |||||
| } | |||||
| } else { | |||||
| Object value = getCellValue(entity, t); | |||||
| if (entity.getType() == BaseEntityTypeConstants.StringType) { | |||||
| createStringCell(row, cellNum++, value == null ? "" : value.toString(), | |||||
| index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), | |||||
| entity); | |||||
| if (entity.isHyperlink()) { | |||||
| row.getCell(cellNum - 1) | |||||
| .setHyperlink(dataHanlder.getHyperlink( | |||||
| row.getSheet().getWorkbook().getCreationHelper(), t, | |||||
| entity.getName(), value)); | |||||
| } | |||||
| } else if (entity.getType() == BaseEntityTypeConstants.DoubleType) { | |||||
| createDoubleCell(row, cellNum++, value == null ? "" : value.toString(), | |||||
| index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), | |||||
| entity); | |||||
| if (entity.isHyperlink()) { | |||||
| row.getCell(cellNum - 1) | |||||
| .setHyperlink(dataHanlder.getHyperlink( | |||||
| row.getSheet().getWorkbook().getCreationHelper(), t, | |||||
| entity.getName(), value)); | |||||
| } | |||||
| } else { | |||||
| createImageCell(patriarch, entity, row, cellNum++, | |||||
| value == null ? "" : value.toString(), t); | |||||
| } | |||||
| } | |||||
| } | |||||
| // 合并需要合并的单元格 | |||||
| cellNum = 0; | |||||
| for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { | |||||
| entity = excelParams.get(k); | |||||
| if (entity.getList() != null) { | |||||
| cellNum += entity.getList().size(); | |||||
| } else if (entity.isNeedMerge() && maxHeight > 1) { | |||||
| for (int i = index + 1; i < index + maxHeight; i++) { | |||||
| sheet.getRow(i).createCell(cellNum); | |||||
| sheet.getRow(i).getCell(cellNum).setCellStyle(getStyles(false, entity)); | |||||
| } | |||||
| sheet.addMergedRegion( | |||||
| new CellRangeAddress(index, index + maxHeight - 1, cellNum, cellNum)); | |||||
| cellNum++; | |||||
| } | |||||
| } | |||||
| return maxHeight; | |||||
| } | |||||
| /** | |||||
| * 图片类型的Cell | |||||
| */ | |||||
| public void createImageCell(Drawing patriarch, ExcelExportEntity entity, Row row, int i, | |||||
| String imagePath, Object obj) throws Exception { | |||||
| Cell cell = row.createCell(i); | |||||
| byte[] value = null; | |||||
| if (entity.getExportImageType() != 1) { | |||||
| value = (byte[]) (entity.getMethods() != null | |||||
| ? getFieldBySomeMethod(entity.getMethods(), obj) | |||||
| : entity.getMethod().invoke(obj, new Object[]{})); | |||||
| } | |||||
| createImageCell(cell, 50 * entity.getHeight(), entity.getExportImageType() == 1 ? imagePath : null, value); | |||||
| } | |||||
| /** | |||||
| * 图片类型的Cell | |||||
| */ | |||||
| public void createImageCell(Cell cell, double height, | |||||
| String imagePath, byte[] data) throws Exception { | |||||
| if (height > cell.getRow().getHeight()) { | |||||
| cell.getRow().setHeight((short) height); | |||||
| } | |||||
| ClientAnchor anchor; | |||||
| if (type.equals(ExcelType.HSSF)) { | |||||
| anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), | |||||
| cell.getRow().getRowNum() + 1); | |||||
| } else { | |||||
| anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), | |||||
| cell.getRow().getRowNum() + 1); | |||||
| } | |||||
| if (StringUtils.isNotEmpty(imagePath)) { | |||||
| data = ImageCache.getImage(imagePath); | |||||
| } | |||||
| if (data != null) { | |||||
| PoiExcelGraphDataUtil.getDrawingPatriarch(cell.getSheet()).createPicture(anchor, | |||||
| cell.getSheet().getWorkbook().addPicture(data, getImageType(data))); | |||||
| } | |||||
| } | |||||
| private int createIndexCell(Row row, int index, ExcelExportEntity excelExportEntity) { | |||||
| if (excelExportEntity.getName() != null && excelExportEntity.getName().equals("序号") && excelExportEntity.getFormat() != null | |||||
| && excelExportEntity.getFormat().equals(PoiBaseConstants.IS_ADD_INDEX)) { | |||||
| createStringCell(row, 0, currentIndex + "", | |||||
| index % 2 == 0 ? getStyles(false, null) : getStyles(true, null), null); | |||||
| currentIndex = currentIndex + 1; | |||||
| return 1; | |||||
| } | |||||
| return 0; | |||||
| } | |||||
| /** | |||||
| * 创建List之后的各个Cells | |||||
| */ | |||||
| public void createListCells(Drawing patriarch, int index, int cellNum, Object obj, | |||||
| List<ExcelExportEntity> excelParams, Sheet sheet, | |||||
| Workbook workbook) throws Exception { | |||||
| ExcelExportEntity entity; | |||||
| Row row; | |||||
| if (sheet.getRow(index) == null) { | |||||
| row = sheet.createRow(index); | |||||
| row.setHeight(getRowHeight(excelParams)); | |||||
| } else { | |||||
| row = sheet.getRow(index); | |||||
| } | |||||
| for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | |||||
| entity = excelParams.get(k); | |||||
| Object value = getCellValue(entity, obj); | |||||
| if (entity.getType() == BaseEntityTypeConstants.StringType) { | |||||
| createStringCell(row, cellNum++, value == null ? "" : value.toString(), | |||||
| row.getRowNum() % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), | |||||
| entity); | |||||
| if (entity.isHyperlink()) { | |||||
| row.getCell(cellNum - 1) | |||||
| .setHyperlink(dataHanlder.getHyperlink( | |||||
| row.getSheet().getWorkbook().getCreationHelper(), obj, entity.getName(), | |||||
| value)); | |||||
| } | |||||
| } else if (entity.getType() == BaseEntityTypeConstants.DoubleType) { | |||||
| createDoubleCell(row, cellNum++, value == null ? "" : value.toString(), | |||||
| index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); | |||||
| if (entity.isHyperlink()) { | |||||
| row.getCell(cellNum - 1) | |||||
| .setHyperlink(dataHanlder.getHyperlink( | |||||
| row.getSheet().getWorkbook().getCreationHelper(), obj, entity.getName(), | |||||
| value)); | |||||
| } | |||||
| } else { | |||||
| createImageCell(patriarch, entity, row, cellNum++, | |||||
| value == null ? "" : value.toString(), obj); | |||||
| } | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 创建文本类型的Cell | |||||
| */ | |||||
| public void createStringCell(Row row, int index, String text, CellStyle style, | |||||
| ExcelExportEntity entity) { | |||||
| Cell cell = row.createCell(index); | |||||
| if (style != null && style.getDataFormat() > 0 && style.getDataFormat() < 12) { | |||||
| cell.setCellValue(Double.parseDouble(text)); | |||||
| cell.setCellType(Cell.CELL_TYPE_NUMERIC); | |||||
| } else { | |||||
| RichTextString Rtext; | |||||
| if (type.equals(ExcelType.HSSF)) { | |||||
| Rtext = new HSSFRichTextString(text); | |||||
| } else { | |||||
| Rtext = new XSSFRichTextString(text); | |||||
| } | |||||
| cell.setCellValue(Rtext); | |||||
| } | |||||
| if (style != null) { | |||||
| cell.setCellStyle(style); | |||||
| } | |||||
| addStatisticsData(index, text, entity); | |||||
| } | |||||
| /** | |||||
| * 创建数字类型的Cell | |||||
| */ | |||||
| public void createDoubleCell(Row row, int index, String text, CellStyle style, | |||||
| ExcelExportEntity entity) { | |||||
| Cell cell = row.createCell(index); | |||||
| if (text != null && text.length() > 0) { | |||||
| cell.setCellValue(Double.parseDouble(text)); | |||||
| } | |||||
| cell.setCellType(Cell.CELL_TYPE_NUMERIC); | |||||
| if (style != null) { | |||||
| cell.setCellStyle(style); | |||||
| } | |||||
| addStatisticsData(index, text, entity); | |||||
| } | |||||
| /** | |||||
| * 创建统计行 | |||||
| */ | |||||
| public void addStatisticsRow(CellStyle styles, Sheet sheet) { | |||||
| if (statistics.size() > 0) { | |||||
| Row row = sheet.createRow(sheet.getLastRowNum() + 1); | |||||
| Set<Integer> keys = statistics.keySet(); | |||||
| createStringCell(row, 0, "合计", styles, null); | |||||
| for (Integer key : keys) { | |||||
| createStringCell(row, key, DOUBLE_FORMAT.format(statistics.get(key)), styles, null); | |||||
| } | |||||
| statistics.clear(); | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 合计统计信息 | |||||
| */ | |||||
| private void addStatisticsData(Integer index, String text, ExcelExportEntity entity) { | |||||
| if (entity != null && entity.isStatistics()) { | |||||
| Double temp = 0D; | |||||
| if (!statistics.containsKey(index)) { | |||||
| statistics.put(index, temp); | |||||
| } | |||||
| try { | |||||
| temp = Double.valueOf(text); | |||||
| } catch (NumberFormatException e) { | |||||
| } | |||||
| statistics.put(index, statistics.get(index) + temp); | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 获取图片类型,设置图片插入类型 | |||||
| * | |||||
| * @author JueYue 2013年11月25日 | |||||
| */ | |||||
| public int getImageType(byte[] value) { | |||||
| String type = PoiPublicUtil.getFileExtendName(value); | |||||
| if (type.equalsIgnoreCase("JPG")) { | |||||
| return Workbook.PICTURE_TYPE_JPEG; | |||||
| } else if (type.equalsIgnoreCase("PNG")) { | |||||
| return Workbook.PICTURE_TYPE_PNG; | |||||
| } | |||||
| return Workbook.PICTURE_TYPE_JPEG; | |||||
| } | |||||
| private Map<Integer, int[]> getMergeDataMap(List<ExcelExportEntity> excelParams) { | |||||
| Map<Integer, int[]> mergeMap = new HashMap<Integer, int[]>(); | |||||
| // 设置参数顺序,为之后合并单元格做准备 | |||||
| int i = 0; | |||||
| for (ExcelExportEntity entity : excelParams) { | |||||
| if (entity.isMergeVertical()) { | |||||
| mergeMap.put(i, entity.getMergeRely()); | |||||
| } | |||||
| if (entity.getList() != null) { | |||||
| for (ExcelExportEntity inner : entity.getList()) { | |||||
| if (inner.isMergeVertical()) { | |||||
| mergeMap.put(i, inner.getMergeRely()); | |||||
| } | |||||
| i++; | |||||
| } | |||||
| } else { | |||||
| i++; | |||||
| } | |||||
| } | |||||
| return mergeMap; | |||||
| } | |||||
| /** | |||||
| * 获取样式 | |||||
| */ | |||||
| public CellStyle getStyles(boolean needOne, ExcelExportEntity entity) { | |||||
| return excelExportStyler.getStyles(needOne, entity); | |||||
| } | |||||
| /** | |||||
| * 合并单元格 | |||||
| */ | |||||
| public void mergeCells(Sheet sheet, List<ExcelExportEntity> excelParams, int titleHeight) { | |||||
| Map<Integer, int[]> mergeMap = getMergeDataMap(excelParams); | |||||
| PoiMergeCellUtil.mergeCells(sheet, mergeMap, titleHeight); | |||||
| } | |||||
| public void setCellWith(List<ExcelExportEntity> excelParams, Sheet sheet) { | |||||
| int index = 0; | |||||
| for (int i = 0; i < excelParams.size(); i++) { | |||||
| if (excelParams.get(i).getList() != null) { | |||||
| List<ExcelExportEntity> list = excelParams.get(i).getList(); | |||||
| for (int j = 0; j < list.size(); j++) { | |||||
| sheet.setColumnWidth(index, (int) (256 * list.get(j).getWidth())); | |||||
| index++; | |||||
| } | |||||
| } else { | |||||
| sheet.setColumnWidth(index, (int) (256 * excelParams.get(i).getWidth())); | |||||
| index++; | |||||
| } | |||||
| } | |||||
| } | |||||
| public void setCurrentIndex(int currentIndex) { | |||||
| this.currentIndex = currentIndex; | |||||
| } | |||||
| public void setExcelExportStyler(IExcelExportStyler excelExportStyler) { | |||||
| this.excelExportStyler = excelExportStyler; | |||||
| } | |||||
| public IExcelExportStyler getExcelExportStyler() { | |||||
| return excelExportStyler; | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 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 cn.afterturn.easypoi.excel.export.base; | |||||
| import org.apache.commons.lang3.StringUtils; | |||||
| import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | |||||
| import org.apache.poi.hssf.usermodel.HSSFClientAnchor; | |||||
| import org.apache.poi.hssf.usermodel.HSSFRichTextString; | |||||
| import org.apache.poi.ss.usermodel.Cell; | |||||
| import org.apache.poi.ss.usermodel.CellStyle; | |||||
| import org.apache.poi.ss.usermodel.ClientAnchor; | |||||
| import org.apache.poi.ss.usermodel.Drawing; | |||||
| import org.apache.poi.ss.usermodel.RichTextString; | |||||
| import org.apache.poi.ss.usermodel.Row; | |||||
| import org.apache.poi.ss.usermodel.Sheet; | |||||
| import org.apache.poi.ss.usermodel.Workbook; | |||||
| import org.apache.poi.ss.util.CellRangeAddress; | |||||
| import org.apache.poi.xssf.usermodel.XSSFClientAnchor; | |||||
| import org.apache.poi.xssf.usermodel.XSSFRichTextString; | |||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| import java.text.DecimalFormat; | |||||
| import java.util.Collection; | |||||
| import java.util.HashMap; | |||||
| import java.util.List; | |||||
| import java.util.Map; | |||||
| import java.util.Set; | |||||
| import cn.afterturn.easypoi.cache.ImageCache; | |||||
| import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; | |||||
| import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | |||||
| import cn.afterturn.easypoi.excel.entity.vo.BaseEntityTypeConstants; | |||||
| import cn.afterturn.easypoi.excel.entity.vo.PoiBaseConstants; | |||||
| import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; | |||||
| import cn.afterturn.easypoi.exception.excel.ExcelExportException; | |||||
| import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum; | |||||
| import cn.afterturn.easypoi.util.PoiExcelGraphDataUtil; | |||||
| import cn.afterturn.easypoi.util.PoiMergeCellUtil; | |||||
| import cn.afterturn.easypoi.util.PoiPublicUtil; | |||||
| /** | |||||
| * 提供POI基础操作服务 | |||||
| * | |||||
| * @author JueYue 2014年6月17日 下午6:15:13 | |||||
| */ | |||||
| @SuppressWarnings("unchecked") | |||||
| public abstract class ExportBaseServer extends ExportCommonServer { | |||||
| private int currentIndex = 0; | |||||
| protected ExcelType type = ExcelType.HSSF; | |||||
| private Map<Integer, Double> statistics = new HashMap<Integer, Double>(); | |||||
| private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00"); | |||||
| protected IExcelExportStyler excelExportStyler; | |||||
| /** | |||||
| * 创建 最主要的 Cells | |||||
| */ | |||||
| public int createCells(Drawing patriarch, int index, Object t, | |||||
| List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook, | |||||
| short rowHeight) { | |||||
| try { | |||||
| ExcelExportEntity entity; | |||||
| Row row = sheet.createRow(index); | |||||
| row.setHeight(rowHeight); | |||||
| int maxHeight = 1, cellNum = 0; | |||||
| int indexKey = createIndexCell(row, index, excelParams.get(0)); | |||||
| cellNum += indexKey; | |||||
| for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { | |||||
| entity = excelParams.get(k); | |||||
| if (entity.getList() != null) { | |||||
| Collection<?> list = getListCellValue(entity, t); | |||||
| int listC = 0; | |||||
| if (list != null && list.size() > 0) { | |||||
| for (Object obj : list) { | |||||
| createListCells(patriarch, index + listC, cellNum, obj, entity.getList(), | |||||
| sheet, workbook, rowHeight); | |||||
| listC++; | |||||
| } | |||||
| } | |||||
| cellNum += entity.getList().size(); | |||||
| if (list != null && list.size() > maxHeight) { | |||||
| maxHeight = list.size(); | |||||
| } | |||||
| } else { | |||||
| Object value = getCellValue(entity, t); | |||||
| if (entity.getType() == BaseEntityTypeConstants.StringType) { | |||||
| createStringCell(row, cellNum++, value == null ? "" : value.toString(), | |||||
| index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), | |||||
| entity); | |||||
| if (entity.isHyperlink()) { | |||||
| row.getCell(cellNum - 1) | |||||
| .setHyperlink(dataHanlder.getHyperlink( | |||||
| row.getSheet().getWorkbook().getCreationHelper(), t, | |||||
| entity.getName(), value)); | |||||
| } | |||||
| } else if (entity.getType() == BaseEntityTypeConstants.DoubleType) { | |||||
| createDoubleCell(row, cellNum++, value == null ? "" : value.toString(), | |||||
| index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), | |||||
| entity); | |||||
| if (entity.isHyperlink()) { | |||||
| row.getCell(cellNum - 1).setHyperlink(dataHanlder.getHyperlink( | |||||
| row.getSheet().getWorkbook().getCreationHelper(), t, | |||||
| entity.getName(), value)); | |||||
| } | |||||
| } else { | |||||
| createImageCell(patriarch, entity, row, cellNum++, | |||||
| value == null ? "" : value.toString(), t); | |||||
| } | |||||
| } | |||||
| } | |||||
| // 合并需要合并的单元格 | |||||
| cellNum = 0; | |||||
| for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { | |||||
| entity = excelParams.get(k); | |||||
| if (entity.getList() != null) { | |||||
| cellNum += entity.getList().size(); | |||||
| } else if (entity.isNeedMerge() && maxHeight > 1) { | |||||
| for (int i = index + 1; i < index + maxHeight; i++) { | |||||
| sheet.getRow(i).createCell(cellNum); | |||||
| sheet.getRow(i).getCell(cellNum).setCellStyle(getStyles(false, entity)); | |||||
| } | |||||
| sheet.addMergedRegion( | |||||
| new CellRangeAddress(index, index + maxHeight - 1, cellNum, cellNum)); | |||||
| cellNum++; | |||||
| } | |||||
| } | |||||
| return maxHeight; | |||||
| } catch (Exception e) { | |||||
| LOGGER.error("excel cell export error ,data is :{}", ReflectionToStringBuilder.toString(t)); | |||||
| throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e); | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 图片类型的Cell | |||||
| */ | |||||
| public void createImageCell(Drawing patriarch, ExcelExportEntity entity, Row row, int i, | |||||
| String imagePath, Object obj) throws Exception { | |||||
| Cell cell = row.createCell(i); | |||||
| byte[] value = null; | |||||
| if (entity.getExportImageType() != 1) { | |||||
| value = (byte[]) (entity.getMethods() != null | |||||
| ? getFieldBySomeMethod(entity.getMethods(), obj) | |||||
| : entity.getMethod().invoke(obj, new Object[]{})); | |||||
| } | |||||
| createImageCell(cell, 50 * entity.getHeight(), entity.getExportImageType() == 1 ? imagePath : null, value); | |||||
| } | |||||
| /** | |||||
| * 图片类型的Cell | |||||
| */ | |||||
| public void createImageCell(Cell cell, double height, | |||||
| String imagePath, byte[] data) throws Exception { | |||||
| if (height > cell.getRow().getHeight()) { | |||||
| cell.getRow().setHeight((short) height); | |||||
| } | |||||
| ClientAnchor anchor; | |||||
| if (type.equals(ExcelType.HSSF)) { | |||||
| anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), | |||||
| cell.getRow().getRowNum() + 1); | |||||
| } else { | |||||
| anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), | |||||
| cell.getRow().getRowNum() + 1); | |||||
| } | |||||
| if (StringUtils.isNotEmpty(imagePath)) { | |||||
| data = ImageCache.getImage(imagePath); | |||||
| } | |||||
| if (data != null) { | |||||
| PoiExcelGraphDataUtil.getDrawingPatriarch(cell.getSheet()).createPicture(anchor, | |||||
| cell.getSheet().getWorkbook().addPicture(data, getImageType(data))); | |||||
| } | |||||
| } | |||||
| private int createIndexCell(Row row, int index, ExcelExportEntity excelExportEntity) { | |||||
| if (excelExportEntity.getName() != null && excelExportEntity.getName().equals("序号") && excelExportEntity.getFormat() != null | |||||
| && excelExportEntity.getFormat().equals(PoiBaseConstants.IS_ADD_INDEX)) { | |||||
| createStringCell(row, 0, currentIndex + "", | |||||
| index % 2 == 0 ? getStyles(false, null) : getStyles(true, null), null); | |||||
| currentIndex = currentIndex + 1; | |||||
| return 1; | |||||
| } | |||||
| return 0; | |||||
| } | |||||
| /** | |||||
| * 创建List之后的各个Cells | |||||
| */ | |||||
| public void createListCells(Drawing patriarch, int index, int cellNum, Object obj, | |||||
| List<ExcelExportEntity> excelParams, Sheet sheet, | |||||
| Workbook workbook, short rowHeight) throws Exception { | |||||
| ExcelExportEntity entity; | |||||
| Row row; | |||||
| if (sheet.getRow(index) == null) { | |||||
| row = sheet.createRow(index); | |||||
| row.setHeight(rowHeight); | |||||
| } else { | |||||
| row = sheet.getRow(index); | |||||
| row.setHeight(rowHeight); | |||||
| } | |||||
| for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | |||||
| entity = excelParams.get(k); | |||||
| Object value = getCellValue(entity, obj); | |||||
| if (entity.getType() == BaseEntityTypeConstants.StringType) { | |||||
| createStringCell(row, cellNum++, value == null ? "" : value.toString(), | |||||
| row.getRowNum() % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), | |||||
| entity); | |||||
| if (entity.isHyperlink()) { | |||||
| row.getCell(cellNum - 1) | |||||
| .setHyperlink(dataHanlder.getHyperlink( | |||||
| row.getSheet().getWorkbook().getCreationHelper(), obj, entity.getName(), | |||||
| value)); | |||||
| } | |||||
| } else if (entity.getType() == BaseEntityTypeConstants.DoubleType) { | |||||
| createDoubleCell(row, cellNum++, value == null ? "" : value.toString(), | |||||
| index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); | |||||
| if (entity.isHyperlink()) { | |||||
| row.getCell(cellNum - 1) | |||||
| .setHyperlink(dataHanlder.getHyperlink( | |||||
| row.getSheet().getWorkbook().getCreationHelper(), obj, entity.getName(), | |||||
| value)); | |||||
| } | |||||
| } else { | |||||
| createImageCell(patriarch, entity, row, cellNum++, | |||||
| value == null ? "" : value.toString(), obj); | |||||
| } | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 创建文本类型的Cell | |||||
| */ | |||||
| public void createStringCell(Row row, int index, String text, CellStyle style, | |||||
| ExcelExportEntity entity) { | |||||
| Cell cell = row.createCell(index); | |||||
| if (style != null && style.getDataFormat() > 0 && style.getDataFormat() < 12) { | |||||
| cell.setCellValue(Double.parseDouble(text)); | |||||
| cell.setCellType(Cell.CELL_TYPE_NUMERIC); | |||||
| } else { | |||||
| RichTextString Rtext; | |||||
| if (type.equals(ExcelType.HSSF)) { | |||||
| Rtext = new HSSFRichTextString(text); | |||||
| } else { | |||||
| Rtext = new XSSFRichTextString(text); | |||||
| } | |||||
| cell.setCellValue(Rtext); | |||||
| } | |||||
| if (style != null) { | |||||
| cell.setCellStyle(style); | |||||
| } | |||||
| addStatisticsData(index, text, entity); | |||||
| } | |||||
| /** | |||||
| * 创建数字类型的Cell | |||||
| */ | |||||
| public void createDoubleCell(Row row, int index, String text, CellStyle style, | |||||
| ExcelExportEntity entity) { | |||||
| Cell cell = row.createCell(index); | |||||
| if (text != null && text.length() > 0) { | |||||
| cell.setCellValue(Double.parseDouble(text)); | |||||
| } | |||||
| cell.setCellType(Cell.CELL_TYPE_NUMERIC); | |||||
| if (style != null) { | |||||
| cell.setCellStyle(style); | |||||
| } | |||||
| addStatisticsData(index, text, entity); | |||||
| } | |||||
| /** | |||||
| * 创建统计行 | |||||
| */ | |||||
| public void addStatisticsRow(CellStyle styles, Sheet sheet) { | |||||
| if (statistics.size() > 0) { | |||||
| if (LOGGER.isDebugEnabled()) { | |||||
| LOGGER.debug("add statistics data ,size is {}", statistics.size()); | |||||
| } | |||||
| Row row = sheet.createRow(sheet.getLastRowNum() + 1); | |||||
| Set<Integer> keys = statistics.keySet(); | |||||
| createStringCell(row, 0, "合计", styles, null); | |||||
| for (Integer key : keys) { | |||||
| createStringCell(row, key, DOUBLE_FORMAT.format(statistics.get(key)), styles, null); | |||||
| } | |||||
| statistics.clear(); | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 合计统计信息 | |||||
| */ | |||||
| private void addStatisticsData(Integer index, String text, ExcelExportEntity entity) { | |||||
| if (entity != null && entity.isStatistics()) { | |||||
| Double temp = 0D; | |||||
| if (!statistics.containsKey(index)) { | |||||
| statistics.put(index, temp); | |||||
| } | |||||
| try { | |||||
| temp = Double.valueOf(text); | |||||
| } catch (NumberFormatException e) { | |||||
| } | |||||
| statistics.put(index, statistics.get(index) + temp); | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 获取图片类型,设置图片插入类型 | |||||
| * | |||||
| * @author JueYue 2013年11月25日 | |||||
| */ | |||||
| public int getImageType(byte[] value) { | |||||
| String type = PoiPublicUtil.getFileExtendName(value); | |||||
| if (type.equalsIgnoreCase("JPG")) { | |||||
| return Workbook.PICTURE_TYPE_JPEG; | |||||
| } else if (type.equalsIgnoreCase("PNG")) { | |||||
| return Workbook.PICTURE_TYPE_PNG; | |||||
| } | |||||
| return Workbook.PICTURE_TYPE_JPEG; | |||||
| } | |||||
| private Map<Integer, int[]> getMergeDataMap(List<ExcelExportEntity> excelParams) { | |||||
| Map<Integer, int[]> mergeMap = new HashMap<Integer, int[]>(); | |||||
| // 设置参数顺序,为之后合并单元格做准备 | |||||
| int i = 0; | |||||
| for (ExcelExportEntity entity : excelParams) { | |||||
| if (entity.isMergeVertical()) { | |||||
| mergeMap.put(i, entity.getMergeRely()); | |||||
| } | |||||
| if (entity.getList() != null) { | |||||
| for (ExcelExportEntity inner : entity.getList()) { | |||||
| if (inner.isMergeVertical()) { | |||||
| mergeMap.put(i, inner.getMergeRely()); | |||||
| } | |||||
| i++; | |||||
| } | |||||
| } else { | |||||
| i++; | |||||
| } | |||||
| } | |||||
| return mergeMap; | |||||
| } | |||||
| /** | |||||
| * 获取样式 | |||||
| */ | |||||
| public CellStyle getStyles(boolean needOne, ExcelExportEntity entity) { | |||||
| return excelExportStyler.getStyles(needOne, entity); | |||||
| } | |||||
| /** | |||||
| * 合并单元格 | |||||
| */ | |||||
| public void mergeCells(Sheet sheet, List<ExcelExportEntity> excelParams, int titleHeight) { | |||||
| Map<Integer, int[]> mergeMap = getMergeDataMap(excelParams); | |||||
| PoiMergeCellUtil.mergeCells(sheet, mergeMap, titleHeight); | |||||
| } | |||||
| public void setCellWith(List<ExcelExportEntity> excelParams, Sheet sheet) { | |||||
| int index = 0; | |||||
| for (int i = 0; i < excelParams.size(); i++) { | |||||
| if (excelParams.get(i).getList() != null) { | |||||
| List<ExcelExportEntity> list = excelParams.get(i).getList(); | |||||
| for (int j = 0; j < list.size(); j++) { | |||||
| sheet.setColumnWidth(index, (int) (256 * list.get(j).getWidth())); | |||||
| index++; | |||||
| } | |||||
| } else { | |||||
| sheet.setColumnWidth(index, (int) (256 * excelParams.get(i).getWidth())); | |||||
| index++; | |||||
| } | |||||
| } | |||||
| } | |||||
| public void setCurrentIndex(int currentIndex) { | |||||
| this.currentIndex = currentIndex; | |||||
| } | |||||
| public void setExcelExportStyler(IExcelExportStyler excelExportStyler) { | |||||
| this.excelExportStyler = excelExportStyler; | |||||
| } | |||||
| public IExcelExportStyler getExcelExportStyler() { | |||||
| return excelExportStyler; | |||||
| } | |||||
| } | |||||
| @@ -1,377 +1,381 @@ | |||||
| /** | |||||
| * 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 cn.afterturn.easypoi.excel.export.base; | |||||
| import java.lang.reflect.Field; | |||||
| import java.lang.reflect.Method; | |||||
| import java.lang.reflect.ParameterizedType; | |||||
| import java.text.DecimalFormat; | |||||
| import java.text.SimpleDateFormat; | |||||
| import java.util.ArrayList; | |||||
| import java.util.Arrays; | |||||
| import java.util.Collection; | |||||
| import java.util.Collections; | |||||
| import java.util.Date; | |||||
| import java.util.List; | |||||
| import java.util.Map; | |||||
| import org.apache.commons.lang3.StringUtils; | |||||
| import org.apache.commons.lang3.math.NumberUtils; | |||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| import cn.afterturn.easypoi.excel.annotation.Excel; | |||||
| import cn.afterturn.easypoi.excel.annotation.ExcelCollection; | |||||
| import cn.afterturn.easypoi.excel.annotation.ExcelEntity; | |||||
| import cn.afterturn.easypoi.excel.entity.ExportParams; | |||||
| import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | |||||
| import cn.afterturn.easypoi.excel.entity.vo.PoiBaseConstants; | |||||
| import cn.afterturn.easypoi.handler.inter.IExcelDataHandler; | |||||
| import cn.afterturn.easypoi.util.PoiPublicUtil; | |||||
| import cn.afterturn.easypoi.util.PoiReflectorUtil; | |||||
| /** | |||||
| * 导出基础处理,不涉及POI,只涉及对象,保证复用性 | |||||
| * | |||||
| * @author JueYue | |||||
| * 2014年8月9日 下午11:01:32 | |||||
| */ | |||||
| @SuppressWarnings("rawtypes") | |||||
| public class ExportBase { | |||||
| protected static final Logger LOGGER = LoggerFactory | |||||
| .getLogger(ExportBase.class); | |||||
| protected IExcelDataHandler dataHanlder; | |||||
| protected List<String> needHanlderList; | |||||
| /** | |||||
| * 创建导出实体对象 | |||||
| * | |||||
| * @param field | |||||
| * @param targetId | |||||
| * @param pojoClass | |||||
| * @param getMethods | |||||
| * @return | |||||
| * @throws Exception | |||||
| */ | |||||
| private ExcelExportEntity createExcelExportEntity(Field field, String targetId, | |||||
| Class<?> pojoClass, | |||||
| List<Method> getMethods) throws Exception { | |||||
| Excel excel = field.getAnnotation(Excel.class); | |||||
| ExcelExportEntity excelEntity = new ExcelExportEntity(); | |||||
| excelEntity.setType(excel.type()); | |||||
| 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); | |||||
| } | |||||
| return excelEntity; | |||||
| } | |||||
| private Object dateFormatValue(Object value, ExcelExportEntity entity) throws Exception { | |||||
| Date temp = null; | |||||
| if (value instanceof String) { | |||||
| SimpleDateFormat format = new SimpleDateFormat(entity.getDatabaseFormat()); | |||||
| temp = format.parse(value.toString()); | |||||
| } else if (value instanceof Date) { | |||||
| temp = (Date) value; | |||||
| } else if (value instanceof java.sql.Date){ | |||||
| temp = new Date(((java.sql.Date)value).getTime()); | |||||
| } else if (value instanceof java.sql.Time){ | |||||
| temp = new Date(((java.sql.Time)value).getTime()); | |||||
| } else if (value instanceof java.sql.Timestamp){ | |||||
| temp = new Date(((java.sql.Timestamp)value).getTime()); | |||||
| } | |||||
| if (temp != null) { | |||||
| SimpleDateFormat format = new SimpleDateFormat(entity.getFormat()); | |||||
| value = format.format(temp); | |||||
| } | |||||
| return value; | |||||
| } | |||||
| private Object numFormatValue(Object value, ExcelExportEntity entity) { | |||||
| if(value == null){ | |||||
| return null; | |||||
| } | |||||
| if(!NumberUtils.isNumber(value.toString())){ | |||||
| LOGGER.error("data want num format ,but is not num, value is:"+value); | |||||
| return null; | |||||
| } | |||||
| Double d = Double.parseDouble(value.toString()); | |||||
| DecimalFormat df = new DecimalFormat(entity.getNumFormat()); | |||||
| return df.format(d); | |||||
| } | |||||
| /** | |||||
| * 获取需要导出的全部字段 | |||||
| * | |||||
| * @param exclusions | |||||
| * @param targetId | |||||
| * 目标ID | |||||
| * @param fields | |||||
| * @throws Exception | |||||
| */ | |||||
| public void getAllExcelField(String[] exclusions, String targetId, Field[] fields, | |||||
| List<ExcelExportEntity> excelParams, Class<?> pojoClass, | |||||
| List<Method> getMethods) throws Exception { | |||||
| List<String> exclusionsList = exclusions != null ? Arrays.asList(exclusions) : null; | |||||
| ExcelExportEntity excelEntity; | |||||
| // 遍历整个filed | |||||
| for (int i = 0; i < fields.length; i++) { | |||||
| Field field = fields[i]; | |||||
| // 先判断是不是collection,在判断是不是java自带对象,之后就是我们自己的对象了 | |||||
| if (PoiPublicUtil.isNotUserExcelUserThis(exclusionsList, field, targetId)) { | |||||
| continue; | |||||
| } | |||||
| // 首先判断Excel 可能一下特殊数据用户回自定义处理 | |||||
| if (field.getAnnotation(Excel.class) != null) { | |||||
| Excel excel = field.getAnnotation(Excel.class); | |||||
| String name=PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null); | |||||
| if(StringUtils.isNotBlank(name)){ | |||||
| excelParams.add(createExcelExportEntity(field, targetId, pojoClass, getMethods)); | |||||
| } | |||||
| } else if (PoiPublicUtil.isCollection(field.getType())) { | |||||
| ExcelCollection excel = field.getAnnotation(ExcelCollection.class); | |||||
| ParameterizedType pt = (ParameterizedType) field.getGenericType(); | |||||
| Class<?> clz = (Class<?>) pt.getActualTypeArguments()[0]; | |||||
| List<ExcelExportEntity> list = new ArrayList<ExcelExportEntity>(); | |||||
| getAllExcelField(exclusions, | |||||
| StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId, | |||||
| PoiPublicUtil.getClassFields(clz), list, clz, null); | |||||
| excelEntity = new ExcelExportEntity(); | |||||
| excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null)); | |||||
| excelEntity.setOrderNum(Integer | |||||
| .valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId, "0"))); | |||||
| excelEntity | |||||
| .setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); | |||||
| excelEntity.setList(list); | |||||
| excelParams.add(excelEntity); | |||||
| } else { | |||||
| List<Method> newMethods = new ArrayList<Method>(); | |||||
| if (getMethods != null) { | |||||
| newMethods.addAll(getMethods); | |||||
| } | |||||
| newMethods.add(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); | |||||
| ExcelEntity excel = field.getAnnotation(ExcelEntity.class); | |||||
| getAllExcelField(exclusions, | |||||
| StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId, | |||||
| PoiPublicUtil.getClassFields(field.getType()), excelParams, field.getType(), | |||||
| newMethods); | |||||
| } | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 获取填如这个cell的值,提供一些附加功能 | |||||
| * | |||||
| * @param entity | |||||
| * @param obj | |||||
| * @return | |||||
| * @throws Exception | |||||
| */ | |||||
| @SuppressWarnings("unchecked") | |||||
| public Object getCellValue(ExcelExportEntity entity, Object obj) throws Exception { | |||||
| Object value; | |||||
| if (obj instanceof Map) { | |||||
| value = ((Map<?, ?>) obj).get(entity.getKey()); | |||||
| } else { | |||||
| value = entity.getMethods() != null ? getFieldBySomeMethod(entity.getMethods(), obj) | |||||
| : entity.getMethod().invoke(obj, new Object[] {}); | |||||
| } | |||||
| if (StringUtils.isNotEmpty(entity.getFormat())) { | |||||
| value = dateFormatValue(value, entity); | |||||
| } | |||||
| if (entity.getReplace() != null && entity.getReplace().length > 0) { | |||||
| value = replaceValue(entity.getReplace(), String.valueOf(value)); | |||||
| } | |||||
| if (StringUtils.isNotEmpty(entity.getNumFormat())) { | |||||
| value = numFormatValue(value, entity); | |||||
| } | |||||
| if (needHanlderList != null && needHanlderList.contains(entity.getName())) { | |||||
| value = dataHanlder.exportHandler(obj, entity.getName(), value); | |||||
| } | |||||
| if (StringUtils.isNotEmpty(entity.getSuffix()) && value != null) { | |||||
| value = value + entity.getSuffix(); | |||||
| } | |||||
| return value == null ? "" : value.toString(); | |||||
| } | |||||
| /** | |||||
| * 获取集合的值 | |||||
| * @param entity | |||||
| * @param obj | |||||
| * @return | |||||
| * @throws Exception | |||||
| */ | |||||
| public Collection<?> getListCellValue(ExcelExportEntity entity, Object obj) throws Exception { | |||||
| Object value; | |||||
| if (obj instanceof Map) { | |||||
| value = ((Map<?, ?>) obj).get(entity.getKey()); | |||||
| } else { | |||||
| value = (Collection<?>) entity.getMethod().invoke(obj, new Object[] {}); | |||||
| } | |||||
| return (Collection<?>) value; | |||||
| } | |||||
| /** | |||||
| * 注解到导出对象的转换 | |||||
| * | |||||
| * @param targetId | |||||
| * @param field | |||||
| * @param excelEntity | |||||
| * @param excel | |||||
| * @param pojoClass | |||||
| * @throws Exception | |||||
| */ | |||||
| private void getExcelField(String targetId, Field field, ExcelExportEntity excelEntity, | |||||
| Excel excel, Class<?> pojoClass) throws Exception { | |||||
| excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null)); | |||||
| excelEntity.setWidth(excel.width()); | |||||
| excelEntity.setHeight(excel.height()); | |||||
| excelEntity.setNeedMerge(excel.needMerge()); | |||||
| excelEntity.setMergeVertical(excel.mergeVertical()); | |||||
| excelEntity.setMergeRely(excel.mergeRely()); | |||||
| excelEntity.setReplace(excel.replace()); | |||||
| excelEntity.setOrderNum( | |||||
| Integer.valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId, "0"))); | |||||
| excelEntity.setWrap(excel.isWrap()); | |||||
| excelEntity.setExportImageType(excel.imageType()); | |||||
| excelEntity.setSuffix(excel.suffix()); | |||||
| excelEntity.setDatabaseFormat(excel.databaseFormat()); | |||||
| excelEntity.setFormat( | |||||
| StringUtils.isNotEmpty(excel.exportFormat()) ? excel.exportFormat() : excel.format()); | |||||
| excelEntity.setStatistics(excel.isStatistics()); | |||||
| excelEntity.setHyperlink(excel.isHyperlink()); | |||||
| excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); | |||||
| excelEntity.setNumFormat(excel.numFormat()); | |||||
| } | |||||
| /** | |||||
| * 多个反射获取值 | |||||
| * | |||||
| * @param list | |||||
| * @param t | |||||
| * @return | |||||
| * @throws Exception | |||||
| */ | |||||
| public Object getFieldBySomeMethod(List<Method> list, Object t) throws Exception { | |||||
| for (Method m : list) { | |||||
| if (t == null) { | |||||
| t = ""; | |||||
| break; | |||||
| } | |||||
| t = m.invoke(t, new Object[] {}); | |||||
| } | |||||
| return t; | |||||
| } | |||||
| /** | |||||
| * 根据注解获取行高 | |||||
| * | |||||
| * @param excelParams | |||||
| * @return | |||||
| */ | |||||
| public short getRowHeight(List<ExcelExportEntity> excelParams) { | |||||
| double maxHeight = 0; | |||||
| for (int i = 0; i < excelParams.size(); i++) { | |||||
| maxHeight = maxHeight > excelParams.get(i).getHeight() ? maxHeight | |||||
| : excelParams.get(i).getHeight(); | |||||
| if (excelParams.get(i).getList() != null) { | |||||
| for (int j = 0; j < excelParams.get(i).getList().size(); j++) { | |||||
| maxHeight = maxHeight > excelParams.get(i).getList().get(j).getHeight() | |||||
| ? maxHeight : excelParams.get(i).getList().get(j).getHeight(); | |||||
| } | |||||
| } | |||||
| } | |||||
| return (short) (maxHeight * 50); | |||||
| } | |||||
| private Object replaceValue(String[] replace, String value) { | |||||
| String[] temp; | |||||
| for (String str : replace) { | |||||
| temp = str.split("_"); | |||||
| if (value.equals(temp[1])) { | |||||
| value = temp[0]; | |||||
| break; | |||||
| } | |||||
| } | |||||
| return value; | |||||
| } | |||||
| /** | |||||
| * 对字段根据用户设置排序 | |||||
| */ | |||||
| public void sortAllParams(List<ExcelExportEntity> excelParams) { | |||||
| Collections.sort(excelParams); | |||||
| for (ExcelExportEntity entity : excelParams) { | |||||
| if (entity.getList() != null) { | |||||
| Collections.sort(entity.getList()); | |||||
| } | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 添加Index列 | |||||
| * @param entity | |||||
| * @return | |||||
| */ | |||||
| public ExcelExportEntity indexExcelEntity(ExportParams entity) { | |||||
| ExcelExportEntity exportEntity = new ExcelExportEntity(); | |||||
| exportEntity.setOrderNum(0); | |||||
| exportEntity.setName(entity.getIndexName()); | |||||
| exportEntity.setWidth(10); | |||||
| exportEntity.setFormat(PoiBaseConstants.IS_ADD_INDEX); | |||||
| return exportEntity; | |||||
| } | |||||
| /** | |||||
| * 获取导出报表的字段总长度 | |||||
| * | |||||
| * @param excelParams | |||||
| * @return | |||||
| */ | |||||
| public int getFieldLength(List<ExcelExportEntity> excelParams) { | |||||
| int length = -1;// 从0开始计算单元格的 | |||||
| for (ExcelExportEntity entity : excelParams) { | |||||
| length += entity.getList() != null ? entity.getList().size() : 1; | |||||
| } | |||||
| return length; | |||||
| } | |||||
| /** | |||||
| * 判断表头是只有一行还是两行 | |||||
| * | |||||
| * @param excelParams | |||||
| * @return | |||||
| */ | |||||
| public int getRowNums(List<ExcelExportEntity> excelParams) { | |||||
| for (int i = 0; i < excelParams.size(); i++) { | |||||
| if (excelParams.get(i).getList() != null | |||||
| && StringUtils.isNotBlank(excelParams.get(i).getName())) { | |||||
| return 2; | |||||
| } | |||||
| } | |||||
| return 1; | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 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 cn.afterturn.easypoi.excel.export.base; | |||||
| import java.lang.reflect.Field; | |||||
| import java.lang.reflect.Method; | |||||
| import java.lang.reflect.ParameterizedType; | |||||
| import java.text.DecimalFormat; | |||||
| import java.text.SimpleDateFormat; | |||||
| import java.util.ArrayList; | |||||
| import java.util.Arrays; | |||||
| import java.util.Collection; | |||||
| import java.util.Collections; | |||||
| import java.util.Date; | |||||
| import java.util.HashMap; | |||||
| import java.util.Iterator; | |||||
| import java.util.List; | |||||
| import java.util.Map; | |||||
| import java.util.Set; | |||||
| import org.apache.commons.lang3.StringUtils; | |||||
| import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | |||||
| import org.apache.commons.lang3.math.NumberUtils; | |||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| import cn.afterturn.easypoi.excel.annotation.Excel; | |||||
| import cn.afterturn.easypoi.excel.annotation.ExcelCollection; | |||||
| import cn.afterturn.easypoi.excel.annotation.ExcelEntity; | |||||
| import cn.afterturn.easypoi.excel.entity.ExportParams; | |||||
| import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | |||||
| import cn.afterturn.easypoi.excel.entity.vo.PoiBaseConstants; | |||||
| import cn.afterturn.easypoi.exception.excel.ExcelExportException; | |||||
| import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum; | |||||
| import cn.afterturn.easypoi.handler.inter.IExcelDataHandler; | |||||
| import cn.afterturn.easypoi.util.PoiPublicUtil; | |||||
| import cn.afterturn.easypoi.util.PoiReflectorUtil; | |||||
| /** | |||||
| * 导出基础处理,不涉及POI,只涉及对象,保证复用性 | |||||
| * | |||||
| * @author JueYue 2014年8月9日 下午11:01:32 | |||||
| */ | |||||
| @SuppressWarnings("rawtypes") | |||||
| public class ExportCommonServer { | |||||
| protected static final Logger LOGGER = LoggerFactory.getLogger(ExportCommonServer.class); | |||||
| protected IExcelDataHandler dataHanlder; | |||||
| protected List<String> needHanlderList; | |||||
| /** | |||||
| * 创建导出实体对象 | |||||
| */ | |||||
| private ExcelExportEntity createExcelExportEntity(Field field, String targetId, | |||||
| Class<?> pojoClass, | |||||
| List<Method> getMethods, ExcelEntity excelGroup) throws Exception { | |||||
| Excel excel = field.getAnnotation(Excel.class); | |||||
| ExcelExportEntity excelEntity = new ExcelExportEntity(); | |||||
| excelEntity.setType(excel.type()); | |||||
| getExcelField(targetId, field, excelEntity, excel, pojoClass, excelGroup); | |||||
| if (getMethods != null) { | |||||
| List<Method> newMethods = new ArrayList<Method>(); | |||||
| newMethods.addAll(getMethods); | |||||
| newMethods.add(excelEntity.getMethod()); | |||||
| excelEntity.setMethods(newMethods); | |||||
| } | |||||
| return excelEntity; | |||||
| } | |||||
| private Object dateFormatValue(Object value, ExcelExportEntity entity) throws Exception { | |||||
| Date temp = null; | |||||
| if (value instanceof String) { | |||||
| SimpleDateFormat format = new SimpleDateFormat(entity.getDatabaseFormat()); | |||||
| temp = format.parse(value.toString()); | |||||
| } else if (value instanceof Date) { | |||||
| temp = (Date) value; | |||||
| } else if (value instanceof java.sql.Date) { | |||||
| temp = new Date(((java.sql.Date) value).getTime()); | |||||
| } else if (value instanceof java.sql.Time) { | |||||
| temp = new Date(((java.sql.Time) value).getTime()); | |||||
| } else if (value instanceof java.sql.Timestamp) { | |||||
| temp = new Date(((java.sql.Timestamp) value).getTime()); | |||||
| } | |||||
| if (temp != null) { | |||||
| SimpleDateFormat format = new SimpleDateFormat(entity.getFormat()); | |||||
| value = format.format(temp); | |||||
| } | |||||
| return value; | |||||
| } | |||||
| private Object numFormatValue(Object value, ExcelExportEntity entity) { | |||||
| if (value == null) { | |||||
| return null; | |||||
| } | |||||
| if (!NumberUtils.isNumber(value.toString())) { | |||||
| LOGGER.error("data want num format ,but is not num, value is:" + value); | |||||
| return null; | |||||
| } | |||||
| Double d = Double.parseDouble(value.toString()); | |||||
| DecimalFormat df = new DecimalFormat(entity.getNumFormat()); | |||||
| return df.format(d); | |||||
| } | |||||
| /** | |||||
| * 获取需要导出的全部字段 | |||||
| * | |||||
| * @param targetId 目标ID | |||||
| */ | |||||
| public void getAllExcelField(String[] exclusions, String targetId, Field[] fields, | |||||
| List<ExcelExportEntity> excelParams, Class<?> pojoClass, | |||||
| List<Method> getMethods, ExcelEntity excelGroup) throws Exception { | |||||
| List<String> exclusionsList = exclusions != null ? Arrays.asList(exclusions) : null; | |||||
| ExcelExportEntity excelEntity; | |||||
| // 遍历整个filed | |||||
| for (int i = 0; i < fields.length; i++) { | |||||
| Field field = fields[i]; | |||||
| // 先判断是不是collection,在判断是不是java自带对象,之后就是我们自己的对象了 | |||||
| if (PoiPublicUtil.isNotUserExcelUserThis(exclusionsList, field, targetId)) { | |||||
| continue; | |||||
| } | |||||
| // 首先判断Excel 可能一下特殊数据用户回自定义处理 | |||||
| if (field.getAnnotation(Excel.class) != null) { | |||||
| Excel excel = field.getAnnotation(Excel.class); | |||||
| String name = PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null); | |||||
| if (StringUtils.isNotBlank(name)) { | |||||
| excelParams.add(createExcelExportEntity(field, targetId, pojoClass, getMethods, excelGroup)); | |||||
| } | |||||
| } else if (PoiPublicUtil.isCollection(field.getType())) { | |||||
| ExcelCollection excel = field.getAnnotation(ExcelCollection.class); | |||||
| ParameterizedType pt = (ParameterizedType) field.getGenericType(); | |||||
| Class<?> clz = (Class<?>) pt.getActualTypeArguments()[0]; | |||||
| List<ExcelExportEntity> list = new ArrayList<ExcelExportEntity>(); | |||||
| getAllExcelField(exclusions, | |||||
| StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId, | |||||
| PoiPublicUtil.getClassFields(clz), list, clz, null, null); | |||||
| excelEntity = new ExcelExportEntity(); | |||||
| excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null)); | |||||
| excelEntity.setOrderNum(Integer | |||||
| .valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId, "0"))); | |||||
| excelEntity | |||||
| .setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); | |||||
| excelEntity.setList(list); | |||||
| excelParams.add(excelEntity); | |||||
| } else { | |||||
| List<Method> newMethods = new ArrayList<Method>(); | |||||
| if (getMethods != null) { | |||||
| newMethods.addAll(getMethods); | |||||
| } | |||||
| newMethods.add(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); | |||||
| ExcelEntity excel = field.getAnnotation(ExcelEntity.class); | |||||
| if (excel.show() && StringUtils.isEmpty(excel.name())) { | |||||
| throw new ExcelExportException("if use ExcelEntity ,name mus has value ,data: " + ReflectionToStringBuilder.toString(excel), ExcelExportEnum.PARAMETER_ERROR); | |||||
| } | |||||
| getAllExcelField(exclusions, | |||||
| StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId, | |||||
| PoiPublicUtil.getClassFields(field.getType()), excelParams, field.getType(), | |||||
| newMethods, excel.show() ? excel : null); | |||||
| } | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 获取填如这个cell的值,提供一些附加功能 | |||||
| */ | |||||
| @SuppressWarnings("unchecked") | |||||
| public Object getCellValue(ExcelExportEntity entity, Object obj) throws Exception { | |||||
| Object value; | |||||
| if (obj instanceof Map) { | |||||
| value = ((Map<?, ?>) obj).get(entity.getKey()); | |||||
| } else { | |||||
| value = entity.getMethods() != null ? getFieldBySomeMethod(entity.getMethods(), obj) | |||||
| : entity.getMethod().invoke(obj, new Object[]{}); | |||||
| } | |||||
| if (StringUtils.isNotEmpty(entity.getFormat())) { | |||||
| value = dateFormatValue(value, entity); | |||||
| } | |||||
| if (entity.getReplace() != null && entity.getReplace().length > 0) { | |||||
| value = replaceValue(entity.getReplace(), String.valueOf(value)); | |||||
| } | |||||
| if (StringUtils.isNotEmpty(entity.getNumFormat())) { | |||||
| value = numFormatValue(value, entity); | |||||
| } | |||||
| if (needHanlderList != null && needHanlderList.contains(entity.getName())) { | |||||
| value = dataHanlder.exportHandler(obj, entity.getName(), value); | |||||
| } | |||||
| if (StringUtils.isNotEmpty(entity.getSuffix()) && value != null) { | |||||
| value = value + entity.getSuffix(); | |||||
| } | |||||
| return value == null ? "" : value.toString(); | |||||
| } | |||||
| /** | |||||
| * 获取集合的值 | |||||
| */ | |||||
| public Collection<?> getListCellValue(ExcelExportEntity entity, Object obj) throws Exception { | |||||
| Object value; | |||||
| if (obj instanceof Map) { | |||||
| value = ((Map<?, ?>) obj).get(entity.getKey()); | |||||
| } else { | |||||
| value = (Collection<?>) entity.getMethod().invoke(obj, new Object[]{}); | |||||
| } | |||||
| return (Collection<?>) value; | |||||
| } | |||||
| /** | |||||
| * 注解到导出对象的转换 | |||||
| */ | |||||
| private void getExcelField(String targetId, Field field, ExcelExportEntity excelEntity, | |||||
| Excel excel, Class<?> pojoClass, ExcelEntity excelGroup) throws Exception { | |||||
| excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null)); | |||||
| excelEntity.setWidth(excel.width()); | |||||
| excelEntity.setHeight(excel.height()); | |||||
| excelEntity.setNeedMerge(excel.needMerge()); | |||||
| excelEntity.setMergeVertical(excel.mergeVertical()); | |||||
| excelEntity.setMergeRely(excel.mergeRely()); | |||||
| excelEntity.setReplace(excel.replace()); | |||||
| excelEntity.setOrderNum( | |||||
| Integer.valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId, "0"))); | |||||
| excelEntity.setWrap(excel.isWrap()); | |||||
| excelEntity.setExportImageType(excel.imageType()); | |||||
| excelEntity.setSuffix(excel.suffix()); | |||||
| excelEntity.setDatabaseFormat(excel.databaseFormat()); | |||||
| excelEntity.setFormat( | |||||
| StringUtils.isNotEmpty(excel.exportFormat()) ? excel.exportFormat() : excel.format()); | |||||
| excelEntity.setStatistics(excel.isStatistics()); | |||||
| excelEntity.setHyperlink(excel.isHyperlink()); | |||||
| excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); | |||||
| excelEntity.setNumFormat(excel.numFormat()); | |||||
| if (excelGroup != null) { | |||||
| excelEntity.setGroupName(PoiPublicUtil.getValueByTargetId(excelGroup.name(), targetId, null)); | |||||
| } else { | |||||
| excelEntity.setGroupName(excel.groupName()); | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 多个反射获取值 | |||||
| */ | |||||
| public Object getFieldBySomeMethod(List<Method> list, Object t) throws Exception { | |||||
| for (Method m : list) { | |||||
| if (t == null) { | |||||
| t = ""; | |||||
| break; | |||||
| } | |||||
| t = m.invoke(t, new Object[]{}); | |||||
| } | |||||
| return t; | |||||
| } | |||||
| /** | |||||
| * 根据注解获取行高 | |||||
| */ | |||||
| public short getRowHeight(List<ExcelExportEntity> excelParams) { | |||||
| double maxHeight = 0; | |||||
| for (int i = 0; i < excelParams.size(); i++) { | |||||
| maxHeight = maxHeight > excelParams.get(i).getHeight() ? maxHeight | |||||
| : excelParams.get(i).getHeight(); | |||||
| if (excelParams.get(i).getList() != null) { | |||||
| for (int j = 0; j < excelParams.get(i).getList().size(); j++) { | |||||
| maxHeight = maxHeight > excelParams.get(i).getList().get(j).getHeight() | |||||
| ? maxHeight : excelParams.get(i).getList().get(j).getHeight(); | |||||
| } | |||||
| } | |||||
| } | |||||
| return (short) (maxHeight * 50); | |||||
| } | |||||
| private Object replaceValue(String[] replace, String value) { | |||||
| String[] temp; | |||||
| for (String str : replace) { | |||||
| temp = str.split("_"); | |||||
| if (value.equals(temp[1])) { | |||||
| value = temp[0]; | |||||
| break; | |||||
| } | |||||
| } | |||||
| return value; | |||||
| } | |||||
| /** | |||||
| * 对字段根据用户设置排序 | |||||
| */ | |||||
| public void sortAllParams(List<ExcelExportEntity> excelParams) { | |||||
| // 自然排序,group 内部排序,集合内部排序 | |||||
| // 把有groupName的统一收集起来,内部先排序 | |||||
| Map<String, List<ExcelExportEntity>> groupMap = new HashMap<String, List<ExcelExportEntity>>(); | |||||
| for (int i = excelParams.size() - 1; i > -1; i--) { | |||||
| // 集合内部排序 | |||||
| if (excelParams.get(i).getList() != null) { | |||||
| Collections.sort(excelParams.get(i).getList()); | |||||
| } else if (StringUtils.isNoneEmpty(excelParams.get(i).getGroupName())) { | |||||
| if (!groupMap.containsKey(excelParams.get(i).getGroupName())) { | |||||
| groupMap.put(excelParams.get(i).getGroupName(), new ArrayList<ExcelExportEntity>()); | |||||
| } | |||||
| groupMap.get(excelParams.get(i).getGroupName()).add(excelParams.get(i)); | |||||
| excelParams.remove(i); | |||||
| } | |||||
| } | |||||
| Collections.sort(excelParams); | |||||
| if (groupMap.size() > 0) { | |||||
| // group 内部排序 | |||||
| for (Iterator it = groupMap.entrySet().iterator(); it.hasNext(); ) { | |||||
| Map.Entry<String, List<ExcelExportEntity>> entry = (Map.Entry) it.next(); | |||||
| Collections.sort(entry.getValue()); | |||||
| // 插入到excelParams当中 | |||||
| boolean isInsert = false; | |||||
| for (int i = 0; i < excelParams.size() - 1; i++) { | |||||
| // 跳过groupName 的元素,防止破会内部结构 | |||||
| if (StringUtils.isEmpty(excelParams.get(i).getGroupName()) && excelParams.get(i).getOrderNum() <= entry.getValue().get(0).getOrderNum() | |||||
| && excelParams.get(i + 1).getOrderNum() > entry.getValue().get(0).getOrderNum()) { | |||||
| excelParams.addAll(i + 1, entry.getValue()); | |||||
| isInsert = true; | |||||
| break; | |||||
| } | |||||
| } | |||||
| //如果都比他小就插入到最后 | |||||
| if (!isInsert) { | |||||
| excelParams.addAll(entry.getValue()); | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 添加Index列 | |||||
| */ | |||||
| public ExcelExportEntity indexExcelEntity(ExportParams entity) { | |||||
| ExcelExportEntity exportEntity = new ExcelExportEntity(); | |||||
| //保证是第一排 | |||||
| exportEntity.setOrderNum(Integer.MIN_VALUE); | |||||
| exportEntity.setName(entity.getIndexName()); | |||||
| exportEntity.setWidth(10); | |||||
| exportEntity.setFormat(PoiBaseConstants.IS_ADD_INDEX); | |||||
| return exportEntity; | |||||
| } | |||||
| /** | |||||
| * 获取导出报表的字段总长度 | |||||
| */ | |||||
| public int getFieldLength(List<ExcelExportEntity> excelParams) { | |||||
| int length = -1;// 从0开始计算单元格的 | |||||
| for (ExcelExportEntity entity : excelParams) { | |||||
| length += entity.getList() != null ? entity.getList().size() : 1; | |||||
| } | |||||
| return length; | |||||
| } | |||||
| /** | |||||
| * 判断表头是只有一行还是两行 | |||||
| */ | |||||
| public int getRowNums(List<ExcelExportEntity> excelParams) { | |||||
| for (int i = 0; i < excelParams.size(); i++) { | |||||
| if (excelParams.get(i).getList() != null | |||||
| && StringUtils.isNotBlank(excelParams.get(i).getName())) { | |||||
| return 2; | |||||
| } | |||||
| if (StringUtils.isNoneEmpty(excelParams.get(i).getGroupName())) { | |||||
| return 2; | |||||
| } | |||||
| } | |||||
| return 1; | |||||
| } | |||||
| } | |||||
| @@ -47,7 +47,7 @@ import cn.afterturn.easypoi.excel.entity.TemplateExportParams; | |||||
| import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; | import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; | ||||
| import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | ||||
| import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams; | import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams; | ||||
| import cn.afterturn.easypoi.excel.export.base.ExcelExportBase; | |||||
| import cn.afterturn.easypoi.excel.export.base.ExportBaseServer; | |||||
| import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; | import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; | ||||
| import cn.afterturn.easypoi.excel.export.template.TemplateSumHanlder.TemplateSumEntity; | import cn.afterturn.easypoi.excel.export.template.TemplateSumHanlder.TemplateSumEntity; | ||||
| import cn.afterturn.easypoi.excel.html.helper.MergedRegionHelper; | import cn.afterturn.easypoi.excel.html.helper.MergedRegionHelper; | ||||
| @@ -64,7 +64,7 @@ import cn.afterturn.easypoi.util.PoiSheetUtility; | |||||
| * 2013-10-17 | * 2013-10-17 | ||||
| * @version 1.0 | * @version 1.0 | ||||
| */ | */ | ||||
| public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| public final class ExcelExportOfTemplateUtil extends ExportBaseServer { | |||||
| private static final Logger LOGGER = LoggerFactory | private static final Logger LOGGER = LoggerFactory | ||||
| .getLogger(ExcelExportOfTemplateUtil.class); | .getLogger(ExcelExportOfTemplateUtil.class); | ||||
| @@ -110,7 +110,7 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| } | } | ||||
| // 获取实体对象的导出数据 | // 获取实体对象的导出数据 | ||||
| List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | ||||
| getAllExcelField(null, targetId, fileds, excelParams, pojoClass, null); | |||||
| getAllExcelField(null, targetId, fileds, excelParams, pojoClass, null, null); | |||||
| // 根据表头进行筛选排序 | // 根据表头进行筛选排序 | ||||
| sortAndFilterExportField(excelParams, titlemap); | sortAndFilterExportField(excelParams, titlemap); | ||||
| short rowHeight = getRowHeight(excelParams); | short rowHeight = getRowHeight(excelParams); | ||||
| @@ -41,7 +41,7 @@ import com.itextpdf.text.pdf.PdfWriter; | |||||
| import cn.afterturn.easypoi.cache.ImageCache; | import cn.afterturn.easypoi.cache.ImageCache; | ||||
| import cn.afterturn.easypoi.excel.annotation.ExcelTarget; | import cn.afterturn.easypoi.excel.annotation.ExcelTarget; | ||||
| import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | ||||
| import cn.afterturn.easypoi.excel.export.base.ExportBase; | |||||
| import cn.afterturn.easypoi.excel.export.base.ExportCommonServer; | |||||
| import cn.afterturn.easypoi.pdf.entity.PdfExportParams; | import cn.afterturn.easypoi.pdf.entity.PdfExportParams; | ||||
| import cn.afterturn.easypoi.pdf.styler.IPdfExportStyler; | import cn.afterturn.easypoi.pdf.styler.IPdfExportStyler; | ||||
| import cn.afterturn.easypoi.pdf.styler.PdfExportStylerDefaultImpl; | import cn.afterturn.easypoi.pdf.styler.PdfExportStylerDefaultImpl; | ||||
| @@ -52,7 +52,7 @@ import cn.afterturn.easypoi.util.PoiPublicUtil; | |||||
| * @author JueYue | * @author JueYue | ||||
| * 2015年10月6日 下午8:21:08 | * 2015年10月6日 下午8:21:08 | ||||
| */ | */ | ||||
| public class PdfExportServer extends ExportBase { | |||||
| public class PdfExportServer extends ExportCommonServer { | |||||
| private static final Logger LOGGER = LoggerFactory.getLogger(PdfExportServer.class); | private static final Logger LOGGER = LoggerFactory.getLogger(PdfExportServer.class); | ||||
| @@ -91,7 +91,7 @@ public class PdfExportServer extends ExportBase { | |||||
| ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); | ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); | ||||
| String targetId = etarget == null ? null : etarget.value(); | String targetId = etarget == null ? null : etarget.value(); | ||||
| getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, | getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, | ||||
| null); | |||||
| null, null); | |||||
| createPdfByExportEntity(entity, excelParams, dataSet); | createPdfByExportEntity(entity, excelParams, dataSet); | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| LOGGER.error(e.getMessage(), e); | LOGGER.error(e.getMessage(), e); | ||||
| @@ -1,13 +1,13 @@ | |||||
| /** | /** | ||||
| * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) | * 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 | |||||
| * 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 | |||||
| * | * | ||||
| * Unless required by applicable law or agreed to in writing, software | |||||
| * 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, | * distributed under the License is distributed on an "AS IS" BASIS, | ||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| * See the License for the specific language governing permissions and | * See the License for the specific language governing permissions and | ||||
| @@ -24,6 +24,7 @@ import java.util.List; | |||||
| import java.util.Map; | import java.util.Map; | ||||
| import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
| import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFTable; | import org.apache.poi.xwpf.usermodel.XWPFTable; | ||||
| import org.apache.poi.xwpf.usermodel.XWPFTableCell; | import org.apache.poi.xwpf.usermodel.XWPFTableCell; | ||||
| import org.apache.poi.xwpf.usermodel.XWPFTableRow; | import org.apache.poi.xwpf.usermodel.XWPFTableRow; | ||||
| @@ -32,7 +33,9 @@ import org.slf4j.LoggerFactory; | |||||
| import cn.afterturn.easypoi.excel.annotation.ExcelTarget; | import cn.afterturn.easypoi.excel.annotation.ExcelTarget; | ||||
| import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | ||||
| import cn.afterturn.easypoi.excel.export.base.ExportBase; | |||||
| import cn.afterturn.easypoi.excel.export.base.ExportCommonServer; | |||||
| import cn.afterturn.easypoi.exception.excel.ExcelExportException; | |||||
| import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum; | |||||
| import cn.afterturn.easypoi.exception.word.WordExportException; | import cn.afterturn.easypoi.exception.word.WordExportException; | ||||
| import cn.afterturn.easypoi.exception.word.enmus.WordExportEnum; | import cn.afterturn.easypoi.exception.word.enmus.WordExportEnum; | ||||
| import cn.afterturn.easypoi.util.PoiPublicUtil; | import cn.afterturn.easypoi.util.PoiPublicUtil; | ||||
| @@ -40,11 +43,11 @@ import cn.afterturn.easypoi.word.entity.params.ExcelListEntity; | |||||
| /** | /** | ||||
| * 解析实体类对象 复用注解 | * 解析实体类对象 复用注解 | ||||
| * | |||||
| * | |||||
| * @author JueYue | * @author JueYue | ||||
| * 2014年8月9日 下午10:30:57 | * 2014年8月9日 下午10:30:57 | ||||
| */ | */ | ||||
| public class ExcelEntityParse extends ExportBase { | |||||
| public class ExcelEntityParse extends ExportCommonServer { | |||||
| private static final Logger LOGGER = LoggerFactory.getLogger(ExcelEntityParse.class); | private static final Logger LOGGER = LoggerFactory.getLogger(ExcelEntityParse.class); | ||||
| @@ -56,62 +59,66 @@ public class ExcelEntityParse extends ExportBase { | |||||
| } | } | ||||
| private int createCells(int index, Object t, List<ExcelExportEntity> excelParams, | private int createCells(int index, Object t, List<ExcelExportEntity> excelParams, | ||||
| XWPFTable table, short rowHeight) throws Exception { | |||||
| ExcelExportEntity entity; | |||||
| XWPFTableRow row = table.insertNewTableRow(index); | |||||
| row.setHeight(rowHeight); | |||||
| int maxHeight = 1, cellNum = 0; | |||||
| for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | |||||
| entity = excelParams.get(k); | |||||
| if (entity.getList() != null) { | |||||
| Collection<?> list = (Collection<?>) entity.getMethod().invoke(t, new Object[] {}); | |||||
| int listC = 0; | |||||
| for (Object obj : list) { | |||||
| createListCells(index + listC, cellNum, obj, entity.getList(), table); | |||||
| listC++; | |||||
| } | |||||
| cellNum += entity.getList().size(); | |||||
| if (list != null && list.size() > maxHeight) { | |||||
| maxHeight = list.size(); | |||||
| } | |||||
| } else { | |||||
| Object value = getCellValue(entity, t); | |||||
| if (entity.getType() == 1) { | |||||
| setCellValue(row, value, cellNum++); | |||||
| XWPFTable table, short rowHeight) { | |||||
| try { | |||||
| ExcelExportEntity entity; | |||||
| XWPFTableRow row = table.insertNewTableRow(index); | |||||
| row.setHeight(rowHeight); | |||||
| int maxHeight = 1, cellNum = 0; | |||||
| for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | |||||
| entity = excelParams.get(k); | |||||
| if (entity.getList() != null) { | |||||
| Collection<?> list = (Collection<?>) entity.getMethod().invoke(t, new Object[]{}); | |||||
| int listC = 0; | |||||
| for (Object obj : list) { | |||||
| createListCells(index + listC, cellNum, obj, entity.getList(), table, rowHeight); | |||||
| listC++; | |||||
| } | |||||
| cellNum += entity.getList().size(); | |||||
| if (list != null && list.size() > maxHeight) { | |||||
| maxHeight = list.size(); | |||||
| } | |||||
| } else { | |||||
| Object value = getCellValue(entity, t); | |||||
| if (entity.getType() == 1) { | |||||
| setCellValue(row, value, cellNum++); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| } | |||||
| // 合并需要合并的单元格 | |||||
| cellNum = 0; | |||||
| for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | |||||
| entity = excelParams.get(k); | |||||
| if (entity.getList() != null) { | |||||
| cellNum += entity.getList().size(); | |||||
| } else if (entity.isNeedMerge() && maxHeight > 1) { | |||||
| table.setCellMargins(index, index + maxHeight - 1, cellNum, cellNum); | |||||
| cellNum++; | |||||
| // 合并需要合并的单元格 | |||||
| cellNum = 0; | |||||
| for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | |||||
| entity = excelParams.get(k); | |||||
| if (entity.getList() != null) { | |||||
| cellNum += entity.getList().size(); | |||||
| } else if (entity.isNeedMerge() && maxHeight > 1) { | |||||
| table.setCellMargins(index, index + maxHeight - 1, cellNum, cellNum); | |||||
| cellNum++; | |||||
| } | |||||
| } | } | ||||
| return maxHeight; | |||||
| } catch (Exception e) { | |||||
| LOGGER.error("excel cell export error ,data is :{}", ReflectionToStringBuilder.toString(t)); | |||||
| throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e); | |||||
| } | } | ||||
| return maxHeight; | |||||
| } | } | ||||
| /** | /** | ||||
| * 创建List之后的各个Cells | * 创建List之后的各个Cells | ||||
| * @param index | * @param index | ||||
| * @param cellNum | * @param cellNum | ||||
| * @param obj | |||||
| * @param excelParams | |||||
| * @param table | |||||
| * @param obj 当前对象 | |||||
| * @param excelParams 列参数信息 | |||||
| * @param table 当前表格 | |||||
| * @param rowHeight 行高 | |||||
| * @throws Exception | * @throws Exception | ||||
| */ | */ | ||||
| public void createListCells(int index, int cellNum, Object obj, | |||||
| List<ExcelExportEntity> excelParams, | |||||
| XWPFTable table) throws Exception { | |||||
| public void createListCells(int index, int cellNum, Object obj, List<ExcelExportEntity> excelParams, XWPFTable table, short rowHeight) throws Exception { | |||||
| ExcelExportEntity entity; | ExcelExportEntity entity; | ||||
| XWPFTableRow row; | XWPFTableRow row; | ||||
| if (table.getRow(index) == null) { | if (table.getRow(index) == null) { | ||||
| row = table.createRow(); | row = table.createRow(); | ||||
| row.setHeight(getRowHeight(excelParams)); | |||||
| row.setHeight(rowHeight); | |||||
| } else { | } else { | ||||
| row = table.getRow(index); | row = table.getRow(index); | ||||
| } | } | ||||
| @@ -126,7 +133,7 @@ public class ExcelEntityParse extends ExportBase { | |||||
| /** | /** | ||||
| * 获取表头数据 | * 获取表头数据 | ||||
| * | |||||
| * | |||||
| * @param table | * @param table | ||||
| * @param index | * @param index | ||||
| * @return | * @return | ||||
| @@ -152,7 +159,7 @@ public class ExcelEntityParse extends ExportBase { | |||||
| /** | /** | ||||
| * 解析上一行并生成更多行 | * 解析上一行并生成更多行 | ||||
| * | |||||
| * | |||||
| * @param table | * @param table | ||||
| * @param index | * @param index | ||||
| * @param entity | * @param entity | ||||
| @@ -172,7 +179,7 @@ public class ExcelEntityParse extends ExportBase { | |||||
| } | } | ||||
| // 获取实体对象的导出数据 | // 获取实体对象的导出数据 | ||||
| List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | ||||
| getAllExcelField(null, targetId, fileds, excelParams, entity.getClazz(), null); | |||||
| getAllExcelField(null, targetId, fileds, excelParams, entity.getClazz(), null, null); | |||||
| // 根据表头进行筛选排序 | // 根据表头进行筛选排序 | ||||
| sortAndFilterExportField(excelParams, titlemap); | sortAndFilterExportField(excelParams, titlemap); | ||||
| short rowHeight = getRowHeight(excelParams); | short rowHeight = getRowHeight(excelParams); | ||||
| @@ -190,16 +197,16 @@ public class ExcelEntityParse extends ExportBase { | |||||
| if (row.getCell(cellNum++) != null) { | if (row.getCell(cellNum++) != null) { | ||||
| row.getCell(cellNum - 1).setText(value == null ? "" : value.toString()); | row.getCell(cellNum - 1).setText(value == null ? "" : value.toString()); | ||||
| PoiPublicUtil.setWordText(row.createCell().addParagraph().createRun(), | PoiPublicUtil.setWordText(row.createCell().addParagraph().createRun(), | ||||
| value == null ? "" : value.toString()); | |||||
| value == null ? "" : value.toString()); | |||||
| } else { | } else { | ||||
| PoiPublicUtil.setWordText(row.createCell().addParagraph().createRun(), | PoiPublicUtil.setWordText(row.createCell().addParagraph().createRun(), | ||||
| value == null ? "" : value.toString()); | |||||
| value == null ? "" : value.toString()); | |||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * 对导出序列进行排序和塞选 | * 对导出序列进行排序和塞选 | ||||
| * | |||||
| * | |||||
| * @param excelParams | * @param excelParams | ||||
| * @param titlemap | * @param titlemap | ||||
| */ | */ | ||||