com.zhongweixian.excel.export.base.ExcelExportServer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of poi-api Show documentation
Show all versions of poi-api Show documentation
poi-api project for Spring Boot
The newest version!
package com.zhongweixian.excel.export.base;
import com.zhongweixian.excel.exception.ExcelExportEnum;
import com.zhongweixian.excel.exception.ExcelExportException;
import com.zhongweixian.excel.entity.ExportParams;
import com.zhongweixian.excel.entity.params.ExcelExportEntity;
import com.zhongweixian.excel.util.PoiExcelGraphDataUtil;
import com.zhongweixian.excel.util.PoiPublicUtil;
import com.zhongweixian.excel.annotation.ExcelTarget;
import com.zhongweixian.excel.entity.enmus.ExcelType;
import com.zhongweixian.excel.export.style.IExcelExportStyler;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.util.*;
/**
* @author [email protected]
* @Date 2017/11/5:23:34
*/
public class ExcelExportServer extends BaseExportServer {
Logger logger = LoggerFactory.getLogger(ExcelExportServer.class);
// 最大行数,超过自动多Sheet
private static int MAX_NUM = 60000;
protected int createHeaderAndTitle(ExportParams entity, Sheet sheet, Workbook workbook,
List excelParams) {
int rows = 0, fieldLength = getFieldLength(excelParams);
if (entity.getTitle() != null) {
rows += createHeaderRow(entity, sheet, workbook, fieldLength);
}
rows += createTitleRow(entity, sheet, workbook, rows, excelParams);
sheet.createFreezePane(0, rows, 0, rows);
return rows;
}
/**
* 创建 表头改变
*/
public int createHeaderRow(ExportParams entity, Sheet sheet, Workbook workbook,
int fieldWidth) {
Row row = sheet.createRow(0);
row.setHeight(entity.getTitleHeight());
createStringCell(row, 0, entity.getTitle(),
getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
for (int i = 1; i <= fieldWidth; i++) {
createStringCell(row, i, "",
getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
}
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, fieldWidth));
if (entity.getSecondTitle() != null) {
row = sheet.createRow(1);
row.setHeight(entity.getSecondTitleHeight());
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.RIGHT);
createStringCell(row, 0, entity.getSecondTitle(), style, null);
for (int i = 1; i <= fieldWidth; i++) {
createStringCell(row, i, "",
getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
}
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, fieldWidth));
return 2;
}
return 1;
}
public void createSheet(Workbook workbook, ExportParams entity, Class> pojoClass,
Collection> dataSet) {
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 || dataSet == null) {
throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR);
}
try {
List excelParams = new ArrayList();
// 得到所有字段
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);
//获取所有参数后,后面的逻辑判断就一致了
createSheetForMap(workbook, entity, excelParams, dataSet);
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause());
}
}
public void createSheetForMap(Workbook workbook, ExportParams entity,
List entityList, Collection> dataSet) {
if (logger.isDebugEnabled()) {
logger.debug("Excel version is {}",
entity.getType().equals(ExcelType.HSSF) ? "03" : "07");
}
if (workbook == null || entity == null || entityList == null || dataSet == null) {
throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR);
}
super.type = entity.getType();
if (type.equals(ExcelType.XSSF)) {
MAX_NUM = 1000000;
}
if (entity.getMaxNum() > 0) {
MAX_NUM = entity.getMaxNum();
}
Sheet sheet = null;
try {
sheet = workbook.createSheet(entity.getSheetName());
} catch (Exception e) {
// 重复遍历,出现了重名现象,创建非指定的名称Sheet
sheet = workbook.createSheet();
}
insertDataToSheet(workbook, entity, entityList, dataSet, sheet);
}
protected void insertDataToSheet(Workbook workbook, ExportParams entity,
List 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));
Drawing patriarch = PoiExcelGraphDataUtil.getDrawingPatriarch(sheet);
List excelParams = new ArrayList();
if (entity.isAddIndex()) {
excelParams.add(indexExcelEntity(entity));
}
excelParams.addAll(entityList);
sortAllParams(excelParams);
int index = entity.isCreateHeadRows()
? createHeaderAndTitle(entity, sheet, workbook, excelParams) : 0;
int titleHeight = index;
setCellWith(excelParams, sheet);
short rowHeight = entity.getHeight() > 0 ? entity.getHeight() : getRowHeight(excelParams);
setCurrentIndex(1);
Iterator> its = dataSet.iterator();
List