2. Apache POI
2.1 POI介绍
Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office(Excel、WORD、PowerPoint、Visio等)格式档案读和写的功能。POI为“Poor Obfuscation Implementation”的首字母缩写,意为“可怜的模糊实现”。其中使用最多的就是使用POI操作Excel文件。
maven坐标:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency>
POI结构:
HSSF - 提供读写Microsoft Excel XLS格式档案的功能 (office97-2003版本支持,.xls) XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能(office 2007以后的版本支持,.xlsx) HWPF - 提供读写Microsoft Word DOC格式档案的功能 HSLF - 提供读写Microsoft PowerPoint格式档案的功能 HDGF - 提供读Microsoft Visio格式档案的功能 HPBF - 提供读Microsoft Publisher格式档案的功能 HSMF - 提供读Microsoft Outlook格式档案的功能
2.2 POI API
2.2.1 从Excel文件读取数据
使用POI可以从一个已经存在的Excel文件中读取数据
/**
* 读excel
* 步骤:
* 1.创建工作簿
* 2.获取工作表,既可以根据工作表的顺序获取,也可以根据工作表的名称获取
* 3.遍历工作表,获得行对象
* 4.遍历行对象,获取单元格对象
* 5.获得单元格中的值
* 6.释放资源
*/
public static void read() throws IOException {
//创建工作簿
XSSFWorkbook workbook = new XSSFWorkbook("d:\\hello.xlsx");
//获取工作表,既可以根据工作表的顺序获取,也可以根据工作表的名称获取
XSSFSheet sheet = workbook.getSheetAt(0);
//遍历工作表,获得行对象
for (Row row : sheet) {
//遍历行对象,获取单元格对象
for (Cell cell : row) {
//获得单元格中的值
String value = cell.getStringCellValue();
System.out.print(value + "\t");
}
System.out.println();
}
//释放资源
workbook.close();
}
POI操作Excel表格封装了几个核心对象:
XSSFWorkbook:工作簿 XSSFSheet:工作表 Row:行 Cell:单元格
上面案例是通过遍历工作表获得行,遍历行获得单元格,最终获取单元格中的值。
还有一种方式就是获取工作表最后一个行号,从而根据行号获得行对象,通过行获取最后一个单元格索引,从而根据单元格索引获取每行的一个单元格对象,代码如下:
public static void read() throws IOException {
//创建工作簿
XSSFWorkbook workbook = new XSSFWorkbook("D:\\hello.xlsx");
//获取工作表,既可以根据工作表的顺序获取,也可以根据工作表的名称获取
XSSFSheet sheet = workbook.getSheetAt(0);
//获取当前工作表最后一行的行号,行号从0开始
int lastRowNum = sheet.getLastRowNum();
for(int i=0;i<=lastRowNum;i++){
//根据行号获取行对象
XSSFRow row = sheet.getRow(i);
short lastCellNum = row.getLastCellNum();
for(short j=0;j<lastCellNum;j++){
String value = row.getCell(j).getStringCellValue();
System.out.println(value);
}
}
workbook.close();
}
2.2.2 向Excel文件写入数据
使用POI可以在内存中创建一个Excel文件并将数据写入到这个文件,最后通过输出流将内存中的Excel文件写入到磁盘
/**
* 写入excel
* 步骤:
* 1.在内存中创建一个excel文件
* 2.创建工作表,指定工作表名称
* 3.创建行,0表示第一行
* 4.创建单元格,0表示第一个单元格
* 5.通过输出流将workbook对象写入到磁盘
* 6.释放资源
*/
public static void write() throws IOException {
//在内存中创建一个excel文件
XSSFWorkbook workbook = new XSSFWorkbook();
//创建工作表,指定工作表名称
XSSFSheet sheet = workbook.createSheet("传智播客");
//创建行,0表示第一行
XSSFRow row = sheet.createRow(0);
//创建单元格,0表示第一个单元格
row.createCell(0).setCellValue("编号");
row.createCell(1).setCellValue("名称");
row.createCell(2).setCellValue("年龄");
XSSFRow row1 = sheet.createRow(1);
row1.createCell(0).setCellValue("1");
row1.createCell(1).setCellValue("小明");
row1.createCell(2).setCellValue("10");
XSSFRow row2 = sheet.createRow(2);
row2.createCell(0).setCellValue("2");
row2.createCell(1).setCellValue("小王");
row2.createCell(2).setCellValue("20");
//通过输出流将workbook对象写入到磁盘
FileOutputStream out = new FileOutputStream("d:\\itcast.xlsx");
workbook.write(out);
out.flush();
//释放资源
out.close();
workbook.close();
}
来源:CSDN
作者:tracyxiaoge
链接:https://blog.csdn.net/tracyxiaoge/article/details/104286254