java-poi-导出多sheet页

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


/**
*
* 没考虑样式
*/
@ApiOperationSupport(order = 5)
@GetMapping("/export-hcgr-month")
public void exportHcgrMonth(@ApiParam(value = "年月(YYYY-MM),多个用,分隔", required = true) String yearMons, HttpServletResponse response) throws IOException {
List<YxfkDeviceDetailMonthReportVO> list = receiveElectricityService.getListYxfkHcgrDeviceReportViews(Func.toStrArray(yearMons));
OutputStream out = null;
BufferedOutputStream bos = null;
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("蒸汽年月用量报表-" + yearMons + ".xlsx", "utf-8");
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
// 定义sheet
XSSFWorkbook wb = new XSSFWorkbook();
String[] title = {"序号", "蒸汽表编号", "蒸汽表名称", "起码", "止码", "倍率", "用汽量"};
XSSFCellStyle cellStyle1 = wb.createCellStyle();
//居中
cellStyle1.setAlignment(HorizontalAlignment.CENTER);
list.stream().forEach(YxfkDeviceDetailMonthReportVO -> {
XSSFSheet sheet = wb.createSheet(YxfkDeviceDetailMonthReportVO.getYearMon().replace("-", "年") + "月用量报表");
Row row = sheet.createRow((short) 0);
Cell cellHeader = row.createCell((short) 0);
cellHeader.setCellValue("蒸汽设备" + YxfkDeviceDetailMonthReportVO.getYearMon() + "用量统计");
cellHeader.setCellStyle(cellStyle1);
//跨单元格合并
CellRangeAddress cra = new CellRangeAddress(0, 0, 0, 5);
sheet.addMergedRegion(cra);
//创建标题栏
Row rowTitle = sheet.createRow((short) 1);
Cell celle0 = rowTitle.createCell(0);
Cell celle1 = rowTitle.createCell(1);
Cell celle2 = rowTitle.createCell(2);
Cell celle3 = rowTitle.createCell(3);
Cell celle4 = rowTitle.createCell(4);
Cell celle5 = rowTitle.createCell(5);
celle0.setCellValue(title[0]);
celle1.setCellValue(title[1]);
celle2.setCellValue(title[2]);
celle3.setCellValue(title[3]);
celle4.setCellValue(title[4]);
celle5.setCellValue(title[5]);
List<YxfkDeviceDetialMonthDTO> list1 = YxfkDeviceDetailMonthReportVO.getYxfkDeviceDetialMonthDTOList();
if (list1.size() > 0) {
//存入数据到excle中
for (int i = 0; i < list1.size(); i++) {
Row rowForEachNum = sheet.createRow((i + 2));
Cell celln0 = rowForEachNum.createCell(0);
Cell celln1 = rowForEachNum.createCell(1);
Cell celln2 = rowForEachNum.createCell(2);
Cell celln3 = rowForEachNum.createCell(3);
Cell celln4 = rowForEachNum.createCell(4);
Cell celln5 = rowForEachNum.createCell(5);
celln0.setCellValue(i + 1);
celln1.setCellValue(list1.get(i).getDeviceNumber());
celln2.setCellValue(list1.get(i).getDeviceName());
celln3.setCellValue(list1.get(i).getStartCode().toString());
celln4.setCellValue(list1.get(i).getEndCode().toString());
celln5.setCellValue(list1.get(i).getUseQty().toString());
}
}
Row rowEnd = sheet.createRow((short) list1.size() + 2);
Cell cellEnd1 = rowEnd.createCell((short) 0);
cellEnd1.setCellValue("当月总用量");
cellEnd1.setCellStyle(cellStyle1);
Cell cellEnd2 = rowEnd.createCell((short) 5);
cellEnd2.setCellValue(YxfkDeviceDetailMonthReportVO.getSumUseQty().toString());
CellRangeAddress cra1 = new CellRangeAddress((short) list1.size() + 2, (short) list1.size() + 2, 0, 4);
sheet.addMergedRegion(cra1);
});
out = response.getOutputStream();
wb.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
posted @ 2021-05-13 19:47  子凌_HAND  阅读(535)  评论(0)    收藏  举报