您好,登录后才能下订单哦!
Apache POI(Poor Obfuscation Implementation)是一个开源的Java库,用于处理Microsoft Office文档,包括Excel、Word和PowerPoint等。POI库提供了丰富的API,使得Java开发者能够轻松地读取、写入和操作Excel文件。本文将详细介绍如何使用Apache POI库来读取Excel文件。
在开始之前,我们需要确保开发环境中已经配置了Apache POI库。可以通过Maven或手动下载JAR包的方式引入POI库。
在pom.xml
文件中添加以下依赖:
<dependencies>
<!-- Apache POI核心库 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<!-- Apache POI OOXML库(用于处理.xlsx文件) -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<!-- Apache POI OOXML Schemas库 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<!-- XMLBeans库 -->
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>5.1.1</version>
</dependency>
</dependencies>
如果你不使用Maven,可以手动下载以下JAR包并将其添加到项目的类路径中:
poi-5.2.3.jar
poi-ooxml-5.2.3.jar
poi-ooxml-schemas-4.1.2.jar
xmlbeans-5.1.1.jar
使用POI读取Excel文件的基本步骤如下:
Workbook
对象,表示整个Excel文件。Sheet
(工作表)。Sheet
中的每一行(Row
)。Cell
)。.xls
文件.xls
文件是Excel 97-2003格式的文件,使用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;
case FORMULA:
System.out.print(cell.getCellFormula() + "\t");
break;
default:
System.out.print("UNKNOWN\t");
}
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
.xlsx
文件.xlsx
文件是Excel 2007及以上版本的文件格式,使用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;
case FORMULA:
System.out.print(cell.getCellFormula() + "\t");
break;
default:
System.out.print("UNKNOWN\t");
}
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
在读取Excel文件时,单元格中的数据可能有多种类型,如字符串、数字、布尔值、公式等。我们需要根据单元格的类型来正确处理数据。
字符串数据是最常见的数据类型,可以直接使用getStringCellValue()
方法获取。
case STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
数字数据可以是整数或浮点数,使用getNumericCellValue()
方法获取。
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.print(cell.getDateCellValue() + "\t");
} else {
System.out.print(cell.getNumericCellValue() + "\t");
}
break;
布尔数据只有true
或false
两种值,使用getBooleanCellValue()
方法获取。
case BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;
公式数据表示单元格中包含的公式,使用getCellFormula()
方法获取公式字符串。
case FORMULA:
System.out.print(cell.getCellFormula() + "\t");
break;
空单元格没有数据,可以使用CellType.BLANK
来判断。
case BLANK:
System.out.print("BLANK\t");
break;
Excel中的日期数据通常以数字形式存储,POI提供了DateUtil
工具类来处理日期数据。
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.print(cell.getDateCellValue() + "\t");
} else {
System.out.print(cell.getNumericCellValue() + "\t");
}
break;
对于非常大的Excel文件,使用XSSFWorkbook
可能会导致内存不足的问题。POI提供了SXSSFWorkbook
类来处理大文件,它采用流式处理方式,只将部分数据加载到内存中。
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.IOException;
public class ReadLargeExcel {
public static void main(String[] args) {
try (FileInputStream file = new FileInputStream("large_example.xlsx")) {
Workbook workbook = new SXSSFWorkbook(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;
case FORMULA:
System.out.print(cell.getCellFormula() + "\t");
break;
default:
System.out.print("UNKNOWN\t");
}
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
通过Apache POI库,Java开发者可以轻松地读取Excel文件中的数据。本文介绍了如何使用POI库读取.xls
和.xlsx
文件,并处理不同类型的数据。对于大文件,可以使用SXSSFWorkbook
来避免内存不足的问题。希望本文能帮助你更好地理解和使用Apache POI库来处理Excel文件。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。