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