@@ -42,12 +42,14 @@ public @interface Excel { | |||||
* 导入的时间格式,以这个是否为空来判断是否需要格式化日期 | * 导入的时间格式,以这个是否为空来判断是否需要格式化日期 | ||||
*/ | */ | ||||
public String importFormat() default ""; | public String importFormat() default ""; | ||||
/** | /** | ||||
* 时间格式,相当于同时设置了exportFormat 和 importFormat | * 时间格式,相当于同时设置了exportFormat 和 importFormat | ||||
*/ | */ | ||||
public String format() default ""; | public String format() default ""; | ||||
/** | |||||
* 时间时区 | |||||
*/ | |||||
public String timezone() default ""; | |||||
/** | /** | ||||
* 数字格式化,参数是Pattern,使用的对象是DecimalFormat | * 数字格式化,参数是Pattern,使用的对象是DecimalFormat | ||||
*/ | */ | ||||
@@ -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 | |||||
* | |||||
* 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, | * 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 | ||||
@@ -15,15 +15,6 @@ | |||||
*/ | */ | ||||
package cn.afterturn.easypoi.excel; | 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.ExportParams; | ||||
import cn.afterturn.easypoi.excel.entity.TemplateExportParams; | import cn.afterturn.easypoi.excel.entity.TemplateExportParams; | ||||
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; | 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.ExcelBatchExportService; | ||||
import cn.afterturn.easypoi.excel.export.ExcelExportService; | import cn.afterturn.easypoi.excel.export.ExcelExportService; | ||||
import cn.afterturn.easypoi.excel.export.template.ExcelExportOfTemplateUtil; | 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 导出工具类 | * excel 导出工具类 | ||||
* | |||||
* | |||||
* @author JueYue | * @author JueYue | ||||
* @version 1.0 | * @version 1.0 | ||||
* 2013-10-17 | |||||
* 2013-10-17 | |||||
*/ | */ | ||||
public final class ExcelExportUtil { | 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() { | 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, | 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 entity | ||||
* @param excelParams | * @param excelParams | ||||
* @param dataSet | |||||
* @param server 查询数据的接口 | |||||
* @param queryParams 查询数据的参数 | |||||
* @return | * @return | ||||
*/ | */ | ||||
public static Workbook exportBigExcel(ExportParams entity, List<ExcelExportEntity> excelParams, | 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, | public static Workbook exportExcel(ExportParams entity, Class<?> pojoClass, | ||||
Collection<?> dataSet) { | Collection<?> dataSet) { | ||||
Workbook workbook = getWorkbook(entity.getType(),dataSet.size()); | |||||
Workbook workbook = getWorkbook(entity.getType(), dataSet.size()); | |||||
new ExcelExportService().createSheet(workbook, entity, pojoClass, dataSet); | new ExcelExportService().createSheet(workbook, entity, pojoClass, dataSet); | ||||
return workbook; | return workbook; | ||||
} | } | ||||
@@ -117,65 +103,57 @@ public final class ExcelExportUtil { | |||||
/** | /** | ||||
* 根据Map创建对应的Excel | * 根据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, | public static Workbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList, | ||||
Collection<?> dataSet) { | Collection<?> dataSet) { | ||||
Workbook workbook = getWorkbook(entity.getType(),dataSet.size());; | |||||
Workbook workbook = getWorkbook(entity.getType(), dataSet.size()); | |||||
; | |||||
new ExcelExportService().createSheetForMap(workbook, entity, entityList, dataSet); | new ExcelExportService().createSheetForMap(workbook, entity, entityList, dataSet); | ||||
return workbook; | return workbook; | ||||
} | } | ||||
/** | /** | ||||
* 根据Map创建对应的Excel(一个excel 创建多个sheet) | * 根据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 | * @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 = getWorkbook(type,0); | |||||
Workbook workbook = getWorkbook(type, 0); | |||||
for (Map<String, Object> map : list) { | for (Map<String, Object> map : list) { | ||||
ExcelExportService service = new ExcelExportService(); | ExcelExportService service = new ExcelExportService(); | ||||
service.createSheet(workbook, (ExportParams) map.get("title"), | 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; | return workbook; | ||||
} | } | ||||
/** | /** | ||||
* 导出文件通过模板解析,不推荐这个了,推荐全部通过模板来执行处理 | * 导出文件通过模板解析,不推荐这个了,推荐全部通过模板来执行处理 | ||||
* | |||||
* @param params | |||||
* 导出参数类 | |||||
* @param pojoClass | |||||
* 对应实体 | |||||
* @param dataSet | |||||
* 实体集合 | |||||
* @param map | |||||
* 模板集合 | |||||
* | |||||
* @param params 导出参数类 | |||||
* @param pojoClass 对应实体 | |||||
* @param dataSet 实体集合 | |||||
* @param map 模板集合 | |||||
* @return | * @return | ||||
*/ | */ | ||||
@Deprecated | @Deprecated | ||||
public static Workbook exportExcel(TemplateExportParams params, Class<?> pojoClass, | public static Workbook exportExcel(TemplateExportParams params, Class<?> pojoClass, | ||||
Collection<?> dataSet, Map<String, Object> map) { | Collection<?> dataSet, Map<String, Object> map) { | ||||
return new ExcelExportOfTemplateUtil().createExcelByTemplate(params, pojoClass, dataSet, | return new ExcelExportOfTemplateUtil().createExcelByTemplate(params, pojoClass, dataSet, | ||||
map); | |||||
map); | |||||
} | } | ||||
/** | /** | ||||
* 导出文件通过模板解析只有模板,没有集合 | * 导出文件通过模板解析只有模板,没有集合 | ||||
* | |||||
* @param params | |||||
* 导出参数类 | |||||
* @param map | |||||
* 模板集合 | |||||
* | |||||
* @param params 导出参数类 | |||||
* @param map 模板集合 | |||||
* @return | * @return | ||||
*/ | */ | ||||
public static Workbook exportExcel(TemplateExportParams params, Map<String, Object> map) { | public static Workbook exportExcel(TemplateExportParams params, Map<String, Object> map) { | ||||
@@ -185,10 +163,9 @@ public final class ExcelExportUtil { | |||||
/** | /** | ||||
* 导出文件通过模板解析只有模板,没有集合 | * 导出文件通过模板解析只有模板,没有集合 | ||||
* 每个sheet对应一个map,导出到处,key是sheet的NUM | * 每个sheet对应一个map,导出到处,key是sheet的NUM | ||||
* @param params | |||||
* 导出参数类 | |||||
* @param map | |||||
* 模板集合 | |||||
* | |||||
* @param params 导出参数类 | |||||
* @param map 模板集合 | |||||
* @return | * @return | ||||
*/ | */ | ||||
public static Workbook exportExcel(Map<Integer, Map<String, Object>> map, | public static Workbook exportExcel(Map<Integer, Map<String, Object>> map, | ||||
@@ -199,14 +176,13 @@ public final class ExcelExportUtil { | |||||
/** | /** | ||||
* 导出文件通过模板解析只有模板,没有集合 | * 导出文件通过模板解析只有模板,没有集合 | ||||
* 每个sheet对应一个list,按照数量进行导出排序,key是sheet的NUM | * 每个sheet对应一个list,按照数量进行导出排序,key是sheet的NUM | ||||
* @param params | |||||
* 导出参数类 | |||||
* @param map | |||||
* 模板集合 | |||||
* | |||||
* @param params 导出参数类 | |||||
* @param map 模板集合 | |||||
* @return | * @return | ||||
*/ | */ | ||||
public static Workbook exportExcelClone(Map<Integer, List<Map<String, Object>>> map, | public static Workbook exportExcelClone(Map<Integer, List<Map<String, Object>>> map, | ||||
TemplateExportParams params) { | |||||
TemplateExportParams params) { | |||||
return new ExcelExportOfTemplateUtil().createExcelCloneByTemplate(params, map); | return new ExcelExportOfTemplateUtil().createExcelCloneByTemplate(params, map); | ||||
} | } | ||||
@@ -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 | |||||
* | |||||
* 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, | * 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 | ||||
@@ -22,6 +22,6 @@ package cn.afterturn.easypoi.excel.entity.enmus; | |||||
*/ | */ | ||||
public enum CellValueType { | 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 Integer fixedIndex; | ||||
/** | |||||
* 时区 | |||||
*/ | |||||
private String timezone; | |||||
private List<Method> methods; | private List<Method> methods; | ||||
@@ -162,5 +166,11 @@ public class ExcelBaseEntity { | |||||
this.fixedIndex = fixedIndex; | 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 interface BaseEntityTypeConstants { | ||||
public final static Integer STRING_TYPE = 1; | 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 IMAGE_TYPE = 2; | ||||
public final static Integer FUNCTION_TYPE = 3; | 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.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; | ||||
import cn.afterturn.easypoi.handler.inter.IExcelExportServer; | |||||
import cn.afterturn.easypoi.util.PoiExcelGraphDataUtil; | import cn.afterturn.easypoi.util.PoiExcelGraphDataUtil; | ||||
import cn.afterturn.easypoi.util.PoiPublicUtil; | import cn.afterturn.easypoi.util.PoiPublicUtil; | ||||
import org.apache.poi.ss.usermodel.Drawing; | 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.lang.reflect.Field; | ||||
import java.util.*; | import java.util.*; | ||||
import static cn.afterturn.easypoi.excel.ExcelExportUtil.USE_SXSSF_LIMIT; | |||||
/** | /** | ||||
* 提供批次插入服务 | * 提供批次插入服务 | ||||
* | |||||
* @author JueYue | * @author JueYue | ||||
* 2016年8月29日 | * 2016年8月29日 | ||||
*/ | */ | ||||
public class ExcelBatchExportService extends ExcelExportService { | 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) { | public void init(ExportParams entity, Class<?> pojoClass) { | ||||
List<ExcelExportEntity> excelParams = createExcelExportEntityList(entity, pojoClass); | List<ExcelExportEntity> excelParams = createExcelExportEntityList(entity, pojoClass); | ||||
@@ -49,7 +51,7 @@ public class ExcelBatchExportService extends ExcelExportService { | |||||
super.type = entity.getType(); | super.type = entity.getType(); | ||||
createSheet(workbook, entity, excelParams); | createSheet(workbook, entity, excelParams); | ||||
if (entity.getMaxNum() == 0) { | if (entity.getMaxNum() == 0) { | ||||
entity.setMaxNum(1000000); | |||||
entity.setMaxNum(USE_SXSSF_LIMIT); | |||||
} | } | ||||
insertDataToSheet(workbook, entity, excelParams, null, sheet); | insertDataToSheet(workbook, entity, excelParams, null, sheet); | ||||
} | } | ||||
@@ -61,11 +63,11 @@ public class ExcelBatchExportService extends ExcelExportService { | |||||
excelParams.add(indexExcelEntity(entity)); | 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, | getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, | ||||
null, null); | |||||
null, null); | |||||
sortAllParams(excelParams); | sortAllParams(excelParams); | ||||
return excelParams; | return excelParams; | ||||
@@ -78,7 +80,7 @@ public class ExcelBatchExportService extends ExcelExportService { | |||||
if (LOGGER.isDebugEnabled()) { | if (LOGGER.isDebugEnabled()) { | ||||
LOGGER.debug("Excel export start ,List<ExcelExportEntity> is {}", excelParams); | LOGGER.debug("Excel export start ,List<ExcelExportEntity> is {}", excelParams); | ||||
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 || excelParams == null) { | if (workbook == null || entity == null || excelParams == null) { | ||||
throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); | throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); | ||||
@@ -100,7 +102,7 @@ public class ExcelBatchExportService extends ExcelExportService { | |||||
sheet = workbook.createSheet(); | sheet = workbook.createSheet(); | ||||
index = 0; | index = 0; | ||||
} | } | ||||
Iterator<?> its = dataSet.iterator(); | Iterator<?> its = dataSet.iterator(); | ||||
while (its.hasNext()) { | while (its.hasNext()) { | ||||
Object t = its.next(); | Object t = its.next(); | ||||
@@ -127,7 +129,7 @@ public class ExcelBatchExportService extends ExcelExportService { | |||||
i18nHandler = entity.getI18nHandler(); | i18nHandler = entity.getI18nHandler(); | ||||
// 创建表格样式 | // 创建表格样式 | ||||
setExcelExportStyler((IExcelExportStyler) entity.getStyle() | setExcelExportStyler((IExcelExportStyler) entity.getStyle() | ||||
.getConstructor(Workbook.class).newInstance(workbook)); | |||||
.getConstructor(Workbook.class).newInstance(workbook)); | |||||
patriarch = PoiExcelGraphDataUtil.getDrawingPatriarch(sheet); | patriarch = PoiExcelGraphDataUtil.getDrawingPatriarch(sheet); | ||||
List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | ||||
if (entity.isAddIndex()) { | if (entity.isAddIndex()) { | ||||
@@ -136,10 +138,10 @@ public class ExcelBatchExportService extends ExcelExportService { | |||||
excelParams.addAll(entityList); | excelParams.addAll(entityList); | ||||
sortAllParams(excelParams); | sortAllParams(excelParams); | ||||
this.index = entity.isCreateHeadRows() | this.index = entity.isCreateHeadRows() | ||||
? createHeaderAndTitle(entity, sheet, workbook, excelParams) : 0; | |||||
? createHeaderAndTitle(entity, sheet, workbook, excelParams) : 0; | |||||
titleHeight = index; | titleHeight = index; | ||||
setCellWith(excelParams, sheet); | setCellWith(excelParams, sheet); | ||||
setColumnHidden(excelParams,sheet); | |||||
setColumnHidden(excelParams, sheet); | |||||
rowHeight = getRowHeight(excelParams); | rowHeight = getRowHeight(excelParams); | ||||
setCurrentIndex(1); | setCurrentIndex(1); | ||||
} catch (Exception e) { | } 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) { | if (entity.getFreezeCol() != 0) { | ||||
sheet.createFreezePane(entity.getFreezeCol(), titleHeight, entity.getFreezeCol(), titleHeight); | sheet.createFreezePane(entity.getFreezeCol(), titleHeight, entity.getFreezeCol(), titleHeight); | ||||
} | } | ||||
mergeCells(sheet, excelParams, titleHeight); | mergeCells(sheet, excelParams, titleHeight); | ||||
// 创建合计信息 | // 创建合计信息 | ||||
addStatisticsRow(getExcelExportStyler().getStyles(true, null), sheet); | 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.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; | ||||
import cn.afterturn.easypoi.handler.inter.IExcelExportServer; | |||||
import cn.afterturn.easypoi.util.PoiExcelGraphDataUtil; | import cn.afterturn.easypoi.util.PoiExcelGraphDataUtil; | ||||
import cn.afterturn.easypoi.util.PoiMergeCellUtil; | import cn.afterturn.easypoi.util.PoiMergeCellUtil; | ||||
import cn.afterturn.easypoi.util.PoiPublicUtil; | 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++) { | for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { | ||||
entity = excelParams.get(k); | entity = excelParams.get(k); | ||||
if (entity.getList() != null) { | if (entity.getList() != null) { | ||||
Collection<?> list = getListCellValue(entity, t); | |||||
Collection<?> list = getListCellValue(entity, t); | |||||
int listIndex = 0, tmpListHeight = 0; | int listIndex = 0, tmpListHeight = 0; | ||||
if (list != null && list.size() > 0) { | if (list != null && list.size() > 0) { | ||||
int tempCellNum = 0; | int tempCellNum = 0; | ||||
@@ -93,29 +94,25 @@ public abstract class BaseExportService extends ExportCommonService { | |||||
createStringCell(row, cellNum++, value == null ? "" : value.toString(), | createStringCell(row, cellNum++, value == null ? "" : value.toString(), | ||||
index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), | index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), | ||||
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) { | } else if (entity.getType() == BaseEntityTypeConstants.DOUBLE_TYPE) { | ||||
createDoubleCell(row, cellNum++, value == null ? "" : value.toString(), | createDoubleCell(row, cellNum++, value == null ? "" : value.toString(), | ||||
index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), | index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), | ||||
entity); | entity); | ||||
if (entity.isHyperlink()) { | |||||
row.getCell(cellNum - 1).setHyperlink(dataHandler.getHyperlink( | |||||
row.getSheet().getWorkbook().getCreationHelper(), t, | |||||
entity.getName(), value)); | |||||
} | |||||
} else { | } else { | ||||
createImageCell(patriarch, entity, row, cellNum++, | createImageCell(patriarch, entity, row, cellNum++, | ||||
value == null ? "" : value.toString(), t); | 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; | maxHeight += listMaxHeight - 1; | ||||
if(indexKey == 1 && excelParams.get(1).isNeedMerge()) { | |||||
if (indexKey == 1 && excelParams.get(1).isNeedMerge()) { | |||||
excelParams.get(0).setNeedMerge(true); | excelParams.get(0).setNeedMerge(true); | ||||
} | } | ||||
for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { | 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) { | 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 + "", | createStringCell(row, 0, currentIndex + "", | ||||
index % 2 == 0 ? getStyles(false, null) : getStyles(true, null), null); | index % 2 == 0 ? getStyles(false, null) : getStyles(true, null), null); | ||||
currentIndex = currentIndex + 1; | currentIndex = currentIndex + 1; | ||||
@@ -307,7 +303,7 @@ public abstract class BaseExportService extends ExportCommonService { | |||||
cell.setCellValue(text); | cell.setCellValue(text); | ||||
} | } | ||||
} | } | ||||
if (style != null) { | if (style != null) { | ||||
cell.setCellStyle(style); | cell.setCellStyle(style); | ||||
} | } | ||||
@@ -105,6 +105,9 @@ public class ExportCommonService { | |||||
} | } | ||||
if (temp != null) { | if (temp != null) { | ||||
SimpleDateFormat format = new SimpleDateFormat(entity.getFormat()); | SimpleDateFormat format = new SimpleDateFormat(entity.getFormat()); | ||||
if(StringUtils.isNotEmpty(entity.getTimezone())){ | |||||
format.setTimeZone(TimeZone.getTimeZone(entity.getTimezone())); | |||||
} | |||||
value = format.format(temp); | value = format.format(temp); | ||||
} | } | ||||
return value; | return value; | ||||
@@ -262,6 +265,7 @@ public class ExportCommonService { | |||||
excelEntity.setColumnHidden(excel.isColumnHidden()); | excelEntity.setColumnHidden(excel.isColumnHidden()); | ||||
excelEntity.setDict(excel.dict()); | excelEntity.setDict(excel.dict()); | ||||
excelEntity.setEnumExportField(excel.enumExportField()); | excelEntity.setEnumExportField(excel.enumExportField()); | ||||
excelEntity.setTimezone(excel.timezone()); | |||||
if (excelGroup != null) { | if (excelGroup != null) { | ||||
excelEntity.setGroupName(PoiPublicUtil.getValueByTargetId(excelGroup.name(), targetId, null)); | excelEntity.setGroupName(PoiPublicUtil.getValueByTargetId(excelGroup.name(), targetId, null)); | ||||
} else { | } else { | ||||
@@ -375,6 +379,7 @@ public class ExportCommonService { | |||||
ExcelExportEntity exportEntity = new ExcelExportEntity(); | ExcelExportEntity exportEntity = new ExcelExportEntity(); | ||||
//保证是第一排 | //保证是第一排 | ||||
exportEntity.setOrderNum(Integer.MIN_VALUE); | exportEntity.setOrderNum(Integer.MIN_VALUE); | ||||
exportEntity.setNeedMerge(true); | |||||
exportEntity.setName(entity.getIndexName()); | exportEntity.setName(entity.getIndexName()); | ||||
exportEntity.setWidth(10); | exportEntity.setWidth(10); | ||||
exportEntity.setFormat(PoiBaseConstants.IS_ADD_INDEX); | exportEntity.setFormat(PoiBaseConstants.IS_ADD_INDEX); | ||||
@@ -16,6 +16,7 @@ | |||||
package cn.afterturn.easypoi.excel.export.styler; | package cn.afterturn.easypoi.excel.export.styler; | ||||
import org.apache.poi.ss.usermodel.*; | import org.apache.poi.ss.usermodel.*; | ||||
import org.apache.poi.util.Removal; | |||||
/** | /** | ||||
* 带有边框的Excel样式 | * 带有边框的Excel样式 | ||||
@@ -23,6 +24,10 @@ import org.apache.poi.ss.usermodel.*; | |||||
* @author JueYue | * @author JueYue | ||||
* 2015年1月9日 下午5:55:29 | * 2015年1月9日 下午5:55:29 | ||||
*/ | */ | ||||
@Deprecated | |||||
@Removal( | |||||
version = "4.2" | |||||
) | |||||
public class ExcelExportStylerBorderImpl extends AbstractExcelExportStyler | public class ExcelExportStylerBorderImpl extends AbstractExcelExportStyler | ||||
implements IExcelExportStyler { | implements IExcelExportStyler { | ||||
@@ -16,12 +16,17 @@ | |||||
package cn.afterturn.easypoi.excel.export.styler; | package cn.afterturn.easypoi.excel.export.styler; | ||||
import org.apache.poi.ss.usermodel.*; | import org.apache.poi.ss.usermodel.*; | ||||
import org.apache.poi.util.Removal; | |||||
/** | /** | ||||
* 带有样式的导出服务 | * 带有样式的导出服务 | ||||
* @author JueYue | * @author JueYue | ||||
* 2015年1月9日 下午4:54:15 | * 2015年1月9日 下午4:54:15 | ||||
*/ | */ | ||||
@Deprecated | |||||
@Removal( | |||||
version = "4.2" | |||||
) | |||||
public class ExcelExportStylerColorImpl extends AbstractExcelExportStyler | public class ExcelExportStylerColorImpl extends AbstractExcelExportStyler | ||||
implements IExcelExportStyler { | 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.ExcelExportEntity; | ||||
import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams; | import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams; | ||||
import org.apache.poi.util.Removal; | |||||
/** | /** | ||||
* Excel导出样式接口 | * Excel导出样式接口 | ||||
@@ -42,6 +43,9 @@ public interface IExcelExportStyler { | |||||
* 获取样式方法 | * 获取样式方法 | ||||
*/ | */ | ||||
@Deprecated | @Deprecated | ||||
@Removal( | |||||
version = "4.2" | |||||
) | |||||
public CellStyle getStyles(boolean parity, ExcelExportEntity entity); | public CellStyle getStyles(boolean parity, ExcelExportEntity entity); | ||||
/** | /** | ||||
@@ -104,7 +104,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||||
sheet.getLastRowNum(), shiftRows, true, true); | sheet.getLastRowNum(), shiftRows, true, true); | ||||
mergedRegionHelper.shiftRows(sheet, teplateParams.getHeadingRows() + teplateParams.getHeadingStartRow(), shiftRows); | mergedRegionHelper.shiftRows(sheet, teplateParams.getHeadingRows() + teplateParams.getHeadingStartRow(), shiftRows); | ||||
templateSumHandler.shiftRows(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) { | if (excelParams.size() == 0) { | ||||
return; | 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); | 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); | mergedRegionHelper.shiftRows(cell.getSheet(), cell.getRowIndex() + rowspan, (datas.size() - 1) * rowspan); | ||||
templateSumHandler.shiftRows(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()) { | while (its.hasNext()) { | ||||
Object t = its.next(); | Object t = its.next(); | ||||
@@ -193,7 +193,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||||
* @param excelParams | * @param excelParams | ||||
* @throws Exception | * @throws Exception | ||||
*/ | */ | ||||
public int getOneObjectSize(Object t, List<ExcelExportEntity> excelParams) throws Exception { | |||||
private int getOneObjectSize(Object t, List<ExcelExportEntity> excelParams) throws Exception { | |||||
ExcelExportEntity entity; | ExcelExportEntity entity; | ||||
int maxHeight = 1; | int maxHeight = 1; | ||||
for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | ||||
@@ -221,7 +221,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||||
this.teplateParams = params; | this.teplateParams = params; | ||||
wb = ExcelCache.getWorkbook(teplateParams.getTemplateUrl(), teplateParams.getSheetNum(), | wb = ExcelCache.getWorkbook(teplateParams.getTemplateUrl(), teplateParams.getSheetNum(), | ||||
true); | true); | ||||
int oldSheetNum = wb.getNumberOfSheets(); | |||||
int oldSheetNum = wb.getNumberOfSheets(); | |||||
List<String> oldSheetName = new ArrayList<>(); | List<String> oldSheetName = new ArrayList<>(); | ||||
for (int i = 0; i < oldSheetNum; i++) { | for (int i = 0; i < oldSheetNum; i++) { | ||||
oldSheetName.add(wb.getSheetName(i)); | oldSheetName.add(wb.getSheetName(i)); | ||||
@@ -311,9 +311,6 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||||
} else { | } else { | ||||
wb = getCloneWorkBook(); | wb = getCloneWorkBook(); | ||||
} | } | ||||
if (wb instanceof XSSFWorkbook) { | |||||
super.type = ExcelType.XSSF; | |||||
} | |||||
// 创建表格样式 | // 创建表格样式 | ||||
setExcelExportStyler((IExcelExportStyler) teplateParams.getStyle() | setExcelExportStyler((IExcelExportStyler) teplateParams.getStyle() | ||||
.getConstructor(Workbook.class).newInstance(wb)); | .getConstructor(Workbook.class).newInstance(wb)); | ||||
@@ -382,6 +379,9 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||||
private void parseTemplate(Sheet sheet, Map<String, Object> map, | private void parseTemplate(Sheet sheet, Map<String, Object> map, | ||||
boolean colForeach) throws Exception { | boolean colForeach) throws Exception { | ||||
if (sheet.getWorkbook() instanceof XSSFWorkbook) { | |||||
super.type = ExcelType.XSSF; | |||||
} | |||||
deleteCell(sheet, map); | deleteCell(sheet, map); | ||||
mergedRegionHelper = new MergedRegionHelper(sheet); | mergedRegionHelper = new MergedRegionHelper(sheet); | ||||
templateSumHandler = new TemplateSumHandler(sheet); | templateSumHandler = new TemplateSumHandler(sheet); | ||||
@@ -410,8 +410,12 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||||
private void handlerSumCell(Sheet sheet) { | private void handlerSumCell(Sheet sheet) { | ||||
for (TemplateSumEntity sumEntity : templateSumHandler.getDataList()) { | for (TemplateSumEntity sumEntity : templateSumHandler.getDataList()) { | ||||
Cell cell = sheet.getRow(sumEntity.getRow()).getCell(sumEntity.getCol()); | 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; | return; | ||||
} | } | ||||
String oldString; | String oldString; | ||||
cell.setCellType(CellType.STRING); | |||||
oldString = cell.getStringCellValue(); | oldString = cell.getStringCellValue(); | ||||
if (oldString != null && oldString.indexOf(START_STR) != -1 | if (oldString != null && oldString.indexOf(START_STR) != -1 | ||||
&& !oldString.contains(FOREACH)) { | && !oldString.contains(FOREACH)) { | ||||
@@ -540,6 +543,11 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||||
isNumber = true; | isNumber = true; | ||||
oldString = oldString.replaceFirst(NUMBER_SYMBOL, ""); | oldString = oldString.replaceFirst(NUMBER_SYMBOL, ""); | ||||
} | } | ||||
boolean isStyleBySelf = false; | |||||
if (isStyleBySelf(oldString)) { | |||||
isStyleBySelf = true; | |||||
oldString = oldString.replaceFirst(NUMBER_SYMBOL, ""); | |||||
} | |||||
Object obj = PoiPublicUtil.getRealValue(oldString, map); | Object obj = PoiPublicUtil.getRealValue(oldString, map); | ||||
//如何是数值 类型,就按照数值类型进行设置// 如果是图片就设置为图片 | //如何是数值 类型,就按照数值类型进行设置// 如果是图片就设置为图片 | ||||
if (obj instanceof ImageEntity) { | 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()); | createImageCell(cell, img.getHeight(), img.getRowspan(), img.getColspan(), img.getUrl(), img.getData()); | ||||
} else if (isNumber && StringUtils.isNotBlank(obj.toString())) { | } else if (isNumber && StringUtils.isNotBlank(obj.toString())) { | ||||
cell.setCellValue(Double.parseDouble(obj.toString())); | cell.setCellValue(Double.parseDouble(obj.toString())); | ||||
cell.setCellType(CellType.NUMERIC); | |||||
} else { | } else { | ||||
cell.setCellValue(obj.toString()); | cell.setCellValue(obj.toString()); | ||||
} | } | ||||
@@ -570,6 +577,11 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||||
|| text.contains(" " + NUMBER_SYMBOL); | || text.contains(" " + NUMBER_SYMBOL); | ||||
} | } | ||||
private boolean isStyleBySelf(String text) { | |||||
return text.startsWith(STYLE_SELF) || text.contains("{" + STYLE_SELF) | |||||
|| text.contains(" " + STYLE_SELF); | |||||
} | |||||
/** | /** | ||||
* 创建并返回第一个Row | * 创建并返回第一个Row | ||||
* | * | ||||
@@ -620,8 +632,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||||
ExcelForEachParams params; | ExcelForEachParams params; | ||||
row = row.getSheet().getRow(row.getRowNum() - rowspan + 1); | row = row.getSheet().getRow(row.getRowNum() - rowspan + 1); | ||||
for (int k = 0; k < rowspan; k++) { | for (int k = 0; k < rowspan; k++) { | ||||
int ci = columnIndex;//cell的序号 | |||||
int ci = columnIndex; | |||||
short high = columns.get(0).getHeight(); | short high = columns.get(0).getHeight(); | ||||
int n = k; | int n = k; | ||||
while (n > 0) { | 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()); | createImageCell(row.getCell(ci), img.getHeight(), img.getRowspan(), img.getColspan(), img.getUrl(), img.getData()); | ||||
} else if (isNumber && StringUtils.isNotEmpty(val)) { | } else if (isNumber && StringUtils.isNotEmpty(val)) { | ||||
row.getCell(ci).setCellValue(Double.parseDouble(val)); | row.getCell(ci).setCellValue(Double.parseDouble(val)); | ||||
row.getCell(ci).setCellType(CellType.NUMERIC); | |||||
} else { | } else { | ||||
try { | try { | ||||
row.getCell(ci).setCellValue(val); | row.getCell(ci).setCellValue(val); | ||||
@@ -680,7 +690,9 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||||
LOGGER.error(e.getMessage(), e); | 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()) { | if (params.isNeedSum()) { | ||||
templateSumHandler.addValueOfKey(params.getName(), val); | 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.apache.poi.xssf.usermodel.XSSFWorkbook; | ||||
import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
import sun.misc.BASE64Encoder; | |||||
import java.text.SimpleDateFormat; | import java.text.SimpleDateFormat; | ||||
import java.util.*; | import java.util.*; | ||||
@@ -275,8 +274,8 @@ public class ExcelToHtmlService { | |||||
byte[] data = pictureData.getData(); | byte[] data = pictureData.getData(); | ||||
//直接输出到HTML使用BASE64Encoder | //直接输出到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); | /*String fileName = "pic" + Math.round(Math.random() * 100000000000L); | ||||
fileName += "." + PoiPublicUtil.getFileExtendName(data); | fileName += "." + PoiPublicUtil.getFileExtendName(data); | ||||
if (!imageCachePath.startsWith("/") && !imageCachePath.contains(":")) { | if (!imageCachePath.startsWith("/") && !imageCachePath.contains(":")) { | ||||
@@ -173,8 +173,12 @@ public class CellValueService { | |||||
try { | try { | ||||
return format.parse(value); | return format.parse(value); | ||||
} catch (ParseException e) { | } 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; | return null; | ||||
@@ -82,7 +82,7 @@ public class SheetHandler extends DefaultHandler { | |||||
return; | return; | ||||
} | } | ||||
if (INLINE_STR.equals(cellType)) { | if (INLINE_STR.equals(cellType)) { | ||||
type = CellValueType.String; | |||||
type = CellValueType.InlineStr; | |||||
return; | return; | ||||
} | } | ||||
if (FORMULA.equals(cellType)) { | if (FORMULA.equals(cellType)) { | ||||
@@ -126,7 +126,6 @@ public class SheetHandler extends DefaultHandler { | |||||
int idx = Integer.parseInt(lastContents); | int idx = Integer.parseInt(lastContents); | ||||
lastContents = sharedStringsTable.getItemAt(idx).getString(); | lastContents = sharedStringsTable.getItemAt(idx).getString(); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
} | } | ||||
} | } | ||||
//t元素也包含字符串 | //t元素也包含字符串 | ||||
@@ -146,7 +145,7 @@ public class SheetHandler extends DefaultHandler { | |||||
} else if (CellValueType.Number.equals(type)) { | } else if (CellValueType.Number.equals(type)) { | ||||
BigDecimal bd = new BigDecimal(value); | BigDecimal bd = new BigDecimal(value); | ||||
rowList.add(curCol, new SaxReadCellEntity(CellValueType.Number, bd)); | rowList.add(curCol, new SaxReadCellEntity(CellValueType.Number, bd)); | ||||
} else if (CellValueType.String.equals(type)) { | |||||
} else if (CellValueType.String.equals(type) || CellValueType.InlineStr.equals(type)) { | |||||
rowList.add(curCol, new SaxReadCellEntity(CellValueType.String, value)); | rowList.add(curCol, new SaxReadCellEntity(CellValueType.String, value)); | ||||
} | } | ||||
curCol++; | curCol++; | ||||
@@ -4,16 +4,18 @@ import java.util.List; | |||||
/** | /** | ||||
* 导出数据接口 | * 导出数据接口 | ||||
* | |||||
* @author JueYue | * @author JueYue | ||||
* 2016年9月8日 | * 2016年9月8日 | ||||
*/ | */ | ||||
public interface IExcelExportServer { | public interface IExcelExportServer { | ||||
/** | /** | ||||
* 查询数据接口 | * 查询数据接口 | ||||
* @param obj 查询条件 | |||||
* @param page 当前页数 | |||||
* | |||||
* @param queryParams 查询条件 | |||||
* @param page 当前页数从1开始 | |||||
* @return | * @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 END_STR = "}}"; | ||||
public static final String WRAP = "]]"; | public static final String WRAP = "]]"; | ||||
public static final String NUMBER_SYMBOL = "n:"; | 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_DATE = "fd:"; | ||||
public static final String FORMAT_NUMBER = "fn:"; | public static final String FORMAT_NUMBER = "fn:"; | ||||
public static final String SUM = "sum:"; | public static final String SUM = "sum:"; | ||||
@@ -215,7 +215,7 @@ public final class PoiMergeCellUtil { | |||||
try { | try { | ||||
sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol)); | sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol)); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
LOGGER.error("发生了一次合并单元格错误,{},{},{},{}", new Integer[]{ | |||||
LOGGER.debug("发生了一次合并单元格错误,{},{},{},{}", new Integer[]{ | |||||
firstRow, lastRow, firstCol, lastCol | firstRow, lastRow, firstCol, lastCol | ||||
}); | }); | ||||
// 忽略掉合并的错误,不打印异常 | // 忽略掉合并的错误,不打印异常 | ||||
@@ -46,32 +46,6 @@ public class ParseWord07 { | |||||
private static final Logger LOGGER = LoggerFactory.getLogger(ParseWord07.class); | 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); | Object obj = PoiPublicUtil.getRealValue(currentText, map); | ||||
if (obj instanceof ImageEntity) {// 如果是图片就设置为图片 | if (obj instanceof ImageEntity) {// 如果是图片就设置为图片 | ||||
currentRun.setText("", 0); | currentRun.setText("", 0); | ||||
addAnImage((ImageEntity) obj, currentRun); | |||||
ExcelMapParse.addAnImage((ImageEntity) obj, currentRun); | |||||
} else { | } else { | ||||
currentText = obj.toString(); | currentText = obj.toString(); | ||||
PoiPublicUtil.setWordText(currentRun, currentText); | PoiPublicUtil.setWordText(currentRun, currentText); | ||||
@@ -1,12 +1,12 @@ | |||||
/** | /** | ||||
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) | * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) | ||||
* | |||||
* <p> | |||||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
* You may obtain a copy of the License at | * You may obtain a copy of the License at | ||||
* | |||||
* <p> | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | * http://www.apache.org/licenses/LICENSE-2.0 | ||||
* | |||||
* <p> | |||||
* Unless required by applicable law or agreed to in writing, software | * 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. | ||||
@@ -15,24 +15,7 @@ | |||||
*/ | */ | ||||
package cn.afterturn.easypoi.word.parse.excel; | 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.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.ExportCommonService; | 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.exception.word.enmus.WordExportEnum; | ||||
import cn.afterturn.easypoi.util.PoiPublicUtil; | import cn.afterturn.easypoi.util.PoiPublicUtil; | ||||
import cn.afterturn.easypoi.word.entity.params.ExcelListEntity; | 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 | * @author JueYue | ||||
* 2014年8月9日 下午10:30:57 | |||||
* 2014年8月9日 下午10:30:57 | |||||
*/ | */ | ||||
public class ExcelEntityParse extends ExportCommonService { | public class ExcelEntityParse extends ExportCommonService { | ||||
@@ -64,7 +55,7 @@ public class ExcelEntityParse extends ExportCommonService { | |||||
XWPFTable table, short rowHeight) { | XWPFTable table, short rowHeight) { | ||||
try { | try { | ||||
ExcelExportEntity entity; | ExcelExportEntity entity; | ||||
XWPFTableRow row = table.insertNewTableRow(index); | |||||
XWPFTableRow row = table.insertNewTableRow(index); | |||||
if (rowHeight != -1) { | if (rowHeight != -1) { | ||||
row.setHeight(rowHeight); | row.setHeight(rowHeight); | ||||
} | } | ||||
@@ -72,8 +63,8 @@ public class ExcelEntityParse extends ExportCommonService { | |||||
for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | ||||
entity = excelParams.get(k); | entity = excelParams.get(k); | ||||
if (entity.getList() != null) { | 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) { | for (Object obj : list) { | ||||
createListCells(index + listC, cellNum, obj, entity.getList(), table, rowHeight); | createListCells(index + listC, cellNum, obj, entity.getList(), table, rowHeight); | ||||
listC++; | listC++; | ||||
@@ -109,17 +100,18 @@ public class ExcelEntityParse extends ExportCommonService { | |||||
/** | /** | ||||
* 创建List之后的各个Cells | * 创建List之后的各个Cells | ||||
* | |||||
* @param index | * @param index | ||||
* @param cellNum | * @param cellNum | ||||
* @param obj 当前对象 | |||||
* @param excelParams 列参数信息 | |||||
* @param table 当前表格 | |||||
* @param rowHeight 行高 | |||||
* @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, short rowHeight) 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(rowHeight); | row.setHeight(rowHeight); | ||||
@@ -147,7 +139,7 @@ public class ExcelEntityParse extends ExportCommonService { | |||||
throw new WordExportException(WordExportEnum.EXCEL_NO_HEAD); | throw new WordExportException(WordExportEnum.EXCEL_NO_HEAD); | ||||
} | } | ||||
Map<String, Integer> map = new HashMap<String, Integer>(); | Map<String, Integer> map = new HashMap<String, Integer>(); | ||||
String text; | |||||
String text; | |||||
for (int j = 0; j < headRows; j++) { | for (int j = 0; j < headRows; j++) { | ||||
List<XWPFTableCell> cells = table.getRow(index - j - 1).getTableCells(); | List<XWPFTableCell> cells = table.getRow(index - j - 1).getTableCells(); | ||||
for (int i = 0; i < cells.size(); i++) { | 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()); | Map<String, Integer> titlemap = getTitleMap(table, index, entity.getHeadRows()); | ||||
try { | 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) { | if (etarget != null) { | ||||
targetId = etarget.value(); | targetId = etarget.value(); | ||||
} | } | ||||
@@ -186,8 +178,8 @@ public class ExcelEntityParse extends ExportCommonService { | |||||
getAllExcelField(null, targetId, fileds, excelParams, entity.getClazz(), null, null); | getAllExcelField(null, targetId, fileds, excelParams, entity.getClazz(), null, null); | ||||
// 根据表头进行筛选排序 | // 根据表头进行筛选排序 | ||||
sortAndFilterExportField(excelParams, titlemap); | sortAndFilterExportField(excelParams, titlemap); | ||||
short rowHeight = getRowHeight(excelParams); | |||||
Iterator<?> its = entity.getList().iterator(); | |||||
short rowHeight = getRowHeight(excelParams); | |||||
Iterator<?> its = entity.getList().iterator(); | |||||
while (its.hasNext()) { | while (its.hasNext()) { | ||||
Object t = its.next(); | Object t = its.next(); | ||||
index += createCells(index, t, excelParams, table, rowHeight); | 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) { | private void setCellValue(XWPFTableRow row, Object value, int cellNum) { | ||||
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()); | ||||
setWordText(row, value); | |||||
} else { | |||||
setWordText(row, value); | |||||
} | } | ||||
setWordText(row, value); | |||||
} | } | ||||
/** | /** | ||||
* 解决word导出表格多出的换行符问题 | * 解决word导出表格多出的换行符问题 | ||||
* | |||||
* @param row | * @param row | ||||
* @param value | * @param value | ||||
*/ | */ | ||||
private void setWordText(XWPFTableRow row, Object value) { | private void setWordText(XWPFTableRow row, Object value) { | ||||
XWPFTableCell cell = row.createCell(); | |||||
XWPFTableCell cell = row.createCell(); | |||||
List<XWPFParagraph> paragraphs = cell.getParagraphs(); | 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); | paragraph = paragraphs.get(0); | ||||
} else { | } else { | ||||
paragraph = row.createCell().addParagraph(); | paragraph = row.createCell().addParagraph(); | ||||
} | } | ||||
List<XWPFRun> runs = paragraph.getRuns(); | List<XWPFRun> runs = paragraph.getRuns(); | ||||
if(runs != null && runs.size() > 0) { | |||||
if (runs != null && runs.size() > 0) { | |||||
run = runs.get(0); | run = runs.get(0); | ||||
} else { | } else { | ||||
run = paragraph.createRun(); | 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; | 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 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.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; | ||||
@@ -44,6 +48,32 @@ public final class ExcelMapParse { | |||||
private static final Logger LOGGER = LoggerFactory.getLogger(ExcelMapParse.class); | 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>(); | List<XWPFTableCell> tempCellList = new ArrayList<XWPFTableCell>(); | ||||
tempCellList.addAll(table.getRow(index).getTableCells()); | tempCellList.addAll(table.getRow(index).getTableCells()); | ||||
int templateInde = index; | |||||
int cellIndex = 0;// 创建完成对象一行好像多了一个cell | |||||
int cellIndex = 0; | |||||
Map<String, Object> tempMap = Maps.newHashMap(); | Map<String, Object> tempMap = Maps.newHashMap(); | ||||
LOGGER.debug("start for each data list :{}", list.size()); | LOGGER.debug("start for each data list :{}", list.size()); | ||||
for (Object obj : list) { | for (Object obj : list) { | ||||
@@ -103,7 +132,7 @@ public final class ExcelMapParse { | |||||
currentRow.createCell(), val); | 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.HttpServletRequest; | ||||
import javax.servlet.http.HttpServletResponse; | import javax.servlet.http.HttpServletResponse; | ||||
import java.util.Collections; | |||||
import java.util.List; | |||||
import java.util.Map; | import java.util.Map; | ||||
/** | /** | ||||
@@ -44,19 +42,9 @@ public class EasypoiBigExcelExportView extends MiniAbstractExcelView { | |||||
String codedFileName = "临时文件"; | String codedFileName = "临时文件"; | ||||
Workbook workbook = ExcelExportUtil.exportBigExcel( | Workbook workbook = ExcelExportUtil.exportBigExcel( | ||||
(ExportParams) model.get(BigExcelConstants.PARAMS), | (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)) { | if (model.containsKey(BigExcelConstants.FILE_NAME)) { | ||||
codedFileName = (String) model.get(BigExcelConstants.FILE_NAME); | codedFileName = (String) model.get(BigExcelConstants.FILE_NAME); | ||||
} | } | ||||