-
-
- 一、Excel导入导出的应用场景
-
-
二、POI 简介
-
- 1. 什么是POI
-
2. 为什么使用POI
-
三、工程搭建
-
四、小文件Excel的写入操作
-
- 1、操作2003版本的Excel
-
2、操作2007版本的Excel
-
五、大文件写入操作HSSF、XSSF和SXSSF
-
- 1、使用HSSF
-
2、使用XSSF
-
3、使用SXSSF
-
六、Excel文件的读操作
一、Excel导入导出的应用场景
-
数据导入:减轻录入工作量
-
数据导出:统计信息归档
-
数据传输:异构系统之间数据传输
二、POI 简介
1. 什么是POI
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能
。
-
HSSF - 提供读写Microsoft Excel格式档案的功能。(.xls)
-
XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。(.xlsx)
-
HWPF - 提供读写Microsoft Word格式档案的功能。
-
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
-
HDGF - 提供读写Microsoft Visio格式档案的功能。
2. 为什么使用POI
A major use of the Apache POI api is for Text Extraction applications
such as web spiders, index builders, and content management systems.
So why should you use POIFS, HSSF or XSSF?
You’d use POIFS if you had a document written in OLE 2 Compound
Document Format, probably written using MFC, that you needed to read
in Java. Alternatively, you’d use POIFS to write OLE 2 Compound
Document Format if you needed to inter-operate with software running
on the Windows platform. We are not just bragging when we say that
POIFS is the most complete and correct implementation of this file
format to date!
You’d use HSSF if you needed to read or write an Excel file using Java
(XLS). You’d use XSSF if you need to read or write an OOXML Excel file
using Java (XLSX). The combined SS interface allows you to easily read
and write all kinds of Excel files (XLS and XLSX) using Java.
Additionally there is a specialized SXSSF implementation which allows
to write very large Excel (XLSX) files in a memory optimized way.
三、工程搭建
-
创建一个普通的maven项目
-
pom.xml中引入相关依赖
org.apache.poi
poi
3.9
org.apache.poi
poi-ooxml
3.9
junit
junit
4.12
四、小文件Excel的写入操作
1、操作2003版本的Excel
public class ExcelWrite {
/**
- 操作2003版本的Excel
*/
@Test
public void writeExcel03() throws Exception {
// 1. 创建新的Excel工作簿workbook
HSSFWorkbook workbook = new HSSFWorkbook();
// 2. 根据 workbook 创建 sheet
HSSFSheet sheet = workbook.createSheet(“用户列表”);
// 3. 根据sheet创建行
HSSFRow row1 = sheet.createRow(0);
// 4. 根据行创建列
HSSFCell cell1 = row1.createCell(0);
// 5. 向列里面设置值
cell1.setCellValue(“扬帆向海”);
// 6. 使用输出流写入到文件中
// 6.1 定义输出流
OutputStream out = new FileOutputStream(“D:\文档\poi-test\03Excel.xls”);
// 6.2 把workbook中的内容通过输出流写入到文件中
workbook.write(out);
// 7. 关闭流
out.close();
}
}
执行结果:
2、操作2007版本的Excel
/**
- 操作2007版本的Excel
*/
@Test
public void writeExcel07() throws Exception {
// 1. 创建新的Excel工作簿workbook
XSSFWorkbook workbook = new XSSFWorkbook();
// 2. 根据 workbook 创建 sheet
XSSFSheet sheet = workbook.createSheet(“用户列表”);
// 3. 根据sheet创建行
XSSFRow row1 = sheet.createRow(0);
// 4. 根据行创建列
XSSFCell cell1 = row1.createCell(0);
// 5. 向列里面设置值
cell1.setCellValue(“扬帆向海07”);
// 6. 使用输出流写入到文件中
// 6.1 定义输出流
OutputStream out = new FileOutputStream(“D:\文档\poi-test\07Excel.xlsx”);
// 6.2 把workbook中的内容通过输出流写入到文件中
workbook.write(out);
// 7. 关闭流
out.close();
}
执行结果:
五、大文件写入操作HSSF、XSSF和SXSSF
1、使用HSSF
/**
- 操作大数据量的2003版Excel
*/
@Test
public void writeBigExcel03() throws Exception {
//创建一个SXSSFWorkbook
//-1:关闭 auto-flushing,将所有数据存在内存中
HSSFWorkbook workbook = new HSSFWorkbook();
//创建一个sheet
HSSFSheet sheet = workbook.createSheet();
// 循环多次
for (int i = 0; i < 65536; i++) {
HSSFRow row = sheet.createRow(i);
for (int j = 0; j < 10; j++) {
HSSFCell cell = row.createCell(j);
cell.setCellValue(j);
}
}
FileOutputStream out = new FileOutputStream(“D:\文档\poi-test\03BigExcel.xls”);
惊喜
最后还准备了一套上面资料对应的面试题(有答案哦)和面试时的高频面试算法题(如果面试准备时间不够,那么集中把这些算法题做完即可,命中率高达85%+)
teRow(i);
for (int j = 0; j < 10; j++) {
HSSFCell cell = row.createCell(j);
cell.setCellValue(j);
}
}
FileOutputStream out = new FileOutputStream(“D:\文档\poi-test\03BigExcel.xls”);
惊喜
最后还准备了一套上面资料对应的面试题(有答案哦)和面试时的高频面试算法题(如果面试准备时间不够,那么集中把这些算法题做完即可,命中率高达85%+)
[外链图片转存中…(img-z3wLkpnf-1714576208007)]
[外链图片转存中…(img-w1jGGzSy-1714576208007)]