1

我正在尝试更新 excel 工作表中的一列,但只有第一行正在更新。第二次迭代不起作用。有人可以帮我吗?下面是我正在尝试的代码。

    String excelPath = "path";

    String YES = "Updated YES";
    String NO = "Updated NO";
    try {

        FileInputStream fis= new FileInputStream(excelPath);


        HSSFWorkbook  workSheet = new HSSFWorkbook(fis);
        Cell cell = null;
        FileOutputStream output_file =new FileOutputStream(excelPath);

        for (int i = 0; i < TCID.size(); i++) {
            HSSFSheet sheet1 = workSheet.getSheetAt(0);
            String data = sheet1.getRow(i+1).getCell(i).toString();

            if(data.equals(TCID.get(i))){

                cell = sheet1.getRow(i+1).getCell(i+2);
                cell.setCellValue(YES);                 
                workSheet.write(output_file);
            }else {
                cell.setCellValue(NO);
                workSheet.write(output_file);
            }               

        }

        fis.close();
        output_file.close();
        workSheet.close();

    }catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

我的最新代码如下。现在它正在更新列,但最后一行没有更新。我错过了什么。

FileInputStream fis=new FileInputStream(excelPath);

        HSSFWorkbook  workSheet = new HSSFWorkbook(fis);
        HSSFSheet sheet1 = workSheet.getSheetAt(0);
        //FileOutputStream output_file =new FileOutputStream(excelPath);
        for (int i = 0; i < TCID.size(); i++) {

            String data = sheet1.getRow(i+1).getCell(0).toString();
            Cell cell = sheet1.getRow(i+1).getCell(2);
            if(data.equals(TCID.get(i))){
                cell.setCellValue(YES);                 
            }else {
                cell.setCellValue(NO);
            }               

        }

        fis.close();
        //output_file.close();
        //workSheet.close();
        FileOutputStream output_file =new FileOutputStream(excelPath);
        workSheet.write(output_file);
        output_file.close(); 
4

3 回答 3

1

好的,所以有几件事。

  1. HSSFSheet sheet1 = workSheet.getSheetAt(0);在循环之外声明。您在 for 循环的每次迭代中都使用同一张表,因此只需调用一次。
  2. 您获取单元格数据 ( String data = sheet1.getRow(i+1).getCell(i).toString();) 的逻辑不会返回相同的列。在您的第一次迭代中,您将获得 (R)ow 1 : (C)olumn 0。下一次迭代将返回 R2 : C1,然后是 R2:C2,然后是 R3:C3,等等。注意您正在对角线的模式在列下,而不是垂直。
  3. 从 0 开始。
  4. 您只需要在workSheet.write(output_file);完成所有处理后进行。
  5. 如果该不存在,您将获得一个NullPointerException
  6. 当您每次迭代都使用一个唯一的Cell时,您只需在循环中声明它(因此不需要在Cell cell = null;循环之外)。

这是一个例子:

try {
  FileInputStream fis = new FileInputStream(excelPath);
  Workbook workbook = new HSSFWorkbook(fis);
  Sheet sheet = workbook.getSheetAt(0);
  for (int i = 0; i < 5; i++) {
    Row row = sheet.getRow(i);
    if (row != null) {
      Cell cell = row.getCell(0);
      cell.setCellValue("Updated cell.");
    }
  }
  FileOutputStream output_file =  new FileOutputStream(excelPath);
  workbook.write(output_file);
  output_file.close();
  fis.close();
} catch (Exception e) {
  e.printStackTrace();
}
于 2019-10-28T07:37:55.210 回答
1

我认为在 for 循环中移动 Cell 引用应该可以为您修复它。

单元格 = null;

此外,您可能还需要移出 if-block 以防您在 else-block 中遇到 NullPointerException

单元格 = sheet1.getRow(i+1).getCell(i+2)

像这样的东西...

HSSFSheet sheet1 = workSheet.getSheetAt(0);
for (int i = 0; i < TCID.size(); i++) {
            String data = sheet1.getRow(i+1).getCell(0).toString();
            Cell cell = sheet1.getRow(i+1).getCell(2);
            if(data.equals(TCID.get(i))){
                cell.setCellValue(YES);                 
            }else {
                cell.setCellValue(NO);
            }
            workSheet.write(output_file)
       }
于 2019-10-28T06:37:56.433 回答
1

行和列都以“i”为键,因此您将沿对角线遍历工作表。

但当然也做其他人推荐的事情,他们都是很好的建议。

通常在处理二维信息块时,我发现有一个循环用于行,然后嵌套在该循环中用于列(反之亦然)

例如

for (y = startRow; y <= maxRow; y++) {

  ....

  for (x = startCol; x <= maxCol; x++) {


     .... // do something to each column in the current row

     cell = sheet1.getRow(y).getCell(x);

     .....
于 2019-10-28T06:47:25.867 回答