cn.afterturn.easypoi.excel.export.ExcelBatchExportService Maven / Gradle / Ivy
The newest version!
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.handler.inter.IExcelExportServer;
import cn.afterturn.easypoi.handler.inter.IWriter;
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.*;
import static cn.afterturn.easypoi.excel.ExcelExportUtil.USE_SXSSF_LIMIT;
/**
* 提供批次插入服务
*
* @author JueYue
* 2016年8月29日
*/
public class ExcelBatchExportService extends ExcelExportService implements IWriter {
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(USE_SXSSF_LIMIT);
}
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));
}
i18nHandler = entity.getI18nHandler();
// 得到所有字段
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);
}
}
@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());
}
dictHandler = entity.getDictHandler();
// 创建表格样式
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);
setColumnHidden(excelParams, sheet);
rowHeight = getRowHeight(excelParams);
setCurrentIndex(1);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e);
}
}
public Workbook exportBigExcel(IExcelExportServer server, Object queryParams) {
int page = 1;
List