一. POI介绍
1. 简介
POI为“Poor Obfuscation Implementation”的首字母缩写。Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
2. 结构
结构 | 作用 |
---|---|
HSSF | 提供读写Microsoft Excel XLS 格式档案的功能。 |
XSSF | 提供读写Microsoft Excel OOXML XLSX 格式档案的功能。 |
HWPF | 提供读写Microsoft Word DOC 格式档案的功能。 |
HSLF | 提供读写Microsoft PowerPoint 格式档案的功能。 |
HDGF | 提供读Microsoft Visio 格式档案的功能。 |
HPBF | 提供读Microsoft Publisher 格式档案的功能。 |
HSMF | 提供读Microsoft Outlook 格式档案的功能。 |
其中的HSSF和XSSF就是对Excel文件进行解析的类。他们的区别为:
- HSSF是对Excel 97(-2007)文件操作的实现(也就是.xls文件),与之对应的类为
HSSFWorkbook
。 - XSSF是对Excel 2007文件操作的实现(也就是.xlsx文件),与之对应的类为
XSSFWorkbook
。
3. 依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
二. 解析Excel文件所用到的接口
Workbook
对应为一个Excel文件对象,具体的实现类就是HSSFWorkbook
和XSSFWorkbook
。Sheet
对应为一个Excel的页签对象。Row
对应为一个行对象。Cell
对应为一个单元格对象。
三. 数据读取例子
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class POIExcelImport {
public static void main(String[] args) {
FileInputStream fileInputStream = null;
Workbook workbook = null;
int totalRowNumber = 0;
int totalClounmNumber = 0;
File file = new File("E:\\IDEA代码\\java学习\\" +
"java解析excel\\POILeaning\\ExcelDeom\\POIDemo.xls") ;
//获取workbook对象
try {
fileInputStream =new FileInputStream(file);
workbook = new HSSFWorkbook(fileInputStream);
}
catch (IOException e){
e.getStackTrace();
}
//得到一个工作表
Sheet sheet = workbook.getSheetAt(0);
//获得总数据行数数据(注意,这是个索引值,以0开始)
totalRowNumber = sheet.getLastRowNum();
for (int i=0;i<=totalRowNumber;i++){
//获得每一行数据
Row row = sheet.getRow(i);
//获得每行的单元格数
totalClounmNumber = row.getPhysicalNumberOfCells();
for (int j=0;j<totalClounmNumber;j++) {
Cell cell = row.getCell(j);
System.out.print(cell.getStringCellValue());
}
System.out.println();
}
//关闭文件输入流
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
读取的表格为:
输出结果为:
四. 数据写出例子
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class POIExcelOutport {
public static void main(String[] args) {
//创建一个Workbook对象
Workbook workbook = new HSSFWorkbook();
//创建一个Sheet对象
Sheet sheet = workbook.createSheet();
//创建一个Row对象,行的索引值为0
Row row = sheet.createRow(0);
//创建单元格Cell并赋值
Cell cell1 = row.createCell(0);
cell1.setCellValue("小颖");
row.createCell(1).setCellValue("喜欢");
row.createCell(2).setCellValue("唱歌");
//将workbook写入文件
try {
OutputStream outputStream = new FileOutputStream("E:\\IDEA代码\\java学习\\" +
"java解析excel\\POILeaning\\ExcelDeom\\POIDemo2.xls");
((HSSFWorkbook) workbook).write(outputStream);
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出的结果为: