需要操作的excel文件格式:excel文件内有多个sheet,每个sheet内有5列,每一列的第一行代表列头,用来描述当前列的作用 // java使用poi工具类,读取excel文件,能够读取上述excle文件内的所有内容并生成一个根据sheet的不同,存入不同的map,以sheet为key
时间: 2023-12-14 14:36:52 浏览: 29
好的,那我来给你写一份Java代码,使用POI工具类读取Excel文件,将每个sheet的内容存入不同的map中。
首先,你需要在项目中引入POI的依赖,例如:
```xml
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
```
接下来,你可以使用下面的代码来读取Excel文件:
```java
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
public class ExcelReader {
public static void main(String[] args) throws Exception {
String filePath = "path/to/your/excel/file.xlsx";
Map<String, Map<Integer, Map<String, String>>> dataMap = readExcel(filePath);
// TODO: 处理读取到的数据
}
public static Map<String, Map<Integer, Map<String, String>>> readExcel(String filePath) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream(new File(filePath)));
Map<String, Map<Integer, Map<String, String>>> dataMap = new HashMap<>();
int sheetCount = workbook.getNumberOfSheets();
for (int i = 0; i < sheetCount; i++) {
Sheet sheet = workbook.getSheetAt(i);
Map<Integer, Map<String, String>> sheetDataMap = new HashMap<>();
int rowCount = sheet.getPhysicalNumberOfRows();
Row headerRow = sheet.getRow(0);
int columnCount = headerRow.getLastCellNum();
for (int j = 1; j < rowCount; j++) {
Row row = sheet.getRow(j);
Map<String, String> rowDataMap = new HashMap<>();
for (int k = 0; k < columnCount; k++) {
Cell cell = row.getCell(k, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
String key = headerRow.getCell(k).getStringCellValue().trim();
String value = cell.getStringCellValue().trim();
rowDataMap.put(key, value);
}
sheetDataMap.put(j, rowDataMap);
}
dataMap.put(sheet.getSheetName(), sheetDataMap);
}
return dataMap;
}
}
```
这个代码会读取Excel文件中的每个sheet的内容,并将其存入一个名为`dataMap`的`Map`中。`dataMap`的结构是这样的:
```java
Map<String, Map<Integer, Map<String, String>>>
```
其中,`String`表示sheet的名称,`Map`表示每个sheet的数据,`Integer`表示当前行的索引,`Map`表示当前行的数据,键为列头,值为单元格的值。
如果有多个sheet,它们的数据会分别存储在不同的`Map`中,以sheet的名称为键。你可以根据需要对读取到的数据进行处理。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)