常用场景
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
| <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency>
<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 { Workbook workbook = new HSSFWorkbook(); Sheet sheet = workbook.createSheet("会员列表"); Row row1 = sheet.createRow(0); Cell cell1 = row1.createCell(0); cell1.setCellValue("lucy"); OutputStream out = new FileOutputStream("F:\\1111\\01.xls"); 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 { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("会员列表07"); Row row1 = sheet.createRow(0); Cell cell1 = row1.createCell(0); cell1.setCellValue("lucy07"); OutputStream out = new FileOutputStream("F:\\1111\\0107.xlsx"); 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 { InputStream in = new FileInputStream("F:\\1111\\01.xls"); Workbook workbook = new HSSFWorkbook(in); Sheet sheet = workbook.getSheetAt(0); Row row = sheet.getRow(0); Cell cell = row.getCell(0);
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(); Workbook workbook = new HSSFWorkbook(); 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(); Workbook workbook = new XSSFWorkbook(); 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
| @Test public void testBigData07plus() throws Exception { long begin=System.currentTimeMillis(); Workbook workbook = new SXSSFWorkbook(); 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); }
|
