java使用poi生成excel的步骤

 更新时间:2022年04月16日 12:36:14   作者:歪头儿在帝都  
2010以上格式使用XSSFWorkBook对象, 2003格式使用HSSFWorkBook对象, 其他对象操作基本一样,本文重点给大家介绍java使用poi生成excel的相关知识,感兴趣的朋友一起看看吧

使用poi生成excel通常包含一下几个步骤

  • 创建一个工作簿
  • 创建一个sheet
  • 创建一个Row对象
  • 创建一个cell对象(1个row+1个cell构成一个单元格)
  • 设置单元格内容
  • 设置单元格样式. 字体 字体大小 是否加粗
  • 保存
  • 关闭流对象

生成一个工作簿

2010以上格式使用XSSFWorkBook对象, 2003格式使用HSSFWorkBook对象, 其他对象操作基本一样.

生成2003格式

public void test1() {
    HSSFWorkbook workbook = new HSSFWorkbook();
    
    CellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setBorderBottom(BorderStyle.THIN);
    cellStyle.setBorderLeft(BorderStyle.THIN);
    cellStyle.setBorderRight(BorderStyle.THIN);
    cellStyle.setBorderTop(BorderStyle.THIN);
    
    Font font = workbook.createFont();
    font.setFontName("宋体"); 
    font.setFontHeightInPoints((short) 12);
    cellStyle.setFont(font);
   
    HSSFSheet sheet = workbook.createSheet("Sheet1");
    //设置单元格宽度
    sheet.setColumnWidth(0, 30 * 256);
    sheet.setColumnWidth(1, 30 * 256);
    sheet.setColumnWidth(2, 30 * 256);
    
    Row row0 = sheet.createRow(0);
    Cell cell0 = row0.createCell(0);
    cell0.setCellValue("序号");
    cell0.setCellStyle(cellStyle);
    
    Cell cell1 = row0.createCell(1);
    cell1.setCellValue("姓名");
    
    Cell cell2 = row0.createCell(2);
    cell2.setCellValue("成绩");
    
    OutputStream os = null;
    try {
        os = new FileOutputStream("d:\\测试生成2003.xls");
        workbook.write(os);
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

生成2010以上格式

@Test
public void test2() {
    XSSFWorkbook workbook = new XSSFWorkbook();
    
    CellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setBorderBottom(BorderStyle.THIN);
    cellStyle.setBorderLeft(BorderStyle.THIN);
    cellStyle.setBorderRight(BorderStyle.THIN);
    cellStyle.setBorderTop(BorderStyle.THIN);
    
    Font font = workbook.createFont();
    font.setFontName("宋体");
    font.setFontHeightInPoints((short) 12);
    cellStyle.setFont(font);
    
    
    XSSFSheet sheet = workbook.createSheet("Sheet1");
    Row row0 = sheet.createRow(0);
    Cell cell0 = row0.createCell(0);
    cell0.setCellValue("序号");
    cell0.setCellStyle(cellStyle);
    
    Cell cell1 = row0.createCell(1);
    cell1.setCellValue("姓名");
    
    Cell cell2 = row0.createCell(2);
    cell2.setCellValue("成绩");
    
    OutputStream os = null;
    try {
        os = new FileOutputStream("d:\\测试生成2010.xlsx");
        workbook.write(os);
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

合并单元格

合并单元格在生成excel中算常见的一个场景, 通常先合并单元, 单元格内容居中,并设置单元格边框.
poi合并单元格使用CellRangeAddress类, 构造函数包括4个参数firstRow, lastRow, firstCol, lastCol根据自己需要传入行和列.

public CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol) {
}

合并单元格后设置边框poi已提供了RegionUtil静态类, 可直接使用.

CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2);
sheet.addMergedRegion(region);

RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet);

设置单元格样式

左右居中 上下居中 自动换行

cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setWrapText(true);

使用SpringMVC/SpringBoot导出excel

@Controller
@GetMapping("/excel2003")
public void excel2003(HttpServletResponse httpServletResponse){
    try {
        //2010格式设置
        //response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //2003格式设置
        response.setContentType("application/vnd.ms-excel");
        httpServletResponse.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("学生成绩单.xls", "utf-8"));

        ServletOutputStream outputStream = httpServletResponse.getOutputStream();

        HSSFWorkbook workbook = new HSSFWorkbook();

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);

        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 12);
        cellStyle.setFont(font);

        HSSFSheet sheet = workbook.createSheet("Sheet1");
        Row row0 = sheet.createRow(0);
        Cell cell0 = row0.createCell(0);
        cell0.setCellValue("序号");
        cell0.setCellStyle(cellStyle);

        Cell cell1 = row0.createCell(1);
        cell1.setCellValue("姓名");

        Cell cell2 = row0.createCell(2);
        cell2.setCellValue("成绩");

        workbook.write(outputStream);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

总结

如果你在web项目中导出excel后,打开excel文件时提示文件已损坏,但是文件还可以打开, 则需要在HttpServletResponse上设置响应头, 2003和2010设置方式不同
2003
response.setContentType("application/vnd.ms-excel");
2010
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

下载文件名如果包含中文的话需要编码
httpServletResponse.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("学生成绩单.xls", "utf-8"));
这种设置在浏览器里下载文件中文是没问题的, 只是如果你使用Swagger或者Postman测试的话,文件名还是经过编码的, 这个没问题说明文件下载已经没问题.

到此这篇关于java使用poi生成excel的文章就介绍到这了,更多相关java生成excel内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring Cloud Feign性能优化代码实例

    Spring Cloud Feign性能优化代码实例

    这篇文章主要介绍了Spring Cloud Feign性能优化代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • SpringBoot接收参数使用的注解实例讲解

    SpringBoot接收参数使用的注解实例讲解

    这篇文章主要介绍了详解SpringBoot接收参数使用的几种常用注解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • MyBatis @Param注解的实现

    MyBatis @Param注解的实现

    本文主要介绍了MyBatis @Param注解的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • 浅谈Spring6中的反射机制

    浅谈Spring6中的反射机制

    Java反射机制是Java语言中一种动态(运行时)访问、检测、修改它本身的能力,主要作用是动态(运行时)获取类的完整结构信息、调用对象的方法,需要的朋友可以参考下
    2023-05-05
  • Java数据结构之堆(优先队列)详解

    Java数据结构之堆(优先队列)详解

    堆(优先队列)是一种典型的数据结构,其形状是一棵完全二叉树,一般用于求解topk问题。本文将利用Java语言实现堆,感兴趣的可以学习一下
    2022-07-07
  • Java使用quartz实现定时任务示例详解

    Java使用quartz实现定时任务示例详解

    这篇文章主要为大家介绍了Java使用quartz实现定时任务示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • SpringBoot中间件之封装统一白名单配置

    SpringBoot中间件之封装统一白名单配置

    这篇文章主要介绍了SpringBoot中间件封装统一白名单配置,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-07-07
  • 聊聊@value注解和@ConfigurationProperties注解的使用

    聊聊@value注解和@ConfigurationProperties注解的使用

    这篇文章主要介绍了@value注解和@ConfigurationProperties注解的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • springboot整合jsp,实现公交车站路线图

    springboot整合jsp,实现公交车站路线图

    这篇文章主要介绍了springboot整合jsp,实现公交车站路线图的步骤,帮助大家更好的理解和使用springboot框架,感兴趣的朋友可以了解下
    2021-01-01
  • Springboot实现多服务器session共享

    Springboot实现多服务器session共享

    这篇文章主要为大家详细介绍了Springboot实现多服务器session共享,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05

最新评论