gpt4 book ai didi

java - 如何使用 apache POI 从 Excel 文件中的特定位置获取结果

转载 作者:太空宇宙 更新时间:2023-11-04 14:43:59 24 4
gpt4 key购买 nike

我有一个 excel 文件,其中包含各种信息,还包含一个表结构,其中有两个标题单元代码和描述。所以现在我可以使用 POI 读取整个 excel 文件,但我的问题是我需要读取只有Excel中表格列的两个值。但我无法正确执行此操作,这是我到目前为止所做的代码。

public class ReadExcelFileToList {

public static void main(String[] args) throws IOException {

InputStream input = null;

try {

input = new FileInputStream(
"C:\\Users\\jeet.chatterjee\\Downloads\\unitUploadFormat.xlsx");

System.out.println("file is found");

XSSFWorkbook wb = new XSSFWorkbook(input);
XSSFSheet sheet = wb.getSheetAt(0);
int rowCount = 0;
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
XSSFRow row = (XSSFRow) rows.next();
System.out.println("\n");
if (rowCount == 3) {
Iterator cells = row.cellIterator();

while (cells.hasNext()) {

XSSFCell cell = (XSSFCell) cells.next();
if (XSSFCell.CELL_TYPE_STRING == cell.getCellType())
System.out.print(cell.getStringCellValue()
+ " ");

/*
* else if (XSSFCell.CELL_TYPE_STRING ==
* cell.getCellType())
* System.out.print(cell.getStringCellValue() +
* " "); else if (XSSFCell.CELL_TYPE_STRING ==
* cell.getCellType())
* System.out.print(cell.getStringCellValue() +
* " ");
*/

else
System.out.print("Unknown cell type");

}
rowCount++;
}
}

} catch (IOException ex) {

ex.printStackTrace();
} finally {
input.close();
}

}

}

我正在使用sheet.getLastRowNum()来获得我想要的结果,但我没有得到它,请有人帮忙。 here is my excel file 。这是我的 Excel 文件,我只想获取公斤和公斤值等,所有将放入其中的项目都将被提取。

最佳答案

 while (rows.hasNext()) {
XSSFRow row = (XSSFRow) rows.next();
if(rowCount==3){
Iterator cells = row.cellIterator();
while (cells.hasNext()) {
XSSFCell cell = (XSSFCell) cells.next();
System.out.print(cell.getStringCellValue()+"\t");
}
}
System.out.println();
rowCount++;
}

就像可怕的袋熊所说的那样,使用你自己的计数器来计算行数。

关于java - 如何使用 apache POI 从 Excel 文件中的特定位置获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24646555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com