繁体   English   中英

我正在尝试将一张纸的第一行与另一张纸的第一行进行匹配并检索匹配的数据

[英]I am trying to match first row of one sheet against first row of another sheet and retrieving matching data

我正在尝试将一张纸的第一行与另一张纸的第一行匹配并检索另一张纸的数据的 rest

我已经像这样存储了列表中第一行的数据

    int rowCount=sheet.getPhysicalNumberOfRows();
    List<String> li=new ArrayList<String>();
    for (int x=0; x<rowCount; x++) {
        li.add(sheet.getRow(x).getCell(0).getStringCellValue());
    }

但是,下面的代码无法将第一行的数据与另一张表的第一行的数据进行比较

    sheet=workbook.getSheetAt(1);
    rowCount=sheet.getPhysicalNumberOfRows();
    for (int x=0; x<rowCount; x++) {
        System.out.println(sheet.getRow(x).getCell(0).getStringCellValue());//------------
        if (li.get(x).equalsIgnoreCase(sheet.getRow(x).getCell(0).getStringCellValue())) {
            //retrieve the data of matching row
        }

请在这里帮忙

编辑:我已经成功了,但我得到了 NullPointerException

    for (int y=0; y<rowCount; y++) {
            System.out.println(li.get(y));
            for (int z=0; z<rowCount; z++) {
                if (li.get(y).equalsIgnoreCase(sheet.getRow(z).getCell(0).getStringCellValue())) {
                    XSSFRow row=sheet.getRow(z);
                    for (int x=1; x<=row.getLastCellNum(); x++) {
                        System.out.println(row.getCell(x).getStringCellValue());
                        System.out.println("found");
//                      break;
                    }
                }

            }
        }

我在您的最后一个代码块中看到两个问题:

  1. x <= row.getLastCellNum()getLastCellNum()返回此行中包含的最后一个单元格的索引加一,因此它必须是x < row.getLastCellNum() 见这里: POI:getLastCellNum() 这解释了NullPointerException的一个来源。
  2. 每当您调用row.getCell(cellIndex)时,您需要在调用getStringCellValue()之前检查单元格是否不是 null。 事实上,您还应该在调用getStringCellValue()或调用toString()之前检查单元格类型。

因此,假设所有行中的 cell(0) 都包含一个字符串,您的代码可能如下所示:

    public static void main(String[] args) throws Exception {
        OPCPackage pkg = OPCPackage.open(new File("src/main/resources/sheets.xlsx"));
        XSSFWorkbook workbook = new XSSFWorkbook(pkg);
        XSSFSheet sheet = workbook.getSheetAt(0);
        int rowCount=sheet.getPhysicalNumberOfRows();
        List<String> li=new ArrayList<String>();
        for (int x=0; x<rowCount; x++) {
            li.add(sheet.getRow(x).getCell(0).toString());
        }
            
        for (int y=0; y<rowCount; y++) {
            System.out.println("Sheet1: " + li.get(y));
            for (int z=0; z<rowCount; z++) {
                if (li.get(y).equalsIgnoreCase(sheet.getRow(z).getCell(0).toString())) {
                    XSSFRow row=sheet.getRow(z);
                    for (int x=1; x < row.getLastCellNum(); x++) {
                        XSSFCell cell = row.getCell(x);
                        if (cell != null) {
                            System.out.println(row.getCell(x).toString());
                        } else {
                            System.out.println("<empty>");
                        }
                    }
                }
           }
        }
        workbook.close();
    }

暂无
暂无

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

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