java读取excel文件某一列
时间: 2024-02-01 19:09:49 浏览: 40
以下是使用Java的POI库读取Excel文件某一列的示例代码:
```java
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
public class ReadExcelColumn {
public static void main(String[] args) {
String filePath = "path/to/your/excel/file.xlsx";
int columnIndex = 0; // 读取第一列,索引从0开始
try (FileInputStream fis = new FileInputStream(filePath);
Workbook workbook = new XSSFWorkbook(fis)) {
Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
int rowCount = sheet.getLastRowNum() + 1; // 获取行数
for (int i = 0; i < rowCount; i++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell(columnIndex);
if (cell != null) {
String cellValue = cell.getStringCellValue();
System.out.println("Cell value: " + cellValue);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
请注意,上述代码假设你使用的是Excel的xlsx格式文件,并且使用的是POI的XSSFWorkbook类。如果你使用的是xls格式文件,可以使用HSSFWorkbook类。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)