简体   繁体   English

如何使用Apache POI获取java中各行excel表的最后一列值

[英]how to get last column value of respective row of excel sheet in java using Apache POI

i have following excel sheet 我有以下excel表

id     JobId     Name      Dept        Add       Salary
101      41      Bob     Editing    New York     $ 5000 

i am passing id,jobid as request parameter to servlet from $.ajax() method. 我从$ .ajax()方法将id,jobid作为请求参数传递给servlet。 i want to get salary cell data of excel sheet by matching request parameters ie id and jobid with excel sheet contents and insert it other table. 我想通过匹配请求参数(即id和jobid与excel表单内容)获取excel表的工资单元数据,并将其插入其他表。 How to do this? 这个怎么做?

here is my code... 这是我的代码......

   FileInputStream file = new FileInputStream(fileToBeRead);
   workbook = new HSSFWorkbook(file);
   int jobid = Integer.ParseInt(request.getParameter("jobid"));

          if (id == 101) {
             // get first sheet 
               HSSFSheet sheet = workbook.getSheetAt(0);
               Iterator<Row> rowIterator = sheet.iterator();
               statement = conn.createStatement() ;
               ResultSet resultset = statement.executeQuery("Select job_id,Job,Name, Deptname from Emp,Department where Department.job_id=" + jobid + " AND Emp.Id=" + id + " ");

                while (resultset.next()) { 

                   //how to match database result with excel records and save to other table



                }//resultset.next() ends here...

            resultset.close();
        }

i try to do like this......? 我试着这样做......? for eg but it gives error i am trying to match id=101 and Add=New York for eg and want to get salary data. 例如但它给出错误我试图匹配id = 101和Add =纽约为例如并且想得到薪水数据。 if use if(data[i][j].equalsIgnoreCase("101")) condition it works but i want to match both id and Address ie if(data[i][j].equalsIgnoreCase("101") && data[i][j+5].equalsIgnoreCase("New York")) it gives error 如果使用if(data [i] [j] .equalsIgnoreCase(“101”))条件它可以工作,但我想匹配id和地址,即if(data [i] [j] .equalsIgnoreCase(“101”)&& data [i] [j + 5] .equalsIgnoreCase(“纽约”))它给出错误

    int rowNum = sheet.getLastRowNum() + 1;
    int colNum = sheet.getRow(0).getLastCellNum();
    String[][] data = new String[rowNum][colNum];
   // System.out.println("Row No :"+rowNum +" \nCol no:"+colNum);

    for (int i = 1; i < rowNum; i++) {
        Row row = sheet.getRow(i);
        for (int j = 0; j < colNum; j++) {
            Cell cell = row.getCell(j);
            String value = null;
            double price;
           // int type = cell.getCellType();

            value = cell.getStringCellValue();

                    data[i][j] = value;
                    if(data[i][j].equalsIgnoreCase("101") && data[i][j+4].equalsIgnoreCase("New York"))
                        {
                            Cell lastCellInRow = row.getCell(row.getLastCellNum() - 1);
                            System.out.println(lastCellInRow.getStringCellValue());

                    }

        }
    }

If you're simply trying to get access to the last cell in a row, and you have a Row , try the following: 如果你只是想以访问一排最后一个单元格,和你有一个Row ,请尝试以下操作:

Cell lastCellInRow = row.getCell(row.getLastCellNum() - 1); // -1 because #s are 0-based

EDIT To address your updated question and the comments below. 编辑以解决您更新的问题和以下评论。 This code should do what you're looking for without going through the rigmarole of converting the entire spreadsheet into a 2D array, which is frankly terribly inefficient when you have access to all the data right at your fingertips. 这段代码应该可以满足您的需求,而无需经历将整个电子表格转换为2D数组的严格要求,当您可以轻松访问所有数据时,这非常低效。

for (int i = 1; i < rowNum; i++) {
  Row row = sheet.getRow(i);
  Cell cell1 = row.getCell(0);
  Cell cell2 = row.getCell(4);
  String id = cell1.getStringCellValue();
  String city = cell2.getStringCellValue();

  if(id.equalsIgnoreCase("101") && city.equalsIgnoreCase("New York")) {
    Cell lastCellInRow = row.getCell(row.getLastCellNum() - 1);
    System.out.println(lastCellInRow.getStringCellValue());
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM