Java 读取Excel标题和行值

Java 读取Excel标题和行值,java,excel,apache-poi,xssf,hssfworkbook,Java,Excel,Apache Poi,Xssf,Hssfworkbook,我需要阅读excel内容,其中有标题和N行。 根据列标题输入,需要在JAVA中提取行 我使用的java代码读取了完整的excel内容 //Create Workbook instance holding reference to .xlsx file XSSFWorkbook workbook = new XSSFWorkbook(file); //Get first/desired sheet from the workbook

我需要阅读excel内容,其中有标题和N行。 根据列标题输入,需要在JAVA中提取行

我使用的java代码读取了完整的excel内容

        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook(file);

        //Get first/desired sheet from the workbook
        XSSFSheet sheet = workbook.getSheetAt(0);
        int totalRows = sheet.getPhysicalNumberOfRows();


        //Iterate through each rows one by one
        Iterator<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext())
        {
            Row row = rowIterator.next();
            //For each row, iterate through all the columns
            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) 
            {
                Cell cell = cellIterator.next();
                //Check the cell type and format accordingly
                switch (cell.getCellType()) 
                {
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.print(cell.getNumericCellValue() + "\t");
                        break;
                    case Cell.CELL_TYPE_STRING:
                        System.out.print(cell.getStringCellValue() + "\t");
                        break;
                }
            }
            System.out.println("");
预期结果

if (Coln1==B)
{

Loop the list of B rows (here its 2 rows)
Coln1 Coln2 Coln3 Coln4 
B        11   xl    34
B        88   out   r45

if i need , r45 , How to pass the row cell to get the value?

}

有人能帮忙吗。谢谢

如果标题是第1行,也许您可以这样做

Row headerRow = sheet.getRow(0);   
您可以为索引定义一些常量。 e、 g

int n1ColIndex=0;//以0为基础。这是excel中行的索引。
int n2clindex=1;//
int n3ColIndex=2;
int n4ColIndex=3;
//然后可以逐行读取单元格。getCell()方法。
//在下面的代码中,您可以放入循环中。
XSSFCell n1Cell=row.getCell(n1ColIndex);
if(n1Cell==null){
继续;
}
字符串n1Col=n1Cell.getStringCellValue();
如果(“B”。等于(n1Col)){
//获取r45值。
XSSFCell n4Cell=row.getCell(n4ColIndex);
如果(n4Cell==null){
继续;
}
字符串val=n4Cell.getStringCellValue();
//该值将为34或r45
//你明白吗?
}
//更新1。怎么吵架。
//方法1。
迭代器rowIterator=sheet.Iterator();
while(roweiterator.hasNext())
{
行=行迭代器。下一步();
//对行变量执行一些操作。
}
//方法2。
对于(int index=0;index
API文件

全部手写,无需测试。
任何问题,请回答我。

非常感谢您的帮助。一个快速澄清,XSSFCell n1Cell=row.getCell(n1ColIndex);在这种语法中,行被引用。我可以知道我们需要如何申报这个。。。。这是工作表中的迭代器吗?请澄清。行表示一行表格。您可以使用
sheet.iterator()
获取一行,它在您的代码中。或者您可以使用
sheet.getRow(0),它在我的代码(标题部分)中。你也可以用“循环”来浏览表格。我会更新我的答案。你太棒了!非常感谢你!!这是我的荣幸。你能选择我作为答案吗,谢谢。。我再次感谢你!
Row headerRow = sheet.getRow(0);   
int n1ColIndex = 0;    // 0-based.    this is the index of row in excel.
int n2ColIndex = 1;    // 
int n3ColIndex = 2;
int n4ColIndex = 3;

// then you can read cell by Row.getCell() method.

// below code, you can put in loop.
XSSFCell n1Cell = row.getCell(n1ColIndex);
if ( n1Cell == null ){
    continue;
}

String n1Col = n1Cell.getStringCellValue();
if( "B".equals(n1Col) ) {
    // get r45 value.

    XSSFCell n4Cell = row.getCell(n4ColIndex);
    if ( n4Cell == null ){
        continue;
    }

    String val = n4Cell.getStringCellValue();
    // this value will be 34 or r45
    // Do you understand ?

}

// update 1. how to get a row.

// method 1.
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
    Row row = rowIterator.next();

    // do something with the row var.
}

// method 2.
for ( int index=0; index < sheet.getLastRowNum(); index++){
    Row row = sheet.getRow(index);

    // check null.
    if ( row == null ){
        continue;
    }

    // do something with the row var.

}