您好,登录后才能下订单哦!
Apache POI 是一个用于处理Microsoft Office文档的Java库,支持Excel、Word、PowerPoint等文件的读写操作。本文将详细介绍如何使用POI库来读取Excel文件。
首先,需要在项目中引入POI的相关依赖。如果使用Maven构建项目,可以在pom.xml
中添加以下依赖:
<dependencies>
<!-- POI核心库 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<!-- POI对Excel的扩展支持 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<!-- POI对Excel的扩展支持(需要处理.xlsx文件时使用) -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<!-- XML处理库 -->
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>5.1.1</version>
</dependency>
</dependencies>
使用POI读取Excel文件的基本步骤如下:
.xls
或.xlsx
)创建对应的Workbook
对象。Sheet
)。.xls
文件对于.xls
格式的Excel文件,可以使用HSSFWorkbook
类来读取。
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.IOException;
public class ReadExcelXLS {
public static void main(String[] args) {
try (FileInputStream file = new FileInputStream("example.xls")) {
Workbook workbook = new HSSFWorkbook(file);
Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
case NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;
default:
System.out.print("UNKNOWN\t");
}
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
.xlsx
文件对于.xlsx
格式的Excel文件,可以使用XSSFWorkbook
类来读取。
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.IOException;
public class ReadExcelXLSX {
public static void main(String[] args) {
try (FileInputStream file = new FileInputStream("example.xlsx")) {
Workbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
case NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;
default:
System.out.print("UNKNOWN\t");
}
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
在读取Excel文件时,单元格可能包含不同类型的数据,如字符串、数字、布尔值等。POI提供了CellType
枚举来表示单元格的类型,可以通过cell.getCellType()
方法获取单元格的类型,并根据类型调用相应的方法来获取数据。
如果单元格类型为STRING
,可以使用cell.getStringCellValue()
方法获取字符串值。
case STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
如果单元格类型为NUMERIC
,可以使用cell.getNumericCellValue()
方法获取数字值。
case NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
如果单元格类型为BOOLEAN
,可以使用cell.getBooleanCellValue()
方法获取布尔值。
case BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;
如果单元格类型为BLANK
、ERROR
等,可以根据需要进行处理。
default:
System.out.print("UNKNOWN\t");
在Excel中,日期通常以数字形式存储。POI提供了DateUtil
工具类来处理日期格式的单元格。
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.print(cell.getDateCellValue() + "\t");
} else {
System.out.print(cell.getNumericCellValue() + "\t");
}
break;
通过Apache POI库,Java可以轻松地读取Excel文件中的数据。本文介绍了如何使用POI读取.xls
和.xlsx
格式的Excel文件,并处理不同类型的单元格数据。POI库功能强大,支持多种Excel操作,是Java开发中处理Excel文件的常用工具。
在实际开发中,可以根据需求进一步扩展和优化代码,例如处理大文件、读取特定区域的数据、处理复杂的Excel公式等。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。