Java 尝试使用apache POI从excel工作表检索数据

Java 尝试使用apache POI从excel工作表检索数据,java,selenium-webdriver,apache-poi,Java,Selenium Webdriver,Apache Poi,我是Java编程新手,正在编写一个方法,使用ApachePOI从excel工作表中检索所有行。我创建了一个名为“ExcelDataProvider”的单独类,在该类中我调用该方法从工作表中提取值。我编写的方法在第一行运行良好,但并没有处理所有行。这是我写的代码 public static String getCellDataNew(String sheetName, String colName, int rowNum) { try { int col_Num = 0;

我是Java编程新手,正在编写一个方法,使用ApachePOI从excel工作表中检索所有行。我创建了一个名为“ExcelDataProvider”的单独类,在该类中我调用该方法从工作表中提取值。我编写的方法在第一行运行良好,但并没有处理所有行。这是我写的代码

public static String getCellDataNew(String sheetName, String colName, int rowNum) {
    try {
        int col_Num = 0;
        ExcelWSheet = ExcelWBook.getSheet(sheetName);
        Row = ExcelWSheet.getRow(0);
        int rowcount = ExcelWSheet.getLastRowNum();
        for (int i = 0; i < Row.getLastCellNum(); i++) {
            if (Row.getCell(i).getStringCellValue().trim().equals(colName.trim()))
                col_Num = i;
        }

        for (rowNum = 2; rowNum <= rowcount; rowNum++) {
            Row = ExcelWSheet.getRow(rowNum - 1);
            Cell = Row.getCell(col_Num);
        }

        if (Cell.getCellType() == CellType.STRING)
            return Cell.getStringCellValue();
        else if (Cell.getCellType() == CellType.NUMERIC || Cell.getCellType() == CellType.FORMULA) {
            String cellValue = String.valueOf(Cell.getNumericCellValue());
            return cellValue;
        } else if (Cell.getCellType() == CellType.BLANK)
            return "";
        else
            return String.valueOf(Cell.getBooleanCellValue());
    } catch (Exception e) {
        e.printStackTrace();
        return "row " + rowNum + " or column  does not exist  in Excel";
    }
}
这就是我的电子表格的样子

Testcase|   ClientName| Dealer| BranchCode| BranchName| AccountType|ProductSelection|   TaxResidence|   JointOwnership |    JointOwnershipType |    JointSignature| JointOwnerSelectionLastName|    JointOwnerSelectionFirstName


1   |Test, GR|  NBIN Introducing|   HOUS|   NBIN Intro| Non Registered| Cash |  Ontario|    Yes|    Joint WROS| Yes|    Test|   ABC


2   |Test, GR|  NBIN Introducing|   HOUS|   NBIN Intro| Non Registered| Cash |  Ontario|    Yes|    Joint WROS| Yes|    Test|   ABC

我想我看到了这个问题。下面的for循环不应到此结束。它还应覆盖下面的if else梯子

for (rowNum = 2; rowNum <= rowcount; rowNum++) {
        Row = ExcelWSheet.getRow(rowNum - 1);
        Cell = Row.getCell(col_Num);
    }

for(rowNum=2;rowNum我想我看到了这个问题。下面的for循环不应该到此结束。它还应该覆盖下面的if-else阶梯

for (rowNum = 2; rowNum <= rowcount; rowNum++) {
        Row = ExcelWSheet.getRow(rowNum - 1);
        Cell = Row.getCell(col_Num);
    }

for(rowNum=2;rowNum您可以使用下面的代码使用Apache POI库从excel工作表中读取所有行。我只是直接在main方法中编写。请相应地更改参数/路径

导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.util.Iterator;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
/**
*使用ApachePOI在Java中读取Excel文件的示例Java程序
*
*/
公共类ReadAllExcelRow{
公共静态void main(字符串[]args){
试一试{
文件excel=新文件(“C://temp/test.xlsx”);
FileInputStream fis=新的FileInputStream(excel);
XSSF工作簿=新XSSF工作簿(fis);
XSSFSheet sheet=book.getSheet(“Sheet1”);
迭代器rowIterator=sheet.Iterator();
while(roweiterator.hasNext()){
行=行迭代器。下一步();
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext()){
Cell=cellIterator.next();
开关(cell.getCellType()){
case Cell.Cell\u类型\u数值:
System.out.print(cell.getNumericCellValue()+“\t”);
打破
case Cell.Cell\u类型\u字符串:
System.out.print(cell.getStringCellValue()+“\t”);
打破
}
}
System.out.println(“”);
}
book.close();
}捕获(FileNotFoundException fe){
fe.printStackTrace();
}捕获(IOIE){
即printStackTrace();
}
}
}

您可以使用下面的代码使用Apache POI库读取excel工作表中的所有行。我只是直接在main方法中编写。请相应地更改参数/路径

导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.util.Iterator;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
/**
*使用ApachePOI在Java中读取Excel文件的示例Java程序
*
*/
公共类ReadAllExcelRow{
公共静态void main(字符串[]args){
试一试{
文件excel=新文件(“C://temp/test.xlsx”);
FileInputStream fis=新的FileInputStream(excel);
XSSF工作簿=新XSSF工作簿(fis);
XSSFSheet sheet=book.getSheet(“Sheet1”);
迭代器rowIterator=sheet.Iterator();
while(roweiterator.hasNext()){
行=行迭代器。下一步();
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext()){
Cell=cellIterator.next();
开关(cell.getCellType()){
case Cell.Cell\u类型\u数值:
System.out.print(cell.getNumericCellValue()+“\t”);
打破
case Cell.Cell\u类型\u字符串:
System.out.print(cell.getStringCellValue()+“\t”);
打破
}
}
System.out.println(“”);
}
book.close();
}捕获(FileNotFoundException fe){
fe.printStackTrace();
}捕获(IOIE){
即printStackTrace();
}
}
}

第一种可能的解决方案:

for (int rownum = 1 ; rownum < sheet.rowcount; ++rownum) {
   /* some code */
}
for(int-rownum=1;rownum
第二:
您可以尝试使用sheet.rowIterator第一种可能的解决方案:

for (int rownum = 1 ; rownum < sheet.rowcount; ++rownum) {
   /* some code */
}
for(int-rownum=1;rownum
第二:
您可以尝试使用sheet.rowIterator

我尝试过,但它不起作用。我在rowNum++上得到警告,这是一个“死代码”。我尝试过,但它不起作用。我在rowNum++上得到警告,这是一个“死代码”。
for (int rownum = 1 ; rownum < sheet.rowcount; ++rownum) {
   /* some code */
}