【问题标题】:Compare resultset column with a excel column and if the values match, write data to adjacent column将结果集列与 excel 列进行比较,如果值匹配,则将数据写入相邻列
【发布时间】:2016-06-04 05:41:33
【问题描述】:

我有从 sql 数据库中获取数据(两列)的 java 程序。我有一个包含三列的 excel 文件。 excel文件的第二列和结果集的第一列已经进行了比较。如果它们匹配,我想将数据集第二列的值写入 excel 文件的第四列。到目前为止我有这个代码。问题是,这只检查第一个单元格而不移动到下一个单元格。请帮忙

   int row = 0;
   while(row < 50) {
       cell = sheet.getRow(row).getCell(1);
       String cellValue = cell.getStringCellValue();
       while (results.next()) {
           String name = results.getString("StationName");
           if(cellValue.equals(name)) {
               System.out.println(results.getString("StationName"));
               System.out.println("Strings matched and values written !!!! ");
               newCell = sheet.getRow(row).getCell(3);
               int Bal = results.getInt(2);
               newCell.setCellValue(Bal);
           } else {
               System.out.println(results.getString("StationName"));
               System.out.println("Strings not matched!!! ");
           }
       }
       row = row + 1;
   }

【问题讨论】:

    标签: java excel


    【解决方案1】:

    该网站不允许我发表评论所以这不是答案,而是一个提示。 结果集逐行迭代,您必须使用 while 循环,以便遍历行中的每个值 有点像这样:

    while(results.next())
        {
        String name=results.getString(<Name>)
        if(cellValue.equals(name)) {
            System.out.println(results.getString("StationName"));
            System.out.println("Strings matched and values written !!!! ");
            newCell = sheet.getRow(row).getCell(3);
            int Bal = results.getInt(2);
            newCell.setCellValue(Bal);
           }
      }   
    

    【讨论】:

    • 我的代码循环遍历结果集的“stationName”列(大约 100 行)并检查 B1 单元格是否与结果集的列匹配。问题是当完成检查 B1 时,它不会移动并检查 B2 单元格。
    • 一个建议:如果您创建循环块以从每一列获取数据,例如在第一个 while 循环中获取第 1 列数据,在第 2 个 while 循环中获取第 2 列,依此类推......如果你想对数据执行操作创建另一个函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多