XSSFWorkbook() 获取表头第一列和最后一列
时间: 2024-01-15 10:40:57 浏览: 82
可以使用以下代码获取表头第一列和最后一列:
```java
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream("workbook.xlsx"));
XSSFSheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
XSSFRow headerRow = sheet.getRow(0); // 获取表头行
int firstColumnIndex = headerRow.getFirstCellNum(); // 获取第一列的索引
int lastColumnIndex = headerRow.getLastCellNum() - 1; // 获取最后一列的索引
XSSFCell firstCell = headerRow.getCell(firstColumnIndex); // 获取第一列的单元格
XSSFCell lastCell = headerRow.getCell(lastColumnIndex); // 获取最后一列的单元格
String firstColumn = firstCell.getStringCellValue(); // 获取第一列的值
String lastColumn = lastCell.getStringCellValue(); // 获取最后一列的值
```
这里假设表头是第一行,第一列的索引从0开始,最后一列的索引是列数减1。如果表头不是第一行,可以将`getFirstRowNum()`和`getLastRowNum()`方法的参数修改为表头所在的行号。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)