Java-web实现导出Excel中多个sheet以及自定义sheet格式原理

本文基于 HSSFWorkbook 实现自定义样式及多个sheet实现导出Excel

 

代码实现:

public void testExport(HttpServletResponse response) {
    try {
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        for (int i = 0; i < 5; i++) {
            HSSFSheet sheet = hssfWorkbook.createSheet();
            hssfWorkbook.setSheetName(i, "sheet" + i);
            sheet.setDefaultColumnWidth(17); // 设置单元格的宽度
            HSSFFont headfont = hssfWorkbook.createFont(); // 为首行设置字体
            headfont.setFontName("宋体"); // 字体类型
            headfont.setFontHeightInPoints((short) 12); // 字体大小
            HSSFCellStyle headStyle = hssfWorkbook.createCellStyle(); // 创建单元格,并设置值表头
            headStyle.setAlignment(HorizontalAlignment.CENTER);
            headStyle.setFont(headfont); // 应用字体
            String[] headContent0 = {"序号", "名称", "时间"};
            HSSFRow titleRow = sheet.createRow(0);
            HSSFCell cell = titleRow.createCell(0);
            cell.setCellValue("sheet标题");
            cell.setCellStyle(headStyle);
            HSSFRow headRow = sheet.createRow(1);
            for (int m = 0; m < headContent0.length; m++) {
                HSSFCell cell1 = headRow.createCell(m);
                cell1.setCellValue(headContent0[m]);
                cell1.setCellStyle(headStyle);
            }
            String cellValue = "";
            HSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
            HSSFFont cellFont = hssfWorkbook.createFont();
            for (int n = 0; n < 20; n++) {
                // 填充单元格
                HSSFRow otherRow = sheet.createRow(n + 2);
                otherRow.setHeight((short) 500);
                for (int j = 0; j < headContent0.length; j++) {
                    if (j == 0) {
                        cellValue = String.valueOf(n + 1);
                    } else if (j == 1) {
                        cellValue = "sheet-"+i+"-"+ n;
                    } else if (j == 2) {
                        cellValue = String.valueOf(System.currentTimeMillis());
                    }
                    cellFont.setFontName("宋体"); // 字体类型
                    cellFont.setFontHeightInPoints((short) 11); // 字体大小
                    cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 设置垂直居中
                    cellStyle.setWrapText(true); // 自动换行
                    cellStyle.setAlignment(HorizontalAlignment.CENTER);
                    cellStyle.setFont(cellFont); // 应用字体
                    HSSFCell cellOther = otherRow.createCell(j);
                    cellOther.setCellValue(cellValue);
                    cellOther.setCellStyle(cellStyle);
                }
            }
        }
        setResponseHeader(response, "导出测试" + ".xls");
        OutputStream os = response.getOutputStream();
        hssfWorkbook.write(os);
        os.flush();
        os.close();
    } catch (Exception e) {
        logger.error("导出异常!" + e.getMessage());
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 在pom.xml添加poi和poi-ooxml的依赖: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建Workbook和Sheet对象,并设置表头和数据: ```java Workbook workbook = new XSSFWorkbook(); Sheet sheet1 = workbook.createSheet("Sheet1"); Sheet sheet2 = workbook.createSheet("Sheet2"); //设置表头 Row row1 = sheet1.createRow(0); row1.createCell(0).setCellValue("姓名"); row1.createCell(1).setCellValue("年龄"); Row row2 = sheet2.createRow(0); row2.createCell(0).setCellValue("学科"); row2.createCell(1).setCellValue("成绩"); //设置数据 Row row3 = sheet1.createRow(1); row3.createCell(0).setCellValue("张三"); row3.createCell(1).setCellValue(20); Row row4 = sheet2.createRow(1); row4.createCell(0).setCellValue("语文"); row4.createCell(1).setCellValue(80); ``` 3. 将Workbook对象写入输出流: ```java response.setHeader("Content-Disposition","attachment;filename=test.xlsx"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); ``` 完整代码: ```java @RequestMapping("/export") public String export(HttpServletResponse response) throws IOException { Workbook workbook = new XSSFWorkbook(); Sheet sheet1 = workbook.createSheet("Sheet1"); Sheet sheet2 = workbook.createSheet("Sheet2"); //设置表头 Row row1 = sheet1.createRow(0); row1.createCell(0).setCellValue("姓名"); row1.createCell(1).setCellValue("年龄"); Row row2 = sheet2.createRow(0); row2.createCell(0).setCellValue("学科"); row2.createCell(1).setCellValue("成绩"); //设置数据 Row row3 = sheet1.createRow(1); row3.createCell(0).setCellValue("张三"); row3.createCell(1).setCellValue(20); Row row4 = sheet2.createRow(1); row4.createCell(0).setCellValue("语文"); row4.createCell(1).setCellValue(80); response.setHeader("Content-Disposition","attachment;filename=test.xlsx"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); return "success"; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值