POI创建EXCEL
1 POI依赖
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
创建Excel
public void downloadJyzxSectionExcel(HttpServletRequest request, HttpServletResponse response, Short cycle) throws Exception{
List<GpSystemNoticeGetBO> list = gpSystemNoticeService.findDateSection(cycle).getData();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("项目统计表");
sheet.setColumnWidth(0, 12 * 256);
sheet.setColumnWidth(1, 13 * 256);
sheet.setColumnWidth(2, 11 * 256);
sheet.setColumnWidth(3, 10 * 256);
sheet.setColumnWidth(4, 11 * 256);
sheet.setColumnWidth(5, 11 * 256);
sheet.setColumnWidth(6, 11 * 256);
sheet.setColumnWidth(7, 15 * 256);
sheet.setColumnWidth(8, 23 * 256);
sheet.setColumnWidth(9, 15 * 256);
sheet.setColumnWidth(10, 11 * 256);
sheet.setColumnWidth(11, 11 * 256);
sheet.setColumnWidth(12, 10 * 256);
sheet.setColumnWidth(13, 10 * 256);
sheet.setColumnWidth(14, 15 * 256);
sheet.setColumnWidth(15, 12 * 256);
sheet.setColumnWidth(16, 16 * 256);
HSSFCellStyle titleStyle = workbook.createCellStyle();
titleStyle.setAlignment(HorizontalAlignment.CENTER);
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
titleStyle.setBorderBottom(BorderStyle.MEDIUM);
titleStyle.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
titleStyle.setBorderLeft(BorderStyle.MEDIUM);
titleStyle.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
titleStyle.setBorderTop(BorderStyle.MEDIUM);
titleStyle.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
titleStyle.setBorderRight(BorderStyle.MEDIUM);
titleStyle.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
HSSFFont font = workbook.createFont();
font.setItalic(false);
font.setColor(Font.COLOR_NORMAL);
font.setFontHeightInPoints((short)20);
font.setFontName("Helvetica Neue");
font.setBold(false);
titleStyle.setFont(font);
HSSFCellStyle titleStyle1 = workbook.createCellStyle();
titleStyle1.setAlignment(HorizontalAlignment.CENTER);
titleStyle1.setVerticalAlignment(VerticalAlignment.CENTER);
titleStyle1.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
titleStyle1.setFillPattern(FillPatternType.SOLID_FOREGROUND);
titleStyle1.setBorderBottom(BorderStyle.MEDIUM);
titleStyle1.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
titleStyle1.setBorderLeft(BorderStyle.MEDIUM);
titleStyle1.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
titleStyle1.setBorderTop(BorderStyle.MEDIUM);
titleStyle1.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
titleStyle1.setBorderRight(BorderStyle.MEDIUM);
titleStyle1.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
HSSFFont font1 = workbook.createFont();
font1.setItalic(false);
font1.setColor(Font.COLOR_NORMAL);
font1.setFontHeightInPoints((short)10);
font1.setFontName("Helvetica Neue");
font1.setBold(true);
titleStyle1.setFont(font1);
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setWrapText(true);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setBorderBottom(BorderStyle.MEDIUM);
cellStyle.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
cellStyle.setBorderLeft(BorderStyle.MEDIUM);
cellStyle.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
cellStyle.setBorderTop(BorderStyle.MEDIUM);
cellStyle.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
cellStyle.setBorderRight(BorderStyle.MEDIUM);
cellStyle.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
Font cellFont = workbook.createFont();
cellFont.setColor(Font.COLOR_NORMAL);
cellFont.setFontHeightInPoints((short)10);
cellFont.setFontName("宋体");
cellStyle.setFont(cellFont);
HSSFRow row1 = sheet.createRow(0);
row1.setHeightInPoints(36);
HSSFCell cell = row1.createCell(0);
CellRangeAddress region = new CellRangeAddress(0, 0, 0,16);
sheet.addMergedRegion(region);
cell.setCellValue("项目统计表");
cell.setCellStyle(titleStyle);
HSSFRow row2 = sheet.createRow(1);
row2.setHeightInPoints(25);
HSSFCell row2Cell1 = row2.createCell(0);
row2Cell1.setCellValue("运营商");
row2Cell1.setCellStyle(titleStyle1);
HSSFCell row2Cell2 = row2.createCell(1);
row2Cell2.setCellValue("代理机构");
row2Cell2.setCellStyle(titleStyle1);
HSSFCell row2Cell3 = row2.createCell(2);
row2Cell3.setCellValue("项目编号");
row2Cell3.setCellStyle(titleStyle1);
HSSFCell row2Cell4 = row2.createCell(3);
row2Cell4.setCellValue("时间");
row2Cell4.setCellStyle(titleStyle1);
HSSFCell row2Cell5 = row2.createCell(4);
row2Cell5.setCellValue("名称");
row2Cell5.setCellStyle(titleStyle1);
HSSFCell row2Cell6 = row2.createCell(5);
row2Cell6.setCellValue("标的额");
row2Cell6.setCellStyle(titleStyle1);
HSSFCell row2Cell7 = row2.createCell(6);
row2Cell7.setCellValue("区域");
row2Cell7.setCellStyle(titleStyle1);
HSSFCell row2Cell8 = row2.createCell(7);
row2Cell8.setCellValue("投标人名称");
row2Cell8.setCellStyle(titleStyle1);
HSSFCell row2Cell9 = row2.createCell(8);
row2Cell9.setCellValue("组织机构代码证");
row2Cell9.setCellStyle(titleStyle1);
HSSFCell row2Cell10 = row2.createCell(9);
row2Cell10.setCellValue("付费时间");
row2Cell10.setCellStyle(titleStyle1);
HSSFCell row2Cell11 = row2.createCell(10);
row2Cell11.setCellValue("付费方式");
row2Cell11.setCellStyle(titleStyle1);
HSSFCell row2Cell12 = row2.createCell(11);
row2Cell12.setCellValue("付费账号名");
row2Cell12.setCellStyle(titleStyle1);
HSSFCell row2Cell13 = row2.createCell(12);
row2Cell13.setCellValue("付费金额");
row2Cell13.setCellStyle(titleStyle1);
HSSFCell row2Cell14 = row2.createCell(13);
row2Cell14.setCellValue("联系人");
row2Cell14.setCellStyle(titleStyle1);
HSSFCell row2Cell15 = row2.createCell(14);
row2Cell15.setCellValue("电话");
row2Cell15.setCellStyle(titleStyle1);
HSSFCell row2Cell16 = row2.createCell(15);
row2Cell16.setCellValue("合计");
row2Cell16.setCellStyle(titleStyle1);
HSSFCell row2Cell17 = row2.createCell(16);
row2Cell17.setCellValue("备注");
row2Cell17.setCellStyle(titleStyle1);
int rownum = 2;
for (GpSystemNoticeGetBO gpnoticeBo : list){
List<BbPayInfoQueryBO> rtList = bbPayInfoServiceImpl.getPlatformFeeInfo(gpnoticeBo.getGsId(),
BbPayTypeEnum.PLATFORM_FEE.getValue()).getData();
int regionNum = rownum;
BigDecimal payAmountCount = new BigDecimal("0.00");
for(BbPayInfoQueryBO payInfo : rtList){
payAmountCount = payAmountCount.add(payInfo.getPayAmount());
}
for (int i = 0; i<rtList.size(); i++){
ApCompanyGetBO companyBO = apCompanyServiceImpl.getById(rtList.get(i).getPersonCoId()).getData();
String regionName = apZoningServiceImpl.getFullName(gpnoticeBo.getRegionId()).getData();
String payType = "";
if (rtList.get(i).getPayType() == BbPaymentEnum.ALI_PAY.getValue() ){
payType = "支付宝";
}else if(rtList.get(i).getPayType() == BbPaymentEnum.WX_PAY.getValue()){
payType = "微信";
}
HSSFRow rowi = sheet.createRow(rownum+i);
rowi.setHeightInPoints(30);
HSSFCell rowiCell1 = rowi.createCell(0);
rowiCell1.setCellValue("");
rowiCell1.setCellStyle(cellStyle);
HSSFCell rowiCell2 = rowi.createCell(1);
rowiCell2.setCellValue(gpnoticeBo.getAgentName());
rowiCell2.setCellStyle(cellStyle);
HSSFCell rowiCell3 = rowi.createCell(2);
rowiCell3.setCellValue(gpnoticeBo.getPublicGtpCode());
rowiCell3.setCellStyle(cellStyle);
HSSFCell rowiCell4 = rowi.createCell(3);
rowiCell4.setCellValue("");
rowiCell4.setCellStyle(cellStyle);
HSSFCell rowiCell5 = rowi.createCell(4);
rowiCell5.setCellValue(gpnoticeBo.getGsName());
rowiCell5.setCellStyle(cellStyle);
HSSFCell rowiCell6 = rowi.createCell(5);
rowiCell6.setCellValue(gpnoticeBo.getEstimatedPrice().toString());
rowiCell6.setCellStyle(cellStyle);
HSSFCell rowiCell7 = rowi.createCell(6);
rowiCell7.setCellValue(regionName);
rowiCell7.setCellStyle(cellStyle);
HSSFCell rowiCell8 = rowi.createCell(7);
rowiCell8.setCellValue(rtList.get(i).getPersonCoName());
rowiCell8.setCellStyle(cellStyle);
HSSFCell rowiCell9 = rowi.createCell(8);
rowiCell9.setCellValue(companyBO.getCreditCode());
rowiCell9.setCellStyle(cellStyle);
HSSFCell rowiCell10 = rowi.createCell(9);
rowiCell10.setCellValue(rtList.get(i).getPayTime());
rowiCell10.setCellStyle(cellStyle);
HSSFCell rowiCell11 = rowi.createCell(10);
rowiCell11.setCellValue(payType);
rowiCell11.setCellStyle(cellStyle);
HSSFCell rowiCell12 = rowi.createCell(11);
rowiCell12.setCellValue("");
rowiCell12.setCellStyle(cellStyle);
HSSFCell rowiCell13 = rowi.createCell(12);
rowiCell13.setCellValue(rtList.get(i).getPayAmount().toString());
rowiCell13.setCellStyle(cellStyle);
HSSFCell rowiCell14 = rowi.createCell(13);
rowiCell14.setCellValue(rtList.get(i).getPersonName());
rowiCell14.setCellStyle(cellStyle);
HSSFCell rowiCell15 = rowi.createCell(14);
rowiCell15.setCellValue(rtList.get(i).getPersonPhone());
rowiCell15.setCellStyle(cellStyle);
HSSFCell rowiCell16 = rowi.createCell(15);
rowiCell16.setCellValue(payAmountCount.toString());
rowiCell16.setCellStyle(cellStyle);
HSSFCell rowiCell17 = rowi.createCell(16);
rowiCell17.setCellValue("");
rowiCell17.setCellStyle(cellStyle);
regionNum = regionNum++;
}
if(CollectionUtils.isNotEmpty(rtList) && rtList.size() > 1){
sheet.addMergedRegion(new CellRangeAddress(regionNum,regionNum+rtList.size()-1,0,0));
sheet.addMergedRegion(new CellRangeAddress(regionNum,regionNum+rtList.size()-1,1,1));
sheet.addMergedRegion(new CellRangeAddress(regionNum,regionNum+rtList.size()-1,2,2));
sheet.addMergedRegion(new CellRangeAddress(regionNum,regionNum+rtList.size()-1,3,3));
sheet.addMergedRegion(new CellRangeAddress(regionNum,regionNum+rtList.size()-1,4,4));
sheet.addMergedRegion(new CellRangeAddress(regionNum,regionNum+rtList.size()-1,5,5));
sheet.addMergedRegion(new CellRangeAddress(regionNum,regionNum+rtList.size()-1,6,6));
sheet.addMergedRegion(new CellRangeAddress(regionNum,regionNum+rtList.size()-1,15,15));
sheet.addMergedRegion(new CellRangeAddress(regionNum,regionNum+rtList.size()-1,16,16));
}
rownum = rownum+rtList.size();
}
try {
OutputStream output = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition",
"attachment; filename=projectCount.xls");
response.setContentType("application/msexcel");
workbook.write(output);
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ecf261f8257b49d9e4d00eda7defd6b9.png)