使用 Java 更新 Excel

我正在尝试更新 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(); 


慕田峪9158850
浏览 119回答 3
3回答

墨色风雨

行和列均以“i”键关闭,因此您将沿对角线遍历纸张。但当然也要去做其他人推荐的事情,他们都是很好的建议。通常,在处理二维信息块时,我发现先对行进行一个循环,然后在该循环中嵌套一个对列的循环(反之亦然),这很有用。例如for (y = startRow; y <= maxRow; y++) {&nbsp; ....&nbsp; for (x = startCol; x <= maxCol; x++) {&nbsp; &nbsp; &nbsp;.... // do something to each column in the current row&nbsp; &nbsp; &nbsp;cell = sheet1.getRow(y).getCell(x);&nbsp; &nbsp; &nbsp;.....

守候你守候我

好的,有几件事。HSSFSheet sheet1 = workSheet.getSheetAt(0);在循环之外声明。for 循环的每次迭代都使用同一张工作表,因此只需调用一次。您获取单元格数据 (&nbsp;String data = sheet1.getRow(i+1).getCell(i).toString();) 的逻辑不会返回相同的列。在第一次迭代中,您将得到 (R)ow 1 : (C)olumn 0。下一次迭代将返回 R2 : C1,然后是 R2:C2,然后是 R3:C3,等等。注意您对角线的模式沿着列向下,而不是垂直。行从 0 开始。您只需workSheet.write(output_file);在完成所有处理后即可。如果该行不存在,您将得到一个NullPointerException当您每次迭代都使用一个唯一的Cell时,您只需在循环中声明它(因此不需要在Cell cell = null;循环之外)。这是一个例子:try {&nbsp; FileInputStream fis = new FileInputStream(excelPath);&nbsp; Workbook workbook = new HSSFWorkbook(fis);&nbsp; Sheet sheet = workbook.getSheetAt(0);&nbsp; for (int i = 0; i < 5; i++) {&nbsp; &nbsp; Row row = sheet.getRow(i);&nbsp; &nbsp; if (row != null) {&nbsp; &nbsp; &nbsp; Cell cell = row.getCell(0);&nbsp; &nbsp; &nbsp; cell.setCellValue("Updated cell.");&nbsp; &nbsp; }&nbsp; }&nbsp; FileOutputStream output_file =&nbsp; new FileOutputStream(excelPath);&nbsp; workbook.write(output_file);&nbsp; output_file.close();&nbsp; fis.close();} catch (Exception e) {&nbsp; e.printStackTrace();}

萧十郎

我认为在 for 循环内移动 Cell 引用应该可以解决这个问题。细胞细胞=空;此外,如果您在 else 块中遇到 NullPointerException,您可能还需要移到 if 块之外单元格=sheet1.getRow(i+1).getCell(i+2)像这样的东西...HSSFSheet sheet1 = workSheet.getSheetAt(0);for (int i = 0; i < TCID.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String data = sheet1.getRow(i+1).getCell(0).toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cell cell = sheet1.getRow(i+1).getCell(2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(data.equals(TCID.get(i))){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cell.setCellValue(YES);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cell.setCellValue(NO);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; workSheet.write(output_file)&nbsp; &nbsp; &nbsp; &nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java