ExcelFormatUtil样式工具类
package com.ymf.invoice.common.utils;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.util.List;
public class ExcelFormatUtil {
public static CellStyle headSytle(SXSSFWorkbook workbook) {
CellStyle style1 = workbook.createCellStyle();
style1.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style1.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.index);
style1.setBorderBottom(BorderStyle.THIN);
style1.setBorderLeft(BorderStyle.THIN);
style1.setBorderRight(BorderStyle.THIN);
style1.setBorderTop(BorderStyle.THIN);
Font font1 = workbook.createFont();
font1.setFontName("Arial");
font1.setFontHeightInPoints((short) 16);
font1.setBold(true);
font1.setColor(IndexedColors.WHITE.index);
style1.setFont(font1);
style1.setWrapText(true);
style1.setAlignment(HorizontalAlignment.CENTER);
style1.setVerticalAlignment(VerticalAlignment.CENTER);
return style1;
}
public static CellStyle contentStyle(SXSSFWorkbook wb) {
CellStyle style1 = wb.createCellStyle();
style1.setBorderBottom(BorderStyle.THIN);
style1.setBorderLeft(BorderStyle.THIN);
style1.setBorderRight(BorderStyle.THIN);
style1.setBorderTop(BorderStyle.THIN);
style1.setWrapText(true);
style1.setAlignment(HorizontalAlignment.CENTER);
style1.setVerticalAlignment(VerticalAlignment.CENTER);
style1.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style1.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style1.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style1.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
Font dataFont = wb.createFont();
dataFont.setFontName("Arial");
dataFont.setFontHeightInPoints((short) 10);
style1.setFont(dataFont);
return style1;
}
public static CellStyle titleSytle(SXSSFWorkbook workbook, short color, short fontSize) {
CellStyle style1 = workbook.createCellStyle();
if (color != HSSFColor.HSSFColorPredefined.WHITE.getIndex()) {
style1.setFillForegroundColor(color);
}
style1.setBorderBottom(BorderStyle.THIN);
style1.setBorderLeft(BorderStyle.THIN);
style1.setBorderRight(BorderStyle.THIN);
style1.setBorderTop(BorderStyle.THIN);
Font font1 = workbook.createFont();
font1.setFontName("Arial");
font1.setFontHeightInPoints(fontSize);
font1.setBold(true);
style1.setFont(font1);
style1.setWrapText(true);
style1.setAlignment(HorizontalAlignment.CENTER);
style1.setVerticalAlignment(VerticalAlignment.CENTER);
return style1;
}
public static void initTitleEX(SXSSFSheet sheet, CellStyle header, List<String> title, List<Integer> titleLength) {
SXSSFRow row0 = sheet.createRow(1);
row0.setHeight((short) 800);
for (int j = 0; j < title.size(); j++) {
SXSSFCell cell = row0.createCell(j);
cell.setCellValue(title.get(j));
cell.setCellStyle(header);
sheet.setColumnWidth(j, titleLength.get(j));
}
}
}
Controller
@PostMapping("/export")
@ApiOperation("导出发票")
private void export(HttpServletResponse response, InvoiceImgDto invoiceImgDto) {
invoiceImgService.export(response, invoiceImgDto);
}
service实现类
private SXSSFWorkbook wb;
private SXSSFSheet sheet;
private CellStyle header;
private CellStyle content;
private CellStyle title;
private List<String> strs;
public void export(HttpServletResponse response, InvoiceImgDto invoiceImgDto) {
List<InvoiceImgExcel> excelList = getInvoiceImgExcels(invoiceImgDto);
exportExcel(response, excelList, "发票签收递送清单", "发票签收递送清单");
}
public void createWorkbook(String sheetName) {
this.wb = new SXSSFWorkbook(1000);
this.sheet = this.wb.createSheet();
this.wb.setSheetName(0, sheetName);
this.title = ExcelFormatUtil.titleSytle(this.wb, HSSFColor.HSSFColorPredefined.WHITE.getIndex(), (short) 17);
this.header = ExcelFormatUtil.headSytle(this.wb);
this.content = ExcelFormatUtil.contentStyle(this.wb);
}
public void createTitle(String sheetTitle) {
SXSSFRow titleRow = this.sheet.createRow(0);
titleRow.setHeightInPoints(30);
SXSSFCell titleCell = titleRow.createCell(0);
titleCell.setCellStyle(this.title);
titleCell.setCellValue(sheetTitle);
this.sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(), 6));
}
private void createHeader() {
strs = Arrays.asList("序号", "供应商名称", "发票号码", "金额(元)", "开票日期", "签收日期", "备注");
List<Integer> ints = Arrays.asList(5000, 5000, 5000, 5000, 5000, 5000, 5000);
ExcelFormatUtil.initTitleEX(this.sheet, this.header, strs, ints);
}
private void exportExcel(HttpServletResponse response, List<InvoiceImgExcel> list, String sheetName, String sheetTitle) {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
log.info(">>>>>>>>>>>>>>>>>>>>开始>>>>>>>>>>");
createWorkbook(sheetName);
createTitle(sheetTitle);
createHeader();
log.info(">>>>>>>>>>>>>>>>>>>>表头样式设置完成>>>>>>>>>>");
handleBusiness(list);
try {
this.wb.write(response.getOutputStream());
} catch (Exception e) {
log.error("导出Excel异常{}", e.getMessage());
} finally {
IOUtils.closeQuietly(wb);
}
}
private void handleBusiness(List<InvoiceImgExcel> list) {
if (list != null && list.size() > 0) {
int constant = 0;
log.info(">>>>>>>>>>>>>>>>>>>>开始遍历数据组装单元格内容>>>>>>>>>>");
for (int i = 0; i < list.size(); i++) {
constant = getConstant(list, constant, i);
}
}
int rowCode = list.size() + 2;
SXSSFRow row = sheet.createRow(rowCode);
SXSSFCell cell = row.createCell(0);
cell.setCellValue("合计");
cell.setCellStyle(header);
this.sheet.addMergedRegion(new CellRangeAddress(rowCode, rowCode, 0, 1));
for (int i = 1; i < strs.size(); i++) {
cell = row.createCell(i);
cell.setCellStyle(header);
}
cell = row.getCell(2);
cell.setCellValue("发票" + list.size() + "张");
cell = row.getCell(3);
BigDecimal reduce = list.stream().map(InvoiceImgExcel::getTaxIncludeDamountinfigures).reduce(BigDecimal.ZERO, BigDecimal::add);
cell.setCellValue(ObjectUtil.isNotEmpty(reduce) ? String.valueOf(reduce) : null);
int rowNumber = list.size() + 2 + 1;
SXSSFRow sheetRow = sheet.createRow(rowNumber);
sheetRow.setHeightInPoints(30);
SXSSFCell sxssfCell = sheetRow.createCell(0);
sxssfCell.setCellValue("递送人签字:");
sxssfCell.setCellStyle(title);
sxssfCell = sheetRow.createCell(1);
sxssfCell.setCellStyle(title);
sxssfCell = sheetRow.createCell(2);
sxssfCell.setCellStyle(title);
this.sheet.addMergedRegion(new CellRangeAddress(rowNumber, rowNumber, 1, 2));
sxssfCell = sheetRow.createCell(3);
sxssfCell.setCellValue("接收人签字:");
sxssfCell.setCellStyle(title);
sxssfCell = sheetRow.createCell(4);
sxssfCell.setCellStyle(title);
sxssfCell = sheetRow.createCell(5);
sxssfCell.setCellStyle(title);
this.sheet.addMergedRegion(new CellRangeAddress(rowNumber, rowNumber, 4, 6));
sxssfCell = sheetRow.createCell(6);
sxssfCell.setCellStyle(title);
log.info(">>>>>>>>>>>>>>>>>>>>结束遍历数据组装单元格内容>>>>>>>>>>");
}
private int getConstant(List<InvoiceImgExcel> list, int constant, int i) {
InvoiceImgExcel invoiceImgExcel = list.get(i);
int initRow = i + 2;
SXSSFRow row = sheet.createRow(initRow);
SXSSFCell cell = row.createCell(0);
setCell(invoiceImgExcel, row, cell);
if (i != 0 && !list.get(i).getSellerName().equals(list.get(i - 1).getSellerName()) || list.size() - 1 == i) {
if (2 + constant != initRow - 1 || list.size() - 1 == i) {
this.sheet.addMergedRegion(new CellRangeAddress(2 + constant, list.size() - 1 == i && list.get(i).getSellerName().equals(list.get(i - 1).getSellerName()) ? initRow : initRow - 1, 1, 1));
constant = i;
} else {
constant = i;
}
}
return constant;
}
private void setCell(InvoiceImgExcel invoiceImgExcel, SXSSFRow row, SXSSFCell cell) {
cell.setCellValue(invoiceImgExcel.getSerialNumber());
cell.setCellStyle(content);
cell = row.createCell(1);
cell.setCellValue(invoiceImgExcel.getSellerName());
cell.setCellStyle(content);
cell = row.createCell(2);
cell.setCellValue(invoiceImgExcel.getInvoiceNumber());
cell.setCellStyle(content);
cell = row.createCell(3);
cell.setCellValue(ObjectUtil.isNotEmpty(invoiceImgExcel.getTaxIncludeDamountinfigures()) ? String.valueOf(invoiceImgExcel.getTaxIncludeDamountinfigures()) : null);
cell.setCellStyle(content);
cell = row.createCell(4);
cell.setCellValue(invoiceImgExcel.getIssueDate());
cell.setCellStyle(content);
cell = row.createCell(5);
cell.setCellValue(ObjectUtil.isNotEmpty(invoiceImgExcel.getSignInTime()) ? DateUtil.getLocalDateTimeStr(invoiceImgExcel.getSignInTime()) : null);
cell.setCellStyle(content);
cell = row.createCell(6);
cell.setCellValue(invoiceImgExcel.getSignInMark());
cell.setCellStyle(content);
}