常用场景
1、讲用户信息导出eccel表格

2、将excel表中的信息录入到网站数据库

开发中经常会涉及到excel的处理,如导出excel,导入excel到数据库。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!--xls-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>

<!--xlsx-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>

<!--日期格式化工具-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>

测试向03版本excel写入内容操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Test
public void writeExcel03() throws Exception {
//1 创建workbook 工作簿
Workbook workbook = new HSSFWorkbook();
//2 根据workbook创建sheet 工作表
Sheet sheet = workbook.createSheet("会员列表");
//3根据sheet创建row 行
Row row1 = sheet.createRow(0);
//4根据行创建cell 列
Cell cell1 = row1.createCell(0);
//5 向cell里面设置值
cell1.setCellValue("lucy");
//6 使用输出流写入到文件中
OutputStream out = new FileOutputStream("F:\\1111\\01.xls");
//把workbook内容通过输出流写入文件中
workbook.write(out);
//关闭流
out.close();
}

测试向07版本excel写入内容操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Test
public void writeExcel07() throws Exception {
//1 创建workbook 工作簿
Workbook workbook = new XSSFWorkbook();
//2 根据workbook创建sheet 工作表
Sheet sheet = workbook.createSheet("会员列表07");
//3根据sheet创建row 行
Row row1 = sheet.createRow(0);
//4根据行创建cell 列
Cell cell1 = row1.createCell(0);
//5 向cell里面设置值
cell1.setCellValue("lucy07");
//6 使用输出流写入到文件中
OutputStream out = new FileOutputStream("F:\\1111\\0107.xlsx");
//把workbook内容通过输出流写入文件中
workbook.write(out);
//关闭流
out.close();
}

3 测试读取03版本excel里面的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Test
public void readExcel03() throws Exception {
//1 获取读取文件的输入流
InputStream in = new FileInputStream("F:\\1111\\01.xls");
//2 创建workbook,需要把输入流传递进去
Workbook workbook = new HSSFWorkbook(in);
//3 根据workbook获取sheet
Sheet sheet = workbook.getSheetAt(0);
//4 根据sheet获取行
Row row = sheet.getRow(0);
//5 根据行获取cell
Cell cell = row.getCell(0);

//6 获取cell里面的值
String value = cell.getStringCellValue();
System.out.println(value);
in.close();
}

4.操作大数据量文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Test
public void testBigData03() throws Exception {
long begin=System.currentTimeMillis();
//创建一个SXSSFWorkbook
//-1:关闭 auto-flushing,将所有数据存在内存中
Workbook workbook = new HSSFWorkbook();
//创建一个sheet
Sheet sheet = workbook.createSheet();
//循环多次
for (int i = 0; i < 65536; i++) {
Row row = sheet.createRow(i);
for (int j = 0; j < 10; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(j);
}
}
OutputStream out = new FileOutputStream("F:\\1111\\big01.xls");
workbook.write(out);
out.close();
long end =System.currentTimeMillis();
System.out.println((double)(end-begin)/1000);
}

当数据超过65536时控制台报错

07版本操作POI理论没有大小限制 但是速度变慢

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Test
public void testBigData07() throws Exception {
long begin=System.currentTimeMillis();
//创建一个SXSSFWorkbook
//-1:关闭 auto-flushing,将所有数据存在内存中
Workbook workbook = new XSSFWorkbook();
//创建一个sheet
Sheet sheet = workbook.createSheet();
//循环多次
for (int i = 0; i < 65536; i++) {
Row row = sheet.createRow(i);
for (int j = 0; j < 10; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(j);
}
}
OutputStream out = new FileOutputStream("F:\\1111\\big01.xlsx");
workbook.write(out);
out.close();
long end =System.currentTimeMillis();
System.out.println((double)(end-begin)/1000);
}

要想操作大文件变快可以使用SXSSF

优点:可以写非常大的数据量,如100w条甚至更多,写的数据快,占用内存少

注意

  • 过程中会产生零时文件,需要清理
  • 默认由100条记录保存在内存中,如果超过这数量,则最前面的被写入临时文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//4 07操作大数据量文件
@Test
public void testBigData07plus() throws Exception {
long begin=System.currentTimeMillis();
Workbook workbook = new SXSSFWorkbook();
//创建一个sheet
Sheet sheet = workbook.createSheet();
//循环多次
for (int i = 0; i < 65536; i++) {
Row row = sheet.createRow(i);
for (int j = 0; j < 10; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(j);
}
}
OutputStream out = new FileOutputStream("F:\\1111\\big07p.xlsx");
workbook.write(out);
out.close();
long end =System.currentTimeMillis();
System.out.println((double)(end-begin)/1000);
}