@@ -27,6 +27,7 @@ import org.jeecgframework.poi.excel.entity.ExportParams; | |||||
import org.jeecgframework.poi.excel.entity.TemplateExportParams; | import org.jeecgframework.poi.excel.entity.TemplateExportParams; | ||||
import org.jeecgframework.poi.excel.entity.enmus.ExcelType; | import org.jeecgframework.poi.excel.entity.enmus.ExcelType; | ||||
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | ||||
import org.jeecgframework.poi.excel.export.ExcelBatchExportServer; | |||||
import org.jeecgframework.poi.excel.export.ExcelExportServer; | import org.jeecgframework.poi.excel.export.ExcelExportServer; | ||||
import org.jeecgframework.poi.excel.export.template.ExcelExportOfTemplateUtil; | import org.jeecgframework.poi.excel.export.template.ExcelExportOfTemplateUtil; | ||||
@@ -42,6 +43,27 @@ public class ExcelExportUtil { | |||||
private ExcelExportUtil() { | private ExcelExportUtil() { | ||||
} | } | ||||
/** | |||||
* @param entity | |||||
* 表格标题属性 | |||||
* @param pojoClass | |||||
* Excel对象Class | |||||
* @param dataSet | |||||
* Excel对象数据List | |||||
*/ | |||||
public static Workbook exportBigExcel(ExportParams entity, Class<?> pojoClass, | |||||
Collection<?> dataSet) { | |||||
ExcelBatchExportServer batachServer = ExcelBatchExportServer | |||||
.getExcelBatchExportServer(entity, pojoClass); | |||||
return batachServer.appendData(dataSet); | |||||
} | |||||
public static void closeExportBigExcel() { | |||||
ExcelBatchExportServer batachServer = ExcelBatchExportServer.getExcelBatchExportServer(null, | |||||
null); | |||||
batachServer.closeExportBigExcel(); | |||||
} | |||||
/** | /** | ||||
* @param entity | * @param entity | ||||
* 表格标题属性 | * 表格标题属性 | ||||
@@ -0,0 +1,153 @@ | |||||
package org.jeecgframework.poi.excel.export; | |||||
import java.lang.reflect.Field; | |||||
import java.util.ArrayList; | |||||
import java.util.Arrays; | |||||
import java.util.Collection; | |||||
import java.util.Iterator; | |||||
import java.util.List; | |||||
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 org.jeecgframework.poi.excel.annotation.ExcelTarget; | |||||
import org.jeecgframework.poi.excel.entity.ExportParams; | |||||
import org.jeecgframework.poi.excel.entity.enmus.ExcelType; | |||||
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | |||||
import org.jeecgframework.poi.excel.export.styler.IExcelExportStyler; | |||||
import org.jeecgframework.poi.exception.excel.ExcelExportException; | |||||
import org.jeecgframework.poi.exception.excel.enums.ExcelExportEnum; | |||||
import org.jeecgframework.poi.util.PoiPublicUtil; | |||||
/** | |||||
* 提供批次插入服务 | |||||
* @author JueYue | |||||
* @date 2016年8月29日 | |||||
*/ | |||||
public class ExcelBatchExportServer extends ExcelExportServer { | |||||
private static ThreadLocal<ExcelBatchExportServer> THREAD_LOCAL = new ThreadLocal<ExcelBatchExportServer>(); | |||||
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) { | |||||
LOGGER.debug("ExcelBatchExportServer only support SXSSFWorkbook"); | |||||
entity.setType(ExcelType.XSSF); | |||||
workbook = new SXSSFWorkbook(); | |||||
this.entity = entity; | |||||
createSheet(workbook, entity, pojoClass); | |||||
if (entity.getMaxNum() == 0) { | |||||
entity.setMaxNum(1000000); | |||||
} | |||||
insertDataToSheet(workbook, entity, excelParams, null, sheet); | |||||
} | |||||
public void createSheet(Workbook workbook, ExportParams entity, Class<?> pojoClass) { | |||||
if (LOGGER.isDebugEnabled()) { | |||||
LOGGER.debug("Excel export start ,class is {}", pojoClass); | |||||
LOGGER.debug("Excel version is {}", | |||||
entity.getType().equals(ExcelType.HSSF) ? "03" : "07"); | |||||
} | |||||
if (workbook == null || entity == null || pojoClass == null) { | |||||
throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); | |||||
} | |||||
try { | |||||
excelParams = new ArrayList<ExcelExportEntity>(); | |||||
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); | |||||
try { | |||||
sheet = workbook.createSheet(entity.getSheetName()); | |||||
} catch (Exception e) { | |||||
// 重复遍历,出现了重名现象,创建非指定的名称Sheet | |||||
sheet = workbook.createSheet(); | |||||
} | |||||
} catch (Exception e) { | |||||
LOGGER.error(e.getMessage(), e); | |||||
throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause()); | |||||
} | |||||
} | |||||
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; | |||||
} | |||||
protected void insertDataToSheet(Workbook workbook, ExportParams entity, | |||||
List<ExcelExportEntity> entityList, Collection<?> dataSet, | |||||
Sheet sheet) { | |||||
try { | |||||
dataHanlder = entity.getDataHanlder(); | |||||
if (dataHanlder != null && dataHanlder.getNeedHandlerFields() != null) { | |||||
needHanlderList = Arrays.asList(dataHanlder.getNeedHandlerFields()); | |||||
} | |||||
// 创建表格样式 | |||||
setExcelExportStyler((IExcelExportStyler) entity.getStyle() | |||||
.getConstructor(Workbook.class).newInstance(workbook)); | |||||
patriarch = sheet.createDrawingPatriarch(); | |||||
List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | |||||
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 ExcelBatchExportServer getExcelBatchExportServer(ExportParams entity, | |||||
Class<?> pojoClass) { | |||||
if (THREAD_LOCAL.get() == null) { | |||||
ExcelBatchExportServer batchServer = new ExcelBatchExportServer(); | |||||
batchServer.init(entity, pojoClass); | |||||
THREAD_LOCAL.set(batchServer); | |||||
} | |||||
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); | |||||
} | |||||
} |
@@ -38,8 +38,6 @@ import org.jeecgframework.poi.excel.export.styler.IExcelExportStyler; | |||||
import org.jeecgframework.poi.exception.excel.ExcelExportException; | import org.jeecgframework.poi.exception.excel.ExcelExportException; | ||||
import org.jeecgframework.poi.exception.excel.enums.ExcelExportEnum; | import org.jeecgframework.poi.exception.excel.enums.ExcelExportEnum; | ||||
import org.jeecgframework.poi.util.PoiPublicUtil; | import org.jeecgframework.poi.util.PoiPublicUtil; | ||||
import org.slf4j.Logger; | |||||
import org.slf4j.LoggerFactory; | |||||
/** | /** | ||||
* Excel导出服务 | * Excel导出服务 | ||||
@@ -49,12 +47,10 @@ import org.slf4j.LoggerFactory; | |||||
*/ | */ | ||||
public class ExcelExportServer extends ExcelExportBase { | public class ExcelExportServer extends ExcelExportBase { | ||||
private final static Logger LOGGER = LoggerFactory.getLogger(ExcelExportServer.class); | |||||
// 最大行数,超过自动多Sheet | // 最大行数,超过自动多Sheet | ||||
private int MAX_NUM = 60000; | private int MAX_NUM = 60000; | ||||
private int createHeaderAndTitle(ExportParams entity, Sheet sheet, Workbook workbook, | |||||
protected int createHeaderAndTitle(ExportParams entity, Sheet sheet, Workbook workbook, | |||||
List<ExcelExportEntity> excelParams) { | List<ExcelExportEntity> excelParams) { | ||||
int rows = 0, feildWidth = getFieldLength(excelParams); | int rows = 0, feildWidth = getFieldLength(excelParams); | ||||
if (entity.getTitle() != null) { | if (entity.getTitle() != null) { | ||||
@@ -156,7 +152,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||||
insertDataToSheet(workbook, entity, entityList, dataSet,sheet); | insertDataToSheet(workbook, entity, entityList, dataSet,sheet); | ||||
} | } | ||||
private void insertDataToSheet(Workbook workbook, ExportParams entity, | |||||
protected void insertDataToSheet(Workbook workbook, ExportParams entity, | |||||
List<ExcelExportEntity> entityList, Collection<?> dataSet, | List<ExcelExportEntity> entityList, Collection<?> dataSet, | ||||
Sheet sheet) { | Sheet sheet) { | ||||
try { | try { | ||||
@@ -44,8 +44,6 @@ import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | |||||
import org.jeecgframework.poi.excel.export.styler.IExcelExportStyler; | import org.jeecgframework.poi.excel.export.styler.IExcelExportStyler; | ||||
import org.jeecgframework.poi.util.PoiMergeCellUtil; | import org.jeecgframework.poi.util.PoiMergeCellUtil; | ||||
import org.jeecgframework.poi.util.PoiPublicUtil; | import org.jeecgframework.poi.util.PoiPublicUtil; | ||||
import org.slf4j.Logger; | |||||
import org.slf4j.LoggerFactory; | |||||
/** | /** | ||||
* 提供POI基础操作服务 | * 提供POI基础操作服务 | ||||
@@ -56,9 +54,6 @@ import org.slf4j.LoggerFactory; | |||||
@SuppressWarnings("unchecked") | @SuppressWarnings("unchecked") | ||||
public abstract class ExcelExportBase extends ExportBase { | public abstract class ExcelExportBase extends ExportBase { | ||||
private static final Logger LOGGER = LoggerFactory | |||||
.getLogger(ExcelExportBase.class); | |||||
private int currentIndex = 0; | private int currentIndex = 0; | ||||
protected ExcelType type = ExcelType.HSSF; | protected ExcelType type = ExcelType.HSSF; | ||||
@@ -37,6 +37,8 @@ import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | |||||
import org.jeecgframework.poi.handler.inter.IExcelDataHandler; | import org.jeecgframework.poi.handler.inter.IExcelDataHandler; | ||||
import org.jeecgframework.poi.util.PoiPublicUtil; | import org.jeecgframework.poi.util.PoiPublicUtil; | ||||
import org.jeecgframework.poi.util.PoiReflectorUtil; | import org.jeecgframework.poi.util.PoiReflectorUtil; | ||||
import org.slf4j.Logger; | |||||
import org.slf4j.LoggerFactory; | |||||
/** | /** | ||||
* 导出基础处理,不设计POI,只设计对象,保证复用性 | * 导出基础处理,不设计POI,只设计对象,保证复用性 | ||||
@@ -46,6 +48,9 @@ import org.jeecgframework.poi.util.PoiReflectorUtil; | |||||
*/ | */ | ||||
@SuppressWarnings("rawtypes") | @SuppressWarnings("rawtypes") | ||||
public class ExportBase { | public class ExportBase { | ||||
protected static final Logger LOGGER = LoggerFactory | |||||
.getLogger(ExportBase.class); | |||||
protected IExcelDataHandler dataHanlder; | protected IExcelDataHandler dataHanlder; | ||||