POI实现Excel多sheet页导出

//表头列名样式
public static XSSFCellStyle createHeadStyle(XSSFWorkbook workbook) {
        XSSFCellStyle style = workbook.createCellStyle();
        // 设置背景色
        style.setFillForegroundColor(new XSSFColor(new Color(87, 163, 250)));
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        // 设置边框
        style.setBorderBottom(BorderStyle.THIN); // 下边框
        style.setBorderRight(BorderStyle.THIN);// 右边框
        style.setBorderLeft(BorderStyle.THIN);// 左边框
        style.setBorderTop(BorderStyle.THIN);// 上边框
        // 设置单元格的中心水平对齐-居中
        style.setAlignment(HorizontalAlignment.CENTER);
        // 设置单元格的垂直对齐类型-居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 生成字体
        XSSFFont font = workbook.createFont();
        // 设置字体类型
        font.setFontName("微软雅黑");
        // 设置字体大小
        font.setFontHeightInPoints((short) 10);
        // 把字体应用到当前的样式
        style.setFont(font);
        return style;

    }

   	//表格数据单元格样式
    public static XSSFCellStyle createBodyStyle(XSSFWorkbook workbook) {
        XSSFCellStyle style = workbook.createCellStyle();
        // 设置边框
        style.setBorderBottom(BorderStyle.THIN); // 下边框
        style.setBorderRight(BorderStyle.THIN);// 右边框
        style.setBorderLeft(BorderStyle.THIN);// 左边框
        style.setBorderTop(BorderStyle.THIN);// 上边框
        // 设置单元格的中心水平对齐-居中
        style.setAlignment(HorizontalAlignment.CENTER);
        // 设置单元格的垂直对齐类型-居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 生成字体
        XSSFFont font = workbook.createFont();
        // 字体类型
        font.setFontName("微软雅黑");
        // 设置字体大小
        font.setFontHeightInPoints((short) 9);
        // 把字体应用到当前的样式
        style.setFont(font);
        return style;
    }
@PostMapping("/export")
    public R export(HttpServletResponse response,@RequestBody MainBodyVO mainBodyVO){

        List<MainBody> mainBodyList = mainBodyService.list(Wrappers.<MainBody>lambdaQuery()
                .like(StringUtils.isNotBlank(mainBodyVO.getMainTypes()),MainBody::getMainTypes,mainBodyVO.getMainTypes())
                .like(StringUtils.isNotBlank(mainBodyVO.getIssuerName()),MainBody::getIssuerName,mainBodyVO.getIssuerName())
                .like(StringUtils.isNotBlank(mainBodyVO.getInventoryLevel()),MainBody::getInventoryLevel,mainBodyVO.getInventoryLevel()));

        // 1.声明一个工作薄
        XSSFWorkbook workbook = new XSSFWorkbook();

        mainBodyList.stream().collect(Collectors.groupingBy(MainBody::getMainTypes)).forEach((k, v)->{
            XSSFSheet sheet = workbook.createSheet(k);
            sheet.setDefaultColumnWidth(15);
            XSSFCellStyle headerStyle = createHeadStyle(workbook);
            XSSFCellStyle bodyStyle = createBodyStyle(workbook);

            int rowNum = 0;
            boolean titleFlag = false;
            for (MainBody mainBody : v) {
                // 声明Excel标题名称
                String[] excelHeader = {"列名", "列名"};
                if (!titleFlag) {
                    Row row = sheet.createRow(rowNum++);
                    // 创建标题首行列
                    for (int i = 0; i < excelHeader.length; i++) {
                        Cell cell = row.createCell(i); // 列下标
                        cell.setCellValue(excelHeader[i]); // 列值
                        cell.setCellStyle(headerStyle); // 设置单元格样式
                    }
                }

                titleFlag = true;

                Row row = sheet.createRow(rowNum++);
                for (int i = 0; i < excelHeader.length; i++) {
                    Cell cell = row.createCell(i);
                    cell.setCellStyle(bodyStyle);
                    if (i == 0) {
                    	if(mainBody.getIssuerName() != null){
                    		cell.setCellValue(mainBody.getIssuerName());
                    	}
                    } else if (i == 1) {
                    	if(mainBody.getInventoryLevel() != null){
                    		cell.setCellValue(mainBody.getInventoryLevel());
                    	}
                    }
                }
            }
        });

         // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        try {
            String fileName = "表格名称";
            String codedFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="+codedFileName + ".xlsx");
            OutputStream stream = response.getOutputStream();
            if (null != workbook && null != stream) {
                workbook.write(stream);// 将数据写出去
                workbook.close();
                stream.close();
            }
        } catch (IOException e) {
            log.error("导出失败",e.getMessage());
            return R.failed("导出失败");
        }
        return R.ok(null,"导出成功");
    }

你可能感兴趣的:(java,POI,Excel,导出,多sheet页)