package cn.inspower.elesituation.handler;
import cn.inspower.dto.MarkListDTO;
import cn.inspower.monitor.data.dto.HistoryDataDTO;
import cn.inspower.util.ListUtil;
import framework.common.exception.FrameworkException;
import framework.common.tools.map.ExtHashMap;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
public abstract class AbstractExcelExporter {
protected Workbook workbook;
protected Sheet sheet;
protected String title;
protected Integer dataStartRowNum;
protected List<String> dataIndexs;
protected List<Map> data;
protected Integer columnWidth = 15;
protected Integer rowHeight = 15;
protected Integer titleRowHeight = 20;
protected Short titleFontSize = 16;
protected Short bodyFontSize = 11;
protected String fileName;
public AbstractExcelExporter() {
this.dataIndexs = new ArrayList<>();
this.data = new ArrayList<>();
this.workbook = new XSSFWorkbook();
this.sheet = workbook.createSheet("数据报表");
}
protected void init(List<HistoryDataDTO> dataList, Date startTime, Date endTime, Integer dataStartRowNum) throws FrameworkException {
this.dataStartRowNum = dataStartRowNum;
this.processingData(dataList, startTime, endTime).writeTitle().writeTableHead().writeMainData();
}
protected abstract AbstractExcelExporter processingData(List<HistoryDataDTO> dataList, Date startTime, Date endTime) throws FrameworkException;
protected abstract AbstractExcelExporter writeTableHead();
protected abstract AbstractExcelExporter writeMainData();
public AbstractExcelExporter export(HttpServletResponse response) throws FrameworkException {
OutputStream out = null;
try {
response.setHeader("Content-Disposition", "attachment;Filename=" + URLEncoder.encode(fileName, "utf-8") + ".xlsx");
response.setHeader("FileName", URLEncoder.encode(fileName, "utf-8") + ".xlsx");
response.setHeader("Access-Control-Expose-Headers", "FileName");
response.setHeader("Transfer-Encoding", "utf-8");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
} catch (Exception ex) {
ex.printStackTrace();
throw new FrameworkException("导出Excel异常,异常信息:" + ex.getMessage());
} finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return this;
}
public void saveFile(String filePath) throws FrameworkException {
OutputStream out = null;
try {
File file = new File(filePath + fileName + ".xlsx");
out = new FileOutputStream(file);
workbook.write(out);
out.flush();
out.close();
} catch (Exception ex) {
ex.printStackTrace();
throw new FrameworkException("保存Excel异常,异常信息:" + ex.getMessage());
} finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
protected AbstractExcelExporter writeTitle() throws FrameworkException {
checkConfig();
this.sheet.setDefaultColumnWidth(this.columnWidth);
this.sheet.setDefaultRowHeight(Short.valueOf(rowHeight * 20 + ""));
Row title = this.sheet.createRow(0);
title.setHeightInPoints(titleRowHeight);
CellStyle cellStyle = getTitleStyle();
createTextCell(title, 0, this.title, cellStyle);
mergeCell(sheet, 0, 0, 0, this.dataIndexs.size() - 1);
return this;
}
protected Cell createTextCell(Row row, int index, Object value, CellStyle style) {
Cell cell = row.createCell(index);
if (style != null) {
cell.setCellStyle(style);
}
XSSFRichTextString cellValue = new XSSFRichTextString(value == null ? "-" : value.toString());
cell.setCellValue(cellValue);
return cell;
}
protected Cell createNumberCell(Row row, int index, double value,
CellStyle style, boolean haveDecimal) {
Cell cell = row.createCell(index);
if (style == null) {
style = this.workbook.createCellStyle();
}
if (haveDecimal) {
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
}
cell.setCellStyle(style);
String str = new DecimalFormat("0.00").format(value);
cell.setCellValue(value);
return cell;
}
protected void checkConfig() throws FrameworkException {
if (ListUtil.isEmpty(dataIndexs)) {
throw new FrameworkException("", "列名数组不能为空或者为NULL");
}
if (ListUtil.isEmpty(data)) {
throw new FrameworkException("", "数据为空");
}
}
protected CellStyle getTitleStyle() {
CellStyle cellStyle = workbook.createCellStyle();
center(cellStyle);
Font font = setFontSize(titleFontSize);
font.setBold(true);
cellStyle.setFont(font);
return cellStyle;
}
protected CellStyle getDefaultHeaderStyle() {
return getHeaderStyle(null);
}
protected CellStyle getHeaderStyle(java.awt.Color color) {
CellStyle cellStyle = workbook.createCellStyle();
center(cellStyle);
Font font = setFontSize(bodyFontSize);
font.setBold(true);
if (color != null) {
((XSSFCellStyle) cellStyle).setFillForegroundColor(new XSSFColor(color));
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}
cellStyle.setFont(font);
return cellStyle;
}
protected void center(CellStyle cellStyle) {
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
}
protected void mergeCell(Sheet wbSheet, int firstRow, int lastRow, int firstCol, int lastCol) {
wbSheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol));
}
protected CellStyle getBodyDefaultStyle() {
return getBodyStyle(null);
}
protected CellStyle getBodyStyle(java.awt.Color color) {
CellStyle cellStyle = workbook.createCellStyle();
center(cellStyle);
Font font = setFontSize(bodyFontSize);
cellStyle.setFont(font);
if (color != null) {
((XSSFCellStyle) cellStyle).setFillForegroundColor(new XSSFColor(color));
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}
return cellStyle;
}
protected Font setFontSize(int fontSize) {
Font font = workbook.createFont();
font.setFontName("等线");
font.setFontHeightInPoints((short) fontSize);
return font;
}
}