数据生成excel表

导包

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

实现代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public static void write() throws Exception {
//在内存中创建excel
XSSFWorkbook excel = new XSSFWorkbook();
//在excel文件中创建Sheet页
XSSFSheet sheet = excel.createSheet("info");
XSSFSheet sheet1 = excel.createSheet("wqe");
//在sheet页中创建行对象,rownum从0开始
XSSFRow row = sheet.createRow(1);

//创建单元格并写入文件内容
row.createCell(1).setCellValue("姓名");
row.createCell(2).setCellValue("城市");


//创建新行
row = sheet.createRow(2);
row.createCell(1).setCellValue("张三");
row.createCell(2).setCellValue("长沙");

row = sheet.createRow(3);
row.createCell(1).setCellValue("李四");
row.createCell(2).setCellValue("南京");


//通过输出流把excel文件写入到磁盘
FileOutputStream out = new FileOutputStream(new File("F:\\info.xlsx"));

excel.write(out);

//关闭资源
out.close();
excel.close();

}

效果

图片