1.时区timezone 2.大数据导出函数,不兼容 3.样式准备删除 4.模板导出合并问题 5.BASE64Encoder 采用jdk8代码 6.sax优化读取类型 7.word 列表不支持图片问题4.1.3.A
| @@ -42,12 +42,14 @@ public @interface Excel { | |||
| * 导入的时间格式,以这个是否为空来判断是否需要格式化日期 | |||
| */ | |||
| public String importFormat() default ""; | |||
| /** | |||
| * 时间格式,相当于同时设置了exportFormat 和 importFormat | |||
| */ | |||
| public String format() default ""; | |||
| /** | |||
| * 时间时区 | |||
| */ | |||
| public String timezone() default ""; | |||
| /** | |||
| * 数字格式化,参数是Pattern,使用的对象是DecimalFormat | |||
| */ | |||
| @@ -1,13 +1,13 @@ | |||
| /** | |||
| * 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 | |||
| * <p> | |||
| * 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 | |||
| * <p> | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * <p> | |||
| * 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 | |||
| @@ -15,15 +15,6 @@ | |||
| */ | |||
| package cn.afterturn.easypoi.excel; | |||
| import java.util.Collection; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |||
| import org.apache.poi.ss.usermodel.Workbook; | |||
| import org.apache.poi.xssf.streaming.SXSSFWorkbook; | |||
| import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |||
| import cn.afterturn.easypoi.excel.entity.ExportParams; | |||
| import cn.afterturn.easypoi.excel.entity.TemplateExportParams; | |||
| import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; | |||
| @@ -31,76 +22,71 @@ import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | |||
| import cn.afterturn.easypoi.excel.export.ExcelBatchExportService; | |||
| import cn.afterturn.easypoi.excel.export.ExcelExportService; | |||
| import cn.afterturn.easypoi.excel.export.template.ExcelExportOfTemplateUtil; | |||
| import cn.afterturn.easypoi.handler.inter.IExcelExportServer; | |||
| import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |||
| import org.apache.poi.ss.usermodel.Workbook; | |||
| import org.apache.poi.xssf.streaming.SXSSFWorkbook; | |||
| import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |||
| import java.util.Collection; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| /** | |||
| * excel 导出工具类 | |||
| * | |||
| * | |||
| * @author JueYue | |||
| * @version 1.0 | |||
| * 2013-10-17 | |||
| * 2013-10-17 | |||
| */ | |||
| public final class ExcelExportUtil { | |||
| public static int USE_SXSSF_LIMIT = 100000; | |||
| public static final String SHEET_NAME = "sheetName"; | |||
| public static int USE_SXSSF_LIMIT = 100000; | |||
| public static final String SHEET_NAME = "sheetName"; | |||
| private ExcelExportUtil() { | |||
| } | |||
| /** | |||
| * 大数据量导出,方便导出时集合不用一次生成,可以多次生成,多次调用, | |||
| * 调用完成或需要调用关闭函数(closeExportBigExcel) | |||
| * 大数据量导出 | |||
| * | |||
| * @param entity | |||
| * 表格标题属性 | |||
| * @param pojoClass | |||
| * Excel对象Class | |||
| * @param dataSet | |||
| * Excel对象数据List | |||
| * @param entity 表格标题属性 | |||
| * @param pojoClass Excel对象Class | |||
| * @param server 查询数据的接口 | |||
| * @param queryParams 查询数据的参数 | |||
| */ | |||
| public static Workbook exportBigExcel(ExportParams entity, Class<?> pojoClass, | |||
| Collection<?> dataSet) { | |||
| ExcelBatchExportService batchService = ExcelBatchExportService | |||
| .getExcelBatchExportService(entity, pojoClass); | |||
| return batchService.appendData(dataSet); | |||
| IExcelExportServer server, Object queryParams) { | |||
| ExcelBatchExportService batchServer = new ExcelBatchExportService(); | |||
| batchServer.init(entity, pojoClass); | |||
| return batchServer.exportBigExcel(server, queryParams); | |||
| } | |||
| /** | |||
| * 大数据量导出,方便导出时集合不用一次生成,可以多次生成,多次调用, | |||
| * 调用完成或需要调用关闭函数(closeExportBigExcel) | |||
| * 大数据量导出 | |||
| * | |||
| * @param entity | |||
| * @param excelParams | |||
| * @param dataSet | |||
| * @param server 查询数据的接口 | |||
| * @param queryParams 查询数据的参数 | |||
| * @return | |||
| */ | |||
| public static Workbook exportBigExcel(ExportParams entity, List<ExcelExportEntity> excelParams, | |||
| Collection<?> dataSet) { | |||
| ExcelBatchExportService batchService = ExcelBatchExportService | |||
| .getExcelBatchExportService(entity, excelParams); | |||
| return batchService.appendData(dataSet); | |||
| IExcelExportServer server, Object queryParams) { | |||
| ExcelBatchExportService batchServer = new ExcelBatchExportService(); | |||
| batchServer.init(entity, excelParams); | |||
| return batchServer.exportBigExcel(server, queryParams); | |||
| } | |||
| /** | |||
| * 执行完成上述方法后,请关闭 | |||
| */ | |||
| public static void closeExportBigExcel() { | |||
| ExcelBatchExportService batchService = ExcelBatchExportService.getCurrentExcelBatchExportService(); | |||
| if(batchService != null) { | |||
| batchService.closeExportBigExcel(); | |||
| } | |||
| } | |||
| /** | |||
| * @param entity | |||
| * 表格标题属性 | |||
| * @param pojoClass | |||
| * Excel对象Class | |||
| * @param dataSet | |||
| * Excel对象数据List | |||
| * @param entity 表格标题属性 | |||
| * @param pojoClass Excel对象Class | |||
| * @param dataSet Excel对象数据List | |||
| */ | |||
| public static Workbook exportExcel(ExportParams entity, Class<?> pojoClass, | |||
| Collection<?> dataSet) { | |||
| Workbook workbook = getWorkbook(entity.getType(),dataSet.size()); | |||
| Workbook workbook = getWorkbook(entity.getType(), dataSet.size()); | |||
| new ExcelExportService().createSheet(workbook, entity, pojoClass, dataSet); | |||
| return workbook; | |||
| } | |||
| @@ -117,65 +103,57 @@ public final class ExcelExportUtil { | |||
| /** | |||
| * 根据Map创建对应的Excel | |||
| * @param entity | |||
| * 表格标题属性 | |||
| * @param entityList | |||
| * Map对象列表 | |||
| * @param dataSet | |||
| * Excel对象数据List | |||
| * | |||
| * @param entity 表格标题属性 | |||
| * @param entityList Map对象列表 | |||
| * @param dataSet Excel对象数据List | |||
| */ | |||
| public static Workbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList, | |||
| Collection<?> dataSet) { | |||
| Workbook workbook = getWorkbook(entity.getType(),dataSet.size());; | |||
| Workbook workbook = getWorkbook(entity.getType(), dataSet.size()); | |||
| ; | |||
| new ExcelExportService().createSheetForMap(workbook, entity, entityList, dataSet); | |||
| return workbook; | |||
| } | |||
| /** | |||
| * 根据Map创建对应的Excel(一个excel 创建多个sheet) | |||
| * | |||
| * @param list | |||
| * 多个Map key title 对应表格Title key entity 对应表格对应实体 key data | |||
| * Collection 数据 | |||
| * | |||
| * @param list 多个Map key title 对应表格Title key entity 对应表格对应实体 key data | |||
| * Collection 数据 | |||
| * @return | |||
| */ | |||
| public static Workbook exportExcel(List<Map<String, Object>> list, ExcelType type) { | |||
| Workbook workbook = getWorkbook(type,0); | |||
| Workbook workbook = getWorkbook(type, 0); | |||
| for (Map<String, Object> map : list) { | |||
| ExcelExportService service = new ExcelExportService(); | |||
| service.createSheet(workbook, (ExportParams) map.get("title"), | |||
| (Class<?>) map.get("entity"), (Collection<?>) map.get("data")); | |||
| (Class<?>) map.get("entity"), (Collection<?>) map.get("data")); | |||
| } | |||
| return workbook; | |||
| } | |||
| /** | |||
| * 导出文件通过模板解析,不推荐这个了,推荐全部通过模板来执行处理 | |||
| * | |||
| * @param params | |||
| * 导出参数类 | |||
| * @param pojoClass | |||
| * 对应实体 | |||
| * @param dataSet | |||
| * 实体集合 | |||
| * @param map | |||
| * 模板集合 | |||
| * | |||
| * @param params 导出参数类 | |||
| * @param pojoClass 对应实体 | |||
| * @param dataSet 实体集合 | |||
| * @param map 模板集合 | |||
| * @return | |||
| */ | |||
| @Deprecated | |||
| public static Workbook exportExcel(TemplateExportParams params, Class<?> pojoClass, | |||
| Collection<?> dataSet, Map<String, Object> map) { | |||
| return new ExcelExportOfTemplateUtil().createExcelByTemplate(params, pojoClass, dataSet, | |||
| map); | |||
| map); | |||
| } | |||
| /** | |||
| * 导出文件通过模板解析只有模板,没有集合 | |||
| * | |||
| * @param params | |||
| * 导出参数类 | |||
| * @param map | |||
| * 模板集合 | |||
| * | |||
| * @param params 导出参数类 | |||
| * @param map 模板集合 | |||
| * @return | |||
| */ | |||
| public static Workbook exportExcel(TemplateExportParams params, Map<String, Object> map) { | |||
| @@ -185,10 +163,9 @@ public final class ExcelExportUtil { | |||
| /** | |||
| * 导出文件通过模板解析只有模板,没有集合 | |||
| * 每个sheet对应一个map,导出到处,key是sheet的NUM | |||
| * @param params | |||
| * 导出参数类 | |||
| * @param map | |||
| * 模板集合 | |||
| * | |||
| * @param params 导出参数类 | |||
| * @param map 模板集合 | |||
| * @return | |||
| */ | |||
| public static Workbook exportExcel(Map<Integer, Map<String, Object>> map, | |||
| @@ -199,14 +176,13 @@ public final class ExcelExportUtil { | |||
| /** | |||
| * 导出文件通过模板解析只有模板,没有集合 | |||
| * 每个sheet对应一个list,按照数量进行导出排序,key是sheet的NUM | |||
| * @param params | |||
| * 导出参数类 | |||
| * @param map | |||
| * 模板集合 | |||
| * | |||
| * @param params 导出参数类 | |||
| * @param map 模板集合 | |||
| * @return | |||
| */ | |||
| public static Workbook exportExcelClone(Map<Integer, List<Map<String, Object>>> map, | |||
| TemplateExportParams params) { | |||
| TemplateExportParams params) { | |||
| return new ExcelExportOfTemplateUtil().createExcelCloneByTemplate(params, map); | |||
| } | |||
| @@ -1,13 +1,13 @@ | |||
| /** | |||
| * 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 | |||
| * <p> | |||
| * 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 | |||
| * <p> | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * <p> | |||
| * 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 | |||
| @@ -22,6 +22,6 @@ package cn.afterturn.easypoi.excel.entity.enmus; | |||
| */ | |||
| public enum CellValueType { | |||
| String , Number , Boolean , Date , TElement , Null ,Formula, None; | |||
| InlineStr, String, Number, Boolean, Date, TElement, Null, Formula, None; | |||
| } | |||
| @@ -66,6 +66,10 @@ public class ExcelBaseEntity { | |||
| * 固定的列 | |||
| */ | |||
| private Integer fixedIndex; | |||
| /** | |||
| * 时区 | |||
| */ | |||
| private String timezone; | |||
| private List<Method> methods; | |||
| @@ -162,5 +166,11 @@ public class ExcelBaseEntity { | |||
| this.fixedIndex = fixedIndex; | |||
| } | |||
| public String getTimezone() { | |||
| return timezone; | |||
| } | |||
| public void setTimezone(String timezone) { | |||
| this.timezone = timezone; | |||
| } | |||
| } | |||
| @@ -12,7 +12,8 @@ package cn.afterturn.easypoi.excel.entity.vo; | |||
| */ | |||
| public interface BaseEntityTypeConstants { | |||
| public final static Integer STRING_TYPE = 1; | |||
| public final static Integer DOUBLE_TYPE = 10; | |||
| public final static Integer IMAGE_TYPE = 2; | |||
| public final static Integer FUNCTION_TYPE = 3; | |||
| public final static Integer DATE_TYPE = 4; | |||
| public final static Integer DOUBLE_TYPE = 10; | |||
| } | |||
| @@ -7,6 +7,7 @@ import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | |||
| 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.handler.inter.IExcelExportServer; | |||
| import cn.afterturn.easypoi.util.PoiExcelGraphDataUtil; | |||
| import cn.afterturn.easypoi.util.PoiPublicUtil; | |||
| import org.apache.poi.ss.usermodel.Drawing; | |||
| @@ -17,23 +18,24 @@ import org.apache.poi.xssf.streaming.SXSSFWorkbook; | |||
| import java.lang.reflect.Field; | |||
| import java.util.*; | |||
| import static cn.afterturn.easypoi.excel.ExcelExportUtil.USE_SXSSF_LIMIT; | |||
| /** | |||
| * 提供批次插入服务 | |||
| * | |||
| * @author JueYue | |||
| * 2016年8月29日 | |||
| */ | |||
| public class ExcelBatchExportService extends ExcelExportService { | |||
| private static ThreadLocal<ExcelBatchExportService> THREAD_LOCAL = new ThreadLocal<ExcelBatchExportService>(); | |||
| private Workbook workbook; | |||
| private Sheet sheet; | |||
| private List<ExcelExportEntity> excelParams; | |||
| private ExportParams entity; | |||
| private int titleHeight; | |||
| private Drawing patriarch; | |||
| private short rowHeight; | |||
| private int index; | |||
| private Workbook workbook; | |||
| private Sheet sheet; | |||
| private List<ExcelExportEntity> excelParams; | |||
| private ExportParams entity; | |||
| private int titleHeight; | |||
| private Drawing patriarch; | |||
| private short rowHeight; | |||
| private int index; | |||
| public void init(ExportParams entity, Class<?> pojoClass) { | |||
| List<ExcelExportEntity> excelParams = createExcelExportEntityList(entity, pojoClass); | |||
| @@ -49,7 +51,7 @@ public class ExcelBatchExportService extends ExcelExportService { | |||
| super.type = entity.getType(); | |||
| createSheet(workbook, entity, excelParams); | |||
| if (entity.getMaxNum() == 0) { | |||
| entity.setMaxNum(1000000); | |||
| entity.setMaxNum(USE_SXSSF_LIMIT); | |||
| } | |||
| insertDataToSheet(workbook, entity, excelParams, null, sheet); | |||
| } | |||
| @@ -61,11 +63,11 @@ public class ExcelBatchExportService extends ExcelExportService { | |||
| excelParams.add(indexExcelEntity(entity)); | |||
| } | |||
| // 得到所有字段 | |||
| Field[] fileds = PoiPublicUtil.getClassFields(pojoClass); | |||
| ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); | |||
| String targetId = etarget == null ? null : etarget.value(); | |||
| Field[] fileds = PoiPublicUtil.getClassFields(pojoClass); | |||
| ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); | |||
| String targetId = etarget == null ? null : etarget.value(); | |||
| getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, | |||
| null, null); | |||
| null, null); | |||
| sortAllParams(excelParams); | |||
| return excelParams; | |||
| @@ -78,7 +80,7 @@ public class ExcelBatchExportService extends ExcelExportService { | |||
| if (LOGGER.isDebugEnabled()) { | |||
| LOGGER.debug("Excel export start ,List<ExcelExportEntity> is {}", excelParams); | |||
| LOGGER.debug("Excel version is {}", | |||
| entity.getType().equals(ExcelType.HSSF) ? "03" : "07"); | |||
| entity.getType().equals(ExcelType.HSSF) ? "03" : "07"); | |||
| } | |||
| if (workbook == null || entity == null || excelParams == null) { | |||
| throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); | |||
| @@ -100,7 +102,7 @@ public class ExcelBatchExportService extends ExcelExportService { | |||
| sheet = workbook.createSheet(); | |||
| index = 0; | |||
| } | |||
| Iterator<?> its = dataSet.iterator(); | |||
| while (its.hasNext()) { | |||
| Object t = its.next(); | |||
| @@ -127,7 +129,7 @@ public class ExcelBatchExportService extends ExcelExportService { | |||
| i18nHandler = entity.getI18nHandler(); | |||
| // 创建表格样式 | |||
| setExcelExportStyler((IExcelExportStyler) entity.getStyle() | |||
| .getConstructor(Workbook.class).newInstance(workbook)); | |||
| .getConstructor(Workbook.class).newInstance(workbook)); | |||
| patriarch = PoiExcelGraphDataUtil.getDrawingPatriarch(sheet); | |||
| List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | |||
| if (entity.isAddIndex()) { | |||
| @@ -136,10 +138,10 @@ public class ExcelBatchExportService extends ExcelExportService { | |||
| excelParams.addAll(entityList); | |||
| sortAllParams(excelParams); | |||
| this.index = entity.isCreateHeadRows() | |||
| ? createHeaderAndTitle(entity, sheet, workbook, excelParams) : 0; | |||
| ? createHeaderAndTitle(entity, sheet, workbook, excelParams) : 0; | |||
| titleHeight = index; | |||
| setCellWith(excelParams, sheet); | |||
| setColumnHidden(excelParams,sheet); | |||
| setColumnHidden(excelParams, sheet); | |||
| rowHeight = getRowHeight(excelParams); | |||
| setCurrentIndex(1); | |||
| } catch (Exception e) { | |||
| @@ -148,39 +150,25 @@ public class ExcelBatchExportService extends ExcelExportService { | |||
| } | |||
| } | |||
| public static ExcelBatchExportService getExcelBatchExportService(ExportParams entity, | |||
| Class<?> pojoClass) { | |||
| if (THREAD_LOCAL.get() == null) { | |||
| ExcelBatchExportService batchServer = new ExcelBatchExportService(); | |||
| batchServer.init(entity, pojoClass); | |||
| THREAD_LOCAL.set(batchServer); | |||
| } | |||
| return THREAD_LOCAL.get(); | |||
| } | |||
| public static ExcelBatchExportService getExcelBatchExportService(ExportParams entity, | |||
| List<ExcelExportEntity> excelParams) { | |||
| if (THREAD_LOCAL.get() == null) { | |||
| ExcelBatchExportService batchServer = new ExcelBatchExportService(); | |||
| batchServer.init(entity, excelParams); | |||
| THREAD_LOCAL.set(batchServer); | |||
| } | |||
| return THREAD_LOCAL.get(); | |||
| } | |||
| public static ExcelBatchExportService getCurrentExcelBatchExportService() { | |||
| return THREAD_LOCAL.get(); | |||
| } | |||
| public void closeExportBigExcel() { | |||
| public Workbook closeExportBigExcel() { | |||
| if (entity.getFreezeCol() != 0) { | |||
| sheet.createFreezePane(entity.getFreezeCol(), titleHeight, entity.getFreezeCol(), titleHeight); | |||
| } | |||
| mergeCells(sheet, excelParams, titleHeight); | |||
| // 创建合计信息 | |||
| addStatisticsRow(getExcelExportStyler().getStyles(true, null), sheet); | |||
| THREAD_LOCAL.remove(); | |||
| return workbook; | |||
| } | |||
| public Workbook exportBigExcel(IExcelExportServer server, Object queryParams) { | |||
| int page = 1; | |||
| List<Object> list = server | |||
| .selectListForExcelExport(queryParams, page++); | |||
| while (list != null && list.size() > 0) { | |||
| appendData(list); | |||
| list = server.selectListForExcelExport(queryParams, page++); | |||
| } | |||
| return closeExportBigExcel(); | |||
| } | |||
| } | |||
| @@ -21,6 +21,7 @@ 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.handler.inter.IExcelExportServer; | |||
| import cn.afterturn.easypoi.util.PoiExcelGraphDataUtil; | |||
| import cn.afterturn.easypoi.util.PoiMergeCellUtil; | |||
| import cn.afterturn.easypoi.util.PoiPublicUtil; | |||
| @@ -73,7 +74,7 @@ public abstract class BaseExportService extends ExportCommonService { | |||
| for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { | |||
| entity = excelParams.get(k); | |||
| if (entity.getList() != null) { | |||
| Collection<?> list = getListCellValue(entity, t); | |||
| Collection<?> list = getListCellValue(entity, t); | |||
| int listIndex = 0, tmpListHeight = 0; | |||
| if (list != null && list.size() > 0) { | |||
| int tempCellNum = 0; | |||
| @@ -93,29 +94,25 @@ public abstract class BaseExportService extends ExportCommonService { | |||
| 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(dataHandler.getHyperlink( | |||
| row.getSheet().getWorkbook().getCreationHelper(), t, | |||
| entity.getName(), value)); | |||
| } | |||
| } else if (entity.getType() == BaseEntityTypeConstants.DOUBLE_TYPE) { | |||
| 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(dataHandler.getHyperlink( | |||
| row.getSheet().getWorkbook().getCreationHelper(), t, | |||
| entity.getName(), value)); | |||
| } | |||
| } else { | |||
| createImageCell(patriarch, entity, row, cellNum++, | |||
| value == null ? "" : value.toString(), t); | |||
| } | |||
| if (entity.isHyperlink()) { | |||
| row.getCell(cellNum - 1) | |||
| .setHyperlink(dataHandler.getHyperlink( | |||
| row.getSheet().getWorkbook().getCreationHelper(), t, | |||
| entity.getName(), value)); | |||
| } | |||
| } | |||
| } | |||
| maxHeight += listMaxHeight - 1; | |||
| if(indexKey == 1 && excelParams.get(1).isNeedMerge()) { | |||
| if (indexKey == 1 && excelParams.get(1).isNeedMerge()) { | |||
| excelParams.get(0).setNeedMerge(true); | |||
| } | |||
| for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { | |||
| @@ -210,8 +207,7 @@ public abstract class BaseExportService extends ExportCommonService { | |||
| } | |||
| private int createIndexCell(Row row, int index, ExcelExportEntity excelExportEntity) { | |||
| if (excelExportEntity.getName() != null && "序号".equals(excelExportEntity.getName()) && excelExportEntity.getFormat() != null | |||
| && excelExportEntity.getFormat().equals(PoiBaseConstants.IS_ADD_INDEX)) { | |||
| if (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; | |||
| @@ -307,7 +303,7 @@ public abstract class BaseExportService extends ExportCommonService { | |||
| cell.setCellValue(text); | |||
| } | |||
| } | |||
| if (style != null) { | |||
| cell.setCellStyle(style); | |||
| } | |||
| @@ -105,6 +105,9 @@ public class ExportCommonService { | |||
| } | |||
| if (temp != null) { | |||
| SimpleDateFormat format = new SimpleDateFormat(entity.getFormat()); | |||
| if(StringUtils.isNotEmpty(entity.getTimezone())){ | |||
| format.setTimeZone(TimeZone.getTimeZone(entity.getTimezone())); | |||
| } | |||
| value = format.format(temp); | |||
| } | |||
| return value; | |||
| @@ -262,6 +265,7 @@ public class ExportCommonService { | |||
| excelEntity.setColumnHidden(excel.isColumnHidden()); | |||
| excelEntity.setDict(excel.dict()); | |||
| excelEntity.setEnumExportField(excel.enumExportField()); | |||
| excelEntity.setTimezone(excel.timezone()); | |||
| if (excelGroup != null) { | |||
| excelEntity.setGroupName(PoiPublicUtil.getValueByTargetId(excelGroup.name(), targetId, null)); | |||
| } else { | |||
| @@ -375,6 +379,7 @@ public class ExportCommonService { | |||
| ExcelExportEntity exportEntity = new ExcelExportEntity(); | |||
| //保证是第一排 | |||
| exportEntity.setOrderNum(Integer.MIN_VALUE); | |||
| exportEntity.setNeedMerge(true); | |||
| exportEntity.setName(entity.getIndexName()); | |||
| exportEntity.setWidth(10); | |||
| exportEntity.setFormat(PoiBaseConstants.IS_ADD_INDEX); | |||
| @@ -16,6 +16,7 @@ | |||
| package cn.afterturn.easypoi.excel.export.styler; | |||
| import org.apache.poi.ss.usermodel.*; | |||
| import org.apache.poi.util.Removal; | |||
| /** | |||
| * 带有边框的Excel样式 | |||
| @@ -23,6 +24,10 @@ import org.apache.poi.ss.usermodel.*; | |||
| * @author JueYue | |||
| * 2015年1月9日 下午5:55:29 | |||
| */ | |||
| @Deprecated | |||
| @Removal( | |||
| version = "4.2" | |||
| ) | |||
| public class ExcelExportStylerBorderImpl extends AbstractExcelExportStyler | |||
| implements IExcelExportStyler { | |||
| @@ -16,12 +16,17 @@ | |||
| package cn.afterturn.easypoi.excel.export.styler; | |||
| import org.apache.poi.ss.usermodel.*; | |||
| import org.apache.poi.util.Removal; | |||
| /** | |||
| * 带有样式的导出服务 | |||
| * @author JueYue | |||
| * 2015年1月9日 下午4:54:15 | |||
| */ | |||
| @Deprecated | |||
| @Removal( | |||
| version = "4.2" | |||
| ) | |||
| public class ExcelExportStylerColorImpl extends AbstractExcelExportStyler | |||
| implements IExcelExportStyler { | |||
| @@ -20,6 +20,7 @@ import org.apache.poi.ss.usermodel.CellStyle; | |||
| import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | |||
| import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams; | |||
| import org.apache.poi.util.Removal; | |||
| /** | |||
| * Excel导出样式接口 | |||
| @@ -42,6 +43,9 @@ public interface IExcelExportStyler { | |||
| * 获取样式方法 | |||
| */ | |||
| @Deprecated | |||
| @Removal( | |||
| version = "4.2" | |||
| ) | |||
| public CellStyle getStyles(boolean parity, ExcelExportEntity entity); | |||
| /** | |||
| @@ -104,7 +104,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| sheet.getLastRowNum(), shiftRows, true, true); | |||
| mergedRegionHelper.shiftRows(sheet, teplateParams.getHeadingRows() + teplateParams.getHeadingStartRow(), shiftRows); | |||
| templateSumHandler.shiftRows(teplateParams.getHeadingRows() + teplateParams.getHeadingStartRow(), shiftRows); | |||
| PoiExcelTempUtil.reset(sheet,teplateParams.getHeadingRows() + teplateParams.getHeadingStartRow(), sheet.getLastRowNum()); | |||
| PoiExcelTempUtil.reset(sheet, teplateParams.getHeadingRows() + teplateParams.getHeadingStartRow(), sheet.getLastRowNum()); | |||
| if (excelParams.size() == 0) { | |||
| return; | |||
| } | |||
| @@ -157,7 +157,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| cell.getRow().getSheet().shiftRows(cell.getRowIndex() + rowspan, lastRowNum, (datas.size() - 1) * rowspan, true, true); | |||
| mergedRegionHelper.shiftRows(cell.getSheet(), cell.getRowIndex() + rowspan, (datas.size() - 1) * rowspan); | |||
| templateSumHandler.shiftRows(cell.getRowIndex() + rowspan, (datas.size() - 1) * rowspan); | |||
| PoiExcelTempUtil.reset(cell.getSheet(),cell.getRowIndex() + rowspan+ (datas.size() - 1) * rowspan, cell.getRow().getSheet().getLastRowNum()); | |||
| PoiExcelTempUtil.reset(cell.getSheet(), cell.getRowIndex() + rowspan + (datas.size() - 1) * rowspan, cell.getRow().getSheet().getLastRowNum()); | |||
| } | |||
| while (its.hasNext()) { | |||
| Object t = its.next(); | |||
| @@ -193,7 +193,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| * @param excelParams | |||
| * @throws Exception | |||
| */ | |||
| public int getOneObjectSize(Object t, List<ExcelExportEntity> excelParams) throws Exception { | |||
| private int getOneObjectSize(Object t, List<ExcelExportEntity> excelParams) throws Exception { | |||
| ExcelExportEntity entity; | |||
| int maxHeight = 1; | |||
| for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | |||
| @@ -221,7 +221,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| this.teplateParams = params; | |||
| wb = ExcelCache.getWorkbook(teplateParams.getTemplateUrl(), teplateParams.getSheetNum(), | |||
| true); | |||
| int oldSheetNum = wb.getNumberOfSheets(); | |||
| int oldSheetNum = wb.getNumberOfSheets(); | |||
| List<String> oldSheetName = new ArrayList<>(); | |||
| for (int i = 0; i < oldSheetNum; i++) { | |||
| oldSheetName.add(wb.getSheetName(i)); | |||
| @@ -311,9 +311,6 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| } else { | |||
| wb = getCloneWorkBook(); | |||
| } | |||
| if (wb instanceof XSSFWorkbook) { | |||
| super.type = ExcelType.XSSF; | |||
| } | |||
| // 创建表格样式 | |||
| setExcelExportStyler((IExcelExportStyler) teplateParams.getStyle() | |||
| .getConstructor(Workbook.class).newInstance(wb)); | |||
| @@ -382,6 +379,9 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| private void parseTemplate(Sheet sheet, Map<String, Object> map, | |||
| boolean colForeach) throws Exception { | |||
| if (sheet.getWorkbook() instanceof XSSFWorkbook) { | |||
| super.type = ExcelType.XSSF; | |||
| } | |||
| deleteCell(sheet, map); | |||
| mergedRegionHelper = new MergedRegionHelper(sheet); | |||
| templateSumHandler = new TemplateSumHandler(sheet); | |||
| @@ -410,8 +410,12 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| private void handlerSumCell(Sheet sheet) { | |||
| for (TemplateSumEntity sumEntity : templateSumHandler.getDataList()) { | |||
| Cell cell = sheet.getRow(sumEntity.getRow()).getCell(sumEntity.getCol()); | |||
| cell.setCellValue(cell.getStringCellValue() | |||
| .replace("sum:(" + sumEntity.getSumKey() + ")", sumEntity.getValue() + "")); | |||
| if (cell.getStringCellValue().contains(sumEntity.getSumKey())) { | |||
| cell.setCellValue(cell.getStringCellValue() | |||
| .replace("sum:(" + sumEntity.getSumKey() + ")", sumEntity.getValue() + "")); | |||
| } else { | |||
| cell.setCellValue(cell.getStringCellValue() + sumEntity.getValue()); | |||
| } | |||
| } | |||
| } | |||
| @@ -529,7 +533,6 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| return; | |||
| } | |||
| String oldString; | |||
| cell.setCellType(CellType.STRING); | |||
| oldString = cell.getStringCellValue(); | |||
| if (oldString != null && oldString.indexOf(START_STR) != -1 | |||
| && !oldString.contains(FOREACH)) { | |||
| @@ -540,6 +543,11 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| isNumber = true; | |||
| oldString = oldString.replaceFirst(NUMBER_SYMBOL, ""); | |||
| } | |||
| boolean isStyleBySelf = false; | |||
| if (isStyleBySelf(oldString)) { | |||
| isStyleBySelf = true; | |||
| oldString = oldString.replaceFirst(NUMBER_SYMBOL, ""); | |||
| } | |||
| Object obj = PoiPublicUtil.getRealValue(oldString, map); | |||
| //如何是数值 类型,就按照数值类型进行设置// 如果是图片就设置为图片 | |||
| if (obj instanceof ImageEntity) { | |||
| @@ -553,7 +561,6 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| createImageCell(cell, img.getHeight(), img.getRowspan(), img.getColspan(), img.getUrl(), img.getData()); | |||
| } else if (isNumber && StringUtils.isNotBlank(obj.toString())) { | |||
| cell.setCellValue(Double.parseDouble(obj.toString())); | |||
| cell.setCellType(CellType.NUMERIC); | |||
| } else { | |||
| cell.setCellValue(obj.toString()); | |||
| } | |||
| @@ -570,6 +577,11 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| || text.contains(" " + NUMBER_SYMBOL); | |||
| } | |||
| private boolean isStyleBySelf(String text) { | |||
| return text.startsWith(STYLE_SELF) || text.contains("{" + STYLE_SELF) | |||
| || text.contains(" " + STYLE_SELF); | |||
| } | |||
| /** | |||
| * 创建并返回第一个Row | |||
| * | |||
| @@ -620,8 +632,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| ExcelForEachParams params; | |||
| row = row.getSheet().getRow(row.getRowNum() - rowspan + 1); | |||
| for (int k = 0; k < rowspan; k++) { | |||
| int ci = columnIndex;//cell的序号 | |||
| int ci = columnIndex; | |||
| short high = columns.get(0).getHeight(); | |||
| int n = k; | |||
| while (n > 0) { | |||
| @@ -672,7 +683,6 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| createImageCell(row.getCell(ci), img.getHeight(), img.getRowspan(), img.getColspan(), img.getUrl(), img.getData()); | |||
| } else if (isNumber && StringUtils.isNotEmpty(val)) { | |||
| row.getCell(ci).setCellValue(Double.parseDouble(val)); | |||
| row.getCell(ci).setCellType(CellType.NUMERIC); | |||
| } else { | |||
| try { | |||
| row.getCell(ci).setCellValue(val); | |||
| @@ -680,7 +690,9 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| LOGGER.error(e.getMessage(), e); | |||
| } | |||
| } | |||
| row.getCell(ci).setCellStyle(params.getCellStyle()); // plq modify at 2017-11-13 | |||
| if(params.getCellStyle() != null){ | |||
| row.getCell(ci).setCellStyle(params.getCellStyle()); | |||
| } | |||
| //判断这个属性是不是需要统计 | |||
| if (params.isNeedSum()) { | |||
| templateSumHandler.addValueOfKey(params.getName(), val); | |||
| @@ -13,7 +13,6 @@ import org.apache.poi.xssf.usermodel.XSSFSheet; | |||
| import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import sun.misc.BASE64Encoder; | |||
| import java.text.SimpleDateFormat; | |||
| import java.util.*; | |||
| @@ -275,8 +274,8 @@ public class ExcelToHtmlService { | |||
| byte[] data = pictureData.getData(); | |||
| //直接输出到HTML使用BASE64Encoder | |||
| // 加密 | |||
| BASE64Encoder encoder = new BASE64Encoder(); | |||
| return encoder.encode(data); | |||
| Base64.Encoder encoder = Base64.getEncoder(); | |||
| return encoder.encodeToString(data); | |||
| /*String fileName = "pic" + Math.round(Math.random() * 100000000000L); | |||
| fileName += "." + PoiPublicUtil.getFileExtendName(data); | |||
| if (!imageCachePath.startsWith("/") && !imageCachePath.contains(":")) { | |||
| @@ -173,8 +173,12 @@ public class CellValueService { | |||
| try { | |||
| return format.parse(value); | |||
| } catch (ParseException e) { | |||
| LOGGER.error("时间格式化失败,格式化:{},值:{}", entity.getFormat(), value); | |||
| throw new ExcelImportException(ExcelImportEnum.GET_VALUE_ERROR); | |||
| try { | |||
| return DateUtil.getJavaDate(Double.parseDouble(value)); | |||
| } catch (NumberFormatException ex) { | |||
| LOGGER.error("时间格式化失败,格式化:{},值:{}", entity.getFormat(), value); | |||
| throw new ExcelImportException(ExcelImportEnum.GET_VALUE_ERROR); | |||
| } | |||
| } | |||
| } | |||
| return null; | |||
| @@ -82,7 +82,7 @@ public class SheetHandler extends DefaultHandler { | |||
| return; | |||
| } | |||
| if (INLINE_STR.equals(cellType)) { | |||
| type = CellValueType.String; | |||
| type = CellValueType.InlineStr; | |||
| return; | |||
| } | |||
| if (FORMULA.equals(cellType)) { | |||
| @@ -126,7 +126,6 @@ public class SheetHandler extends DefaultHandler { | |||
| int idx = Integer.parseInt(lastContents); | |||
| lastContents = sharedStringsTable.getItemAt(idx).getString(); | |||
| } catch (Exception e) { | |||
| } | |||
| } | |||
| //t元素也包含字符串 | |||
| @@ -146,7 +145,7 @@ public class SheetHandler extends DefaultHandler { | |||
| } else if (CellValueType.Number.equals(type)) { | |||
| BigDecimal bd = new BigDecimal(value); | |||
| rowList.add(curCol, new SaxReadCellEntity(CellValueType.Number, bd)); | |||
| } else if (CellValueType.String.equals(type)) { | |||
| } else if (CellValueType.String.equals(type) || CellValueType.InlineStr.equals(type)) { | |||
| rowList.add(curCol, new SaxReadCellEntity(CellValueType.String, value)); | |||
| } | |||
| curCol++; | |||
| @@ -4,16 +4,18 @@ import java.util.List; | |||
| /** | |||
| * 导出数据接口 | |||
| * | |||
| * @author JueYue | |||
| * 2016年9月8日 | |||
| */ | |||
| public interface IExcelExportServer { | |||
| /** | |||
| * 查询数据接口 | |||
| * @param obj 查询条件 | |||
| * @param page 当前页数 | |||
| * | |||
| * @param queryParams 查询条件 | |||
| * @param page 当前页数从1开始 | |||
| * @return | |||
| */ | |||
| public List<Object> selectListForExcelExport(Object obj, int page); | |||
| public List<Object> selectListForExcelExport(Object queryParams, int page); | |||
| } | |||
| @@ -40,6 +40,7 @@ public final class PoiElUtil { | |||
| public static final String END_STR = "}}"; | |||
| public static final String WRAP = "]]"; | |||
| public static final String NUMBER_SYMBOL = "n:"; | |||
| public static final String STYLE_SELF = "sy:"; | |||
| public static final String FORMAT_DATE = "fd:"; | |||
| public static final String FORMAT_NUMBER = "fn:"; | |||
| public static final String SUM = "sum:"; | |||
| @@ -215,7 +215,7 @@ public final class PoiMergeCellUtil { | |||
| try { | |||
| sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol)); | |||
| } catch (Exception e) { | |||
| LOGGER.error("发生了一次合并单元格错误,{},{},{},{}", new Integer[]{ | |||
| LOGGER.debug("发生了一次合并单元格错误,{},{},{},{}", new Integer[]{ | |||
| firstRow, lastRow, firstCol, lastCol | |||
| }); | |||
| // 忽略掉合并的错误,不打印异常 | |||
| @@ -46,32 +46,6 @@ public class ParseWord07 { | |||
| private static final Logger LOGGER = LoggerFactory.getLogger(ParseWord07.class); | |||
| /** | |||
| * 添加图片 | |||
| * | |||
| * @param obj | |||
| * @param currentRun | |||
| * @throws Exception | |||
| * @author JueYue | |||
| * 2013-11-20 | |||
| */ | |||
| private void addAnImage(ImageEntity obj, XWPFRun currentRun) throws Exception { | |||
| Object[] isAndType = PoiPublicUtil.getIsAndType(obj); | |||
| String picId; | |||
| try { | |||
| picId = currentRun.getDocument().addPictureData((byte[]) isAndType[0], | |||
| (Integer) isAndType[1]); | |||
| ((MyXWPFDocument) currentRun.getDocument()).createPicture(currentRun, | |||
| picId, currentRun.getDocument() | |||
| .getNextPicNameNumber((Integer) isAndType[1]), | |||
| obj.getWidth(), obj.getHeight()); | |||
| } catch (Exception e) { | |||
| LOGGER.error(e.getMessage(), e); | |||
| } | |||
| } | |||
| /** | |||
| * 根据条件改变值 | |||
| * | |||
| @@ -84,7 +58,7 @@ public class ParseWord07 { | |||
| Object obj = PoiPublicUtil.getRealValue(currentText, map); | |||
| if (obj instanceof ImageEntity) {// 如果是图片就设置为图片 | |||
| currentRun.setText("", 0); | |||
| addAnImage((ImageEntity) obj, currentRun); | |||
| ExcelMapParse.addAnImage((ImageEntity) obj, currentRun); | |||
| } else { | |||
| currentText = obj.toString(); | |||
| PoiPublicUtil.setWordText(currentRun, currentText); | |||
| @@ -1,12 +1,12 @@ | |||
| /** | |||
| * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) | |||
| * | |||
| * <p> | |||
| * 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 | |||
| * | |||
| * <p> | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * | |||
| * <p> | |||
| * 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. | |||
| @@ -15,24 +15,7 @@ | |||
| */ | |||
| package cn.afterturn.easypoi.word.parse.excel; | |||
| import java.lang.reflect.Field; | |||
| import java.util.ArrayList; | |||
| import java.util.Collection; | |||
| import java.util.HashMap; | |||
| import java.util.Iterator; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | |||
| import org.apache.poi.xwpf.usermodel.XWPFParagraph; | |||
| import org.apache.poi.xwpf.usermodel.XWPFRun; | |||
| import org.apache.poi.xwpf.usermodel.XWPFTable; | |||
| import org.apache.poi.xwpf.usermodel.XWPFTableCell; | |||
| import org.apache.poi.xwpf.usermodel.XWPFTableRow; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import cn.afterturn.easypoi.entity.ImageEntity; | |||
| import cn.afterturn.easypoi.excel.annotation.ExcelTarget; | |||
| import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | |||
| import cn.afterturn.easypoi.excel.export.base.ExportCommonService; | |||
| @@ -42,12 +25,20 @@ import cn.afterturn.easypoi.exception.word.WordExportException; | |||
| import cn.afterturn.easypoi.exception.word.enmus.WordExportEnum; | |||
| import cn.afterturn.easypoi.util.PoiPublicUtil; | |||
| import cn.afterturn.easypoi.word.entity.params.ExcelListEntity; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | |||
| import org.apache.poi.xwpf.usermodel.*; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import java.lang.reflect.Field; | |||
| import java.util.*; | |||
| /** | |||
| * 解析实体类对象 复用注解 | |||
| * | |||
| * @author JueYue | |||
| * 2014年8月9日 下午10:30:57 | |||
| * 2014年8月9日 下午10:30:57 | |||
| */ | |||
| public class ExcelEntityParse extends ExportCommonService { | |||
| @@ -64,7 +55,7 @@ public class ExcelEntityParse extends ExportCommonService { | |||
| XWPFTable table, short rowHeight) { | |||
| try { | |||
| ExcelExportEntity entity; | |||
| XWPFTableRow row = table.insertNewTableRow(index); | |||
| XWPFTableRow row = table.insertNewTableRow(index); | |||
| if (rowHeight != -1) { | |||
| row.setHeight(rowHeight); | |||
| } | |||
| @@ -72,8 +63,8 @@ public class ExcelEntityParse extends ExportCommonService { | |||
| 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; | |||
| 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++; | |||
| @@ -109,17 +100,18 @@ public class ExcelEntityParse extends ExportCommonService { | |||
| /** | |||
| * 创建List之后的各个Cells | |||
| * | |||
| * @param index | |||
| * @param cellNum | |||
| * @param obj 当前对象 | |||
| * @param excelParams 列参数信息 | |||
| * @param table 当前表格 | |||
| * @param rowHeight 行高 | |||
| * @param obj 当前对象 | |||
| * @param excelParams 列参数信息 | |||
| * @param table 当前表格 | |||
| * @param rowHeight 行高 | |||
| * @throws Exception | |||
| */ | |||
| public void createListCells(int index, int cellNum, Object obj, List<ExcelExportEntity> excelParams, XWPFTable table, short rowHeight) throws Exception { | |||
| ExcelExportEntity entity; | |||
| XWPFTableRow row; | |||
| XWPFTableRow row; | |||
| if (table.getRow(index) == null) { | |||
| row = table.createRow(); | |||
| row.setHeight(rowHeight); | |||
| @@ -147,7 +139,7 @@ public class ExcelEntityParse extends ExportCommonService { | |||
| throw new WordExportException(WordExportEnum.EXCEL_NO_HEAD); | |||
| } | |||
| Map<String, Integer> map = new HashMap<String, Integer>(); | |||
| String text; | |||
| String text; | |||
| for (int j = 0; j < headRows; j++) { | |||
| List<XWPFTableCell> cells = table.getRow(index - j - 1).getTableCells(); | |||
| for (int i = 0; i < cells.size(); i++) { | |||
| @@ -175,9 +167,9 @@ public class ExcelEntityParse extends ExportCommonService { | |||
| Map<String, Integer> titlemap = getTitleMap(table, index, entity.getHeadRows()); | |||
| try { | |||
| // 得到所有字段 | |||
| Field[] fileds = PoiPublicUtil.getClassFields(entity.getClazz()); | |||
| ExcelTarget etarget = entity.getClazz().getAnnotation(ExcelTarget.class); | |||
| String targetId = null; | |||
| Field[] fileds = PoiPublicUtil.getClassFields(entity.getClazz()); | |||
| ExcelTarget etarget = entity.getClazz().getAnnotation(ExcelTarget.class); | |||
| String targetId = null; | |||
| if (etarget != null) { | |||
| targetId = etarget.value(); | |||
| } | |||
| @@ -186,8 +178,8 @@ public class ExcelEntityParse extends ExportCommonService { | |||
| getAllExcelField(null, targetId, fileds, excelParams, entity.getClazz(), null, null); | |||
| // 根据表头进行筛选排序 | |||
| sortAndFilterExportField(excelParams, titlemap); | |||
| short rowHeight = getRowHeight(excelParams); | |||
| Iterator<?> its = entity.getList().iterator(); | |||
| short rowHeight = getRowHeight(excelParams); | |||
| Iterator<?> its = entity.getList().iterator(); | |||
| while (its.hasNext()) { | |||
| Object t = its.next(); | |||
| index += createCells(index, t, excelParams, table, rowHeight); | |||
| @@ -200,34 +192,38 @@ public class ExcelEntityParse extends ExportCommonService { | |||
| private void setCellValue(XWPFTableRow row, Object value, int cellNum) { | |||
| if (row.getCell(cellNum++) != null) { | |||
| row.getCell(cellNum - 1).setText(value == null ? "" : value.toString()); | |||
| setWordText(row, value); | |||
| } else { | |||
| setWordText(row, value); | |||
| } | |||
| setWordText(row, value); | |||
| } | |||
| /** | |||
| * 解决word导出表格多出的换行符问题 | |||
| * | |||
| * @param row | |||
| * @param value | |||
| */ | |||
| private void setWordText(XWPFTableRow row, Object value) { | |||
| XWPFTableCell cell = row.createCell(); | |||
| XWPFTableCell cell = row.createCell(); | |||
| List<XWPFParagraph> paragraphs = cell.getParagraphs(); | |||
| XWPFParagraph paragraph = null; | |||
| XWPFRun run = null; | |||
| if(paragraphs != null && paragraphs.size() > 0) { | |||
| XWPFParagraph paragraph = null; | |||
| XWPFRun run = null; | |||
| if (paragraphs != null && paragraphs.size() > 0) { | |||
| paragraph = paragraphs.get(0); | |||
| } else { | |||
| paragraph = row.createCell().addParagraph(); | |||
| } | |||
| List<XWPFRun> runs = paragraph.getRuns(); | |||
| if(runs != null && runs.size() > 0) { | |||
| if (runs != null && runs.size() > 0) { | |||
| run = runs.get(0); | |||
| } else { | |||
| run = paragraph.createRun(); | |||
| } | |||
| PoiPublicUtil.setWordText(run, value == null ? "" : value.toString()); | |||
| if (value instanceof ImageEntity){ | |||
| ExcelMapParse.addAnImage((ImageEntity) value, run); | |||
| }else{ | |||
| PoiPublicUtil.setWordText(run, value == null ? "" : value.toString()); | |||
| } | |||
| } | |||
| /** | |||
| @@ -13,8 +13,12 @@ | |||
| */ | |||
| package cn.afterturn.easypoi.word.parse.excel; | |||
| import cn.afterturn.easypoi.entity.ImageEntity; | |||
| import cn.afterturn.easypoi.util.PoiPublicUtil; | |||
| import cn.afterturn.easypoi.word.entity.MyXWPFDocument; | |||
| import com.google.common.collect.Maps; | |||
| import org.apache.poi.xwpf.usermodel.XWPFRun; | |||
| import org.apache.poi.xwpf.usermodel.XWPFTable; | |||
| import org.apache.poi.xwpf.usermodel.XWPFTableCell; | |||
| import org.apache.poi.xwpf.usermodel.XWPFTableRow; | |||
| @@ -44,6 +48,32 @@ public final class ExcelMapParse { | |||
| private static final Logger LOGGER = LoggerFactory.getLogger(ExcelMapParse.class); | |||
| /** | |||
| * 添加图片 | |||
| * | |||
| * @param obj | |||
| * @param currentRun | |||
| * @throws Exception | |||
| * @author JueYue | |||
| * 2013-11-20 | |||
| */ | |||
| public static void addAnImage(ImageEntity obj, XWPFRun currentRun) { | |||
| try { | |||
| Object[] isAndType = PoiPublicUtil.getIsAndType(obj); | |||
| String picId; | |||
| picId = currentRun.getDocument().addPictureData((byte[]) isAndType[0], | |||
| (Integer) isAndType[1]); | |||
| ((MyXWPFDocument) currentRun.getDocument()).createPicture(currentRun, | |||
| picId, currentRun.getDocument() | |||
| .getNextPicNameNumber((Integer) isAndType[1]), | |||
| obj.getWidth(), obj.getHeight()); | |||
| } catch (Exception e) { | |||
| LOGGER.error(e.getMessage(), e); | |||
| } | |||
| } | |||
| /** | |||
| * 解析参数行,获取参数列表 | |||
| * | |||
| @@ -83,8 +113,7 @@ public final class ExcelMapParse { | |||
| //保存这一行的样式是-后面好统一设置 | |||
| List<XWPFTableCell> tempCellList = new ArrayList<XWPFTableCell>(); | |||
| tempCellList.addAll(table.getRow(index).getTableCells()); | |||
| int templateInde = index; | |||
| int cellIndex = 0;// 创建完成对象一行好像多了一个cell | |||
| int cellIndex = 0; | |||
| Map<String, Object> tempMap = Maps.newHashMap(); | |||
| LOGGER.debug("start for each data list :{}", list.size()); | |||
| for (Object obj : list) { | |||
| @@ -103,7 +132,7 @@ public final class ExcelMapParse { | |||
| currentRow.createCell(), val); | |||
| } | |||
| } | |||
| table.removeRow(index);// 移除这一行 | |||
| table.removeRow(index); | |||
| } | |||
| @@ -32,6 +32,6 @@ public interface BigExcelConstants extends BasePOIConstants { | |||
| /** | |||
| * 查询函数 | |||
| */ | |||
| public final static String DATA_INTER = "dataInterf"; | |||
| public final static String DATA_INTER = "dataInter"; | |||
| } | |||
| @@ -24,8 +24,6 @@ import org.springframework.stereotype.Controller; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| import javax.servlet.http.HttpServletResponse; | |||
| import java.util.Collections; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| /** | |||
| @@ -44,19 +42,9 @@ public class EasypoiBigExcelExportView extends MiniAbstractExcelView { | |||
| String codedFileName = "临时文件"; | |||
| Workbook workbook = ExcelExportUtil.exportBigExcel( | |||
| (ExportParams) model.get(BigExcelConstants.PARAMS), | |||
| (Class<?>) model.get(BigExcelConstants.CLASS), Collections.EMPTY_LIST); | |||
| IExcelExportServer server = (IExcelExportServer) model.get(BigExcelConstants.DATA_INTER); | |||
| int page = 1; | |||
| List<Object> list = server | |||
| .selectListForExcelExport(model.get(BigExcelConstants.DATA_PARAMS), page++); | |||
| while (list != null && list.size() > 0) { | |||
| workbook = ExcelExportUtil.exportBigExcel( | |||
| (ExportParams) model.get(BigExcelConstants.PARAMS), | |||
| (Class<?>) model.get(BigExcelConstants.CLASS), list); | |||
| list = server.selectListForExcelExport(model.get(BigExcelConstants.DATA_PARAMS), | |||
| page++); | |||
| } | |||
| ExcelExportUtil.closeExportBigExcel(); | |||
| (Class<?>) model.get(BigExcelConstants.CLASS), | |||
| (IExcelExportServer) model.get(BigExcelConstants.DATA_INTER), | |||
| model.get(BigExcelConstants.DATA_PARAMS)); | |||
| if (model.containsKey(BigExcelConstants.FILE_NAME)) { | |||
| codedFileName = (String) model.get(BigExcelConstants.FILE_NAME); | |||
| } | |||