diff --git a/easypoi-base/ExcelBatchExportService.java b/easypoi-base/ExcelBatchExportService.java new file mode 100644 index 0000000..9f9d784 --- /dev/null +++ b/easypoi-base/ExcelBatchExportService.java @@ -0,0 +1,183 @@ +package cn.afterturn.easypoi.excel.export; + +import cn.afterturn.easypoi.excel.annotation.ExcelTarget; +import cn.afterturn.easypoi.excel.entity.ExportParams; +import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; +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.util.PoiExcelGraphDataUtil; +import cn.afterturn.easypoi.util.PoiPublicUtil; +import org.apache.poi.ss.usermodel.Drawing; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; + +import java.lang.reflect.Field; +import java.util.*; + +/** + * 提供批次插入服务 + * @author JueYue + * 2016年8月29日 + */ +public class ExcelBatchExportService extends ExcelExportService { + + private static ThreadLocal THREAD_LOCAL = new ThreadLocal(); + + private Workbook workbook; + private Sheet sheet; + private List excelParams; + private ExportParams entity; + private int titleHeight; + private Drawing patriarch; + private short rowHeight; + private int index; + + public void init(ExportParams entity, Class pojoClass) { + List excelParams = createExcelExportEntityList(entity, pojoClass); + init(entity, excelParams); + } + + public void init(ExportParams entity, List excelParams) { + LOGGER.debug("ExcelBatchExportServer only support SXSSFWorkbook"); + entity.setType(ExcelType.XSSF); + workbook = new SXSSFWorkbook(); + this.entity = entity; + this.excelParams = excelParams; + super.type = entity.getType(); + createSheet(workbook, entity, excelParams); + if (entity.getMaxNum() == 0) { + entity.setMaxNum(1000000); + } + insertDataToSheet(workbook, entity, excelParams, null, sheet); + } + + public List createExcelExportEntityList(ExportParams entity, Class pojoClass) { + try { + List excelParams = new ArrayList(); + if (entity.isAddIndex()) { + excelParams.add(indexExcelEntity(entity)); + } + // 得到所有字段 + 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); + sortAllParams(excelParams); + + return excelParams; + } catch (Exception e) { + throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e); + } + } + + public void createSheet(Workbook workbook, ExportParams entity, List excelParams) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Excel export start ,List is {}", excelParams); + LOGGER.debug("Excel version is {}", + entity.getType().equals(ExcelType.HSSF) ? "03" : "07"); + } + if (workbook == null || entity == null || excelParams == null) { + throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); + } + try { + try { + sheet = workbook.createSheet(entity.getSheetName()); + } catch (Exception e) { + // 重复遍历,出现了重名现象,创建非指定的名称Sheet + sheet = workbook.createSheet(); + } + } catch (Exception e) { + throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e); + } + } + + public Workbook appendData(Collection dataSet) { + if (sheet.getLastRowNum() + dataSet.size() > entity.getMaxNum()) { + sheet = workbook.createSheet(); + index = 0; + } + + Iterator its = dataSet.iterator(); + while (its.hasNext()) { + Object t = its.next(); + try { + index += createCells(patriarch, index, t, excelParams, sheet, workbook, rowHeight); + } catch (Exception e) { + LOGGER.error(e.getMessage(), e); + throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e); + } + } + return workbook; + } + + @Override + protected void insertDataToSheet(Workbook workbook, ExportParams entity, + List entityList, Collection dataSet, + Sheet sheet) { + try { + dataHandler = entity.getDataHandler(); + if (dataHandler != null && dataHandler.getNeedHandlerFields() != null) { + needHandlerList = Arrays.asList(dataHandler.getNeedHandlerFields()); + } + // 创建表格样式 + setExcelExportStyler((IExcelExportStyler) entity.getStyle() + .getConstructor(Workbook.class).newInstance(workbook)); + patriarch = PoiExcelGraphDataUtil.getDrawingPatriarch(sheet); + List excelParams = new ArrayList(); + if (entity.isAddIndex()) { + excelParams.add(indexExcelEntity(entity)); + } + excelParams.addAll(entityList); + sortAllParams(excelParams); + this.index = entity.isCreateHeadRows() + ? createHeaderAndTitle(entity, sheet, workbook, excelParams) : 0; + titleHeight = index; + setCellWith(excelParams, sheet); + rowHeight = getRowHeight(excelParams); + setCurrentIndex(1); + } catch (Exception e) { + LOGGER.error(e.getMessage(), e); + throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause()); + } + } + + 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 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() { + if (entity.getFreezeCol() != 0) { + sheet.createFreezePane(entity.getFreezeCol(), 0, entity.getFreezeCol(), 0); + } + mergeCells(sheet, excelParams, titleHeight); + // 创建合计信息 + addStatisticsRow(getExcelExportStyler().getStyles(true, null), sheet); + THREAD_LOCAL.remove(); + + } + +} diff --git a/easypoi-base/ExcelExportUtil.java b/easypoi-base/ExcelExportUtil.java new file mode 100644 index 0000000..a840da8 --- /dev/null +++ b/easypoi-base/ExcelExportUtil.java @@ -0,0 +1,182 @@ +/** + * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package cn.afterturn.easypoi.excel; + +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; +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; + +/** + * excel 导出工具类 + * + * @author JueYue + * @version 1.0 + * 2013-10-17 + */ +public class ExcelExportUtil { + + private ExcelExportUtil() { + } + + /** + * @param entity + * 表格标题属性 + * @param pojoClass + * Excel对象Class + * @param dataSet + * Excel对象数据List + */ + public static Workbook exportBigExcel(ExportParams entity, Class pojoClass, + Collection dataSet) { + ExcelBatchExportService batchService = ExcelBatchExportService + .getExcelBatchExportService(entity, pojoClass); + return batchService.appendData(dataSet); + } + + public static Workbook exportBigExcel(ExportParams entity, List excelParams, + Collection dataSet) { + ExcelBatchExportService batchService = ExcelBatchExportService + .getExcelBatchExportService(entity, excelParams); + return batchService.appendData(dataSet); + } + + public static void closeExportBigExcel() { + ExcelBatchExportService batchService = ExcelBatchExportService.getCurrentExcelBatchExportService(); + if(batchService != null) { + batchService.closeExportBigExcel(); + } + } + + /** + * @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()); + new ExcelExportService().createSheet(workbook, entity, pojoClass, dataSet); + return workbook; + } + + private static Workbook getWorkbook(ExcelType type, int size) { + if (ExcelType.HSSF.equals(type)) { + return new HSSFWorkbook(); + } else if (size < 100000) { + return new XSSFWorkbook(); + } else { + return new SXSSFWorkbook(); + } + } + + /** + * 根据Map创建对应的Excel + * @param entity + * 表格标题属性 + * @param entityList + * Map对象列表 + * @param dataSet + * Excel对象数据List + */ + public static Workbook exportExcel(ExportParams entity, List entityList, + Collection dataSet) { + Workbook workbook = getWorkbook(entity.getType(),dataSet.size());; + new ExcelExportService().createSheetForMap(workbook, entity, entityList, dataSet); + return workbook; + } + + /** + * 一个excel 创建多个sheet + * + * @param list + * 多个Map key title 对应表格Title key entity 对应表格对应实体 key data + * Collection 数据 + * @return + */ + public static Workbook exportExcel(List> list, ExcelType type) { + Workbook workbook = getWorkbook(type,0); + for (Map map : list) { + ExcelExportService service = new ExcelExportService(); + service.createSheet(workbook, (ExportParams) map.get("title"), + (Class) map.get("entity"), (Collection) map.get("data")); + } + return workbook; + } + + /** + * 导出文件通过模板解析,不推荐这个了,推荐全部通过模板来执行处理 + * + * @param params + * 导出参数类 + * @param pojoClass + * 对应实体 + * @param dataSet + * 实体集合 + * @param map + * 模板集合 + * @return + */ + @Deprecated + public static Workbook exportExcel(TemplateExportParams params, Class pojoClass, + Collection dataSet, Map map) { + return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, pojoClass, dataSet, + map); + } + + /** + * 导出文件通过模板解析只有模板,没有集合 + * + * @param params + * 导出参数类 + * @param map + * 模板集合 + * @return + */ + public static Workbook exportExcel(TemplateExportParams params, Map map) { + return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, null, null, map); + } + + /** + * 导出文件通过模板解析只有模板,没有集合 + * 每个sheet对应一个map,导出到处,key是sheet的NUM + * @param params + * 导出参数类 + * @param map + * 模板集合 + * @return + */ + public static Workbook exportExcel(Map> map, + TemplateExportParams params) { + return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, map); + } + +}