文件IO是任何軟件進行的重要組成部分,我們在電腦上創建一個Excel文件,然后打開它修改一些東西或者刪除它。Java給我們提供了操縱文件的很多工具類,本文主要是使用POI操縱Excel文件。
1 介紹POI包的一些概念
- Workbook: 這就是一個工作Excel文件。XSSFWorkbook 和 HSSFWorkbook classes都實現這個接口
- XSSFWorkbook: 針對 XLSX 類型文件的一個實現.
- HSSFWorkbook: 針對 XLS 類型文件的一個實現.
- Sheet: 一個Excel的一個sheet頁。XSSFSheet and HSSFSheet 都實現這個接口。
- XSSFSheet: Sheet頁的一個針對 XLSX 類型文件的實現.
- HSSFSheet: Sheet頁的一個針對 XLS 類型文件的實現.
- Row: 一個sheet頁中的一行數據。
- Cell: 一個Excel最小框格,也就是確定行、確定列的一個框格。
2 具體實現
2.1 導入POM包
<!--POI 包-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.versin}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.versin}</version>
</dependency>
2.2 根據文件流創建一個Workbook
可以使用WorkbookFactory
自動根據Excel類型是XLSX還是XLS自動創建對應的Workbook
File file = new File(filePath + File.separator + fileName);
FileInputStream inputStream = new FileInputStream(file);
// 使用工廠模式 根據文件擴展名 創建對應的Workbook
Workbook workbook = WorkbookFactory.create(inputStream);
2.3 獲取確定sheet頁的數據
根據 sheet 頁的名字獲取 sheet 頁數據
Sheet sheet = workbook.getSheet(sheetName);
2.4 獲取總的數據行數
Sheet 類提供了獲取首行行號和最后一行行號的方法,可以根據這兩個方法獲取 sheet 頁中的數據行數。
int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
2.5 獲取行列數據封裝到Map中
如果只需要返回Map數據,到這里就可以返回結果
for (int i = 1; i < rowCount + 1; i++) {
Row row = sheet.getRow(i);
resultMap = new HashMap<>();
for (int j = 0; j < row.getLastCellNum(); j++) {
if(Objects.equals(row.getCell(j).getCellType(), CellType.STRING)) {
resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j).getStringCellValue());
} else if(Objects.equals(row.getCell(j).getCellType(), CellType.NUMERIC)) {
resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j).getNumericCellValue());
}else {
resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j));
}
}
jsonObject = new JSONObject(resultMap);
resultMapList.add(jsonObject.toJSONString());
}
2.6 將結果數據轉成List對象
使用 fasterxml.jackson
將Map結果數據轉成 List 對象
return JsonUtil.ofList(resultMapList.toString(), tClass);
代碼如下
/**
* 獲取Excel,將數據轉換成 List<T> 的形式
* Excel 數據要求第一行為對象的屬性名稱
*
* @param filePath 文件路徑
* @param fileName 文件名稱
* @param sheetName sheet名稱
* @param tClass 要轉換成的實體類
* @param <T>
* @return List對象數組
* @throws IOException
*/
public static <T> List<T> readExcelOfList(String filePath, String fileName, String sheetName, Class<T> tClass) throws IOException {
List<String> resultMapList = new ArrayList<>();
File file = new File(filePath + File.separator + fileName);
FileInputStream inputStream = new FileInputStream(file);
// 使用工廠模式 根據文件擴展名 創建對應的Workbook
Workbook workbook = WorkbookFactory.create(inputStream);
Sheet sheet = workbook.getSheet(sheetName);
int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
JSONObject jsonObject;
Map<String, Object> resultMap;
for (int i = 1; i < rowCount + 1; i++) {
Row row = sheet.getRow(i);
resultMap = new HashMap<>();
for (int j = 0; j < row.getLastCellNum(); j++) {
if(Objects.equals(row.getCell(j).getCellType(), CellType.STRING)) {
resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j).getStringCellValue());
} else if(Objects.equals(row.getCell(j).getCellType(), CellType.NUMERIC)) {
resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j).getNumericCellValue());
}else {
resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j));
}
}
jsonObject = new JSONObject(resultMap);
resultMapList.add(jsonObject.toJSONString());
}
return JsonUtil.ofList(resultMapList.toString(), tClass);
}
源代碼:https://github.com/prepared48/dataProcess-tools.git
參考鏈接:https://www.guru99.com/all-about-excel-in-selenium-poi-jxl.html