第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Java 更新 Excel

使用 Java 更新 Excel

慕田峪9158850 2024-01-05 16:20:03
我正在嘗試更新 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();    }}我的最新代碼如下?,F(xiàn)在它正在更新列,但最后一行沒有更新。我缺少什么。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(); 
查看完整描述

3 回答

?
墨色風雨

TA貢獻1853條經(jīng)驗 獲得超6個贊

行和列均以“i”鍵關(guān)閉,因此您將沿對角線遍歷紙張。


但當然也要去做其他人推薦的事情,他們都是很好的建議。


通常,在處理二維信息塊時,我發(fā)現(xiàn)先對行進行一個循環(huán),然后在該循環(huán)中嵌套一個對列的循環(huán)(反之亦然),這很有用。


例如


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);


     .....


查看完整回答
反對 回復 2024-01-05
?
守候你守候我

TA貢獻1802條經(jīng)驗 獲得超10個贊

好的,有幾件事。

  1. HSSFSheet sheet1 = workSheet.getSheetAt(0);在循環(huán)之外聲明。for 循環(huán)的每次迭代都使用同一張工作表,因此只需調(diào)用一次。

  2. 您獲取單元格數(shù)據(jù) ( 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時,您只需在循環(huán)中聲明它(因此不需要在Cell cell = null;循環(huán)之外)。

這是一個例子:

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();

}


查看完整回答
反對 回復 2024-01-05
?
蕭十郎

TA貢獻1815條經(jīng)驗 獲得超13個贊

我認為在 for 循環(huán)內(nèi)移動 Cell 引用應(yīng)該可以解決這個問題。


細胞細胞=空;


此外,如果您在 else 塊中遇到 NullPointerException,您可能還需要移到 if 塊之外


單元格=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)

       }


查看完整回答
反對 回復 2024-01-05
  • 3 回答
  • 0 關(guān)注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號