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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。