Java使用POI导出Excel表格

1.导入POI相关的依赖(注意版本需要一致)

 1 <dependency>
 2     <groupId>org.apache.poi</groupId>
 3     <artifactId>poi</artifactId>
 4     <version>3.9</version>
 5 </dependency>
 6 <dependency>
 7     <groupId>org.apache.poi</groupId>
 8     <artifactId>poi-ooxml</artifactId>
 9     <version>3.9</version>
10 </dependency>

2.导出Excel表格工具类代

 1 package com.tgram.sboot.uitl;
 2 
 3 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
 4 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 5 import org.apache.poi.ss.usermodel.*;
 6 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 7 
 8 import java.io.IOException;
 9 import java.io.OutputStream;
10 import java.util.List;
11 
12 /**
13  *<p> Description: 导出Excel工具类 </p>
14  *<p> Copyright: Copyright(c) 2018/12/27 </p>
15  *<p> Company: xxx </p>
16  *
17  *@author Jason
18  *@Version 1.0 2018/12/27 11:30
19  */
20 public class ExportExcelUtil
21 {
22     
23     public static void exportExcel(List<List<String>> dataList, OutputStream stream, boolean isNewVersion)
24     {
25         if (dataList == null || dataList.size() == 0)
26         {
27             return;
28         }
29         
30         // 1.创建Workbook对象(excel的文档对象)
31         Workbook workbook;
32         if (isNewVersion)
33         {
34             workbook = new SXSSFWorkbook();
35         }
36         else
37         {
38             workbook = new HSSFWorkbook();
39         }
40         
41         // 2.创建文档格式对象并建立新的sheet对象(excel的表单)
42         CellStyle cellStyle = getCellStyle(workbook);
43         Sheet sheet = workbook.createSheet();
44         
45         // 3.创建行对象,循环生成数据
46         Row row;
47         List<String> list;
48         for (int i = 0; i < dataList.size(); i++)
49         {
50             // 循环遍历生成每一行数据,list保存每行对应的列数据
51             row = sheet.createRow(i);
52             list = dataList.get(i);
53             for (int j = 0; j < list.size(); j++) {
54                 Cell cell = row.createCell(j);      // 通过行对象创建列对象
55                 cell.setCellValue(list.get(j));     // 设置列数据
56                 cell.setCellStyle(cellStyle);       // 设置列样式
57             }
58         }
59 
60         try {
61             // 4.通过输出流写入数据到Excel文档对象中
62             workbook.write(stream);
63         } catch (IOException e) {
64             throw new RuntimeException("系统内部错误");
65         }
66 
67     }
68     
69     /**
70      * 设置Excel的格式
71      * @param workbook Workbook文档对象
72      * @return CellStyle对象
73      */
74     private static CellStyle getCellStyle(Workbook workbook)
75     {
76         CellStyle cellStyle = workbook.createCellStyle();
77         DataFormat dataFormat = workbook.createDataFormat();
78         //文本类型
79         cellStyle.setDataFormat(dataFormat.getFormat("@"));
80         //自动换行
81         cellStyle.setWrapText(true);
82         //上下居中
83         cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
84         
85         return cellStyle;
86     }
87 }

POI DateFormat种类(cellStyle.setDataFormat(dataFormat.getFormat("@")))参考地址:https://blog.csdn.net/phoenixx123/article/details/12720431

3.设置表头整理数据导出Excel表格

 1 /**
 2  *<p> Description: 控制层 </p>
 3  *<p> Copyright: Copyright(c) 2018/11/9 </p>
 4  *<p> Company: xxx </p>
 5  *
 6  *@author Jason
 7  *@Version 1.0 2018/11/9 14:22
 8  */
 9 @RestController
10 public class StudentController
11 {  
12     @Resource
13     private StudentService studentService;
14     
15     /**
16      * 将所有的学生信息到处到excel表格中
17      * @param response
18      */
19     @RequestMapping("/allStudsExportExcle")
20     public void allStudsExportExcle(HttpServletResponse response) throws IOException
21     {
22         // 设置请求头信息(使用String防止文件名中文乱码)
23         response.setHeader("Content-Disposition","attachment;filename=" 
24                 + new String("学生信息表.xls".getBytes(),"ISO-8859-1"));
25         // response.setHeader("Content-disposition", "attachment; filename=student.xls");
26         
27         // 获取输入流信息
28         OutputStream outputStream = response.getOutputStream();
29         
30         // 获取数据并封装数据
31         List<List<String>> dataList = new ArrayList<>();
32         List<String> headList = Arrays.asList("学生编号", "学生姓名", "学生年龄");
33         dataList.add(headList);
34 
35         // 获取数据
36         List<Student> students = studentService.allStudentsList();
37         students.forEach(student -> {
38             List<String> rowList = new ArrayList<>();
39             rowList.add(student.getStu_id() + "");
40             rowList.add(student.getStu_name() + "");
41             rowList.add(student.getStu_age() + "");
42             dataList.add(rowList);
43         });
44 
45         // 调用工具类到处Excel表格
46         ExportExcelUtil.exportExcel(dataList,outputStream,false);
47 
48         // 刷新输出流
49         outputStream.flush();
50     }
51 }
原文地址:https://www.cnblogs.com/jason2018524/p/10184598.html