2017-04-22 33 views
0

我不知道爲什麼我不能用setCellValue()寫2單元格值,它只更新x1單元格和x2單元格中的1個值。 我有一個文件excel,我希望更新這個文件的數據來自arraylist listThoaiThang,tt.getNoiBo()和tt.getNgoaiMang()是一個字符串,並且在日誌時不爲null。不能用excel文件中的Apache POI編寫連續的單元值

代碼如下,謝謝。

public void writeFile(ArrayList<LLThoaiNoiHat> llList) { 
    FileInputStream fis = null; 
    try { 
     DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); 
     String date1 = dateFormat.format(new Date()).toString(); 
     File tongHop = new File("Thongke_LL_NOIHAT_VTT_" + date1 + ".xlsx"); 
     copyFileUsingApacheCommonsIO(new File("Mau_Thongke_LL_NOIHAT_VTT.xlsx"), tongHop); //copy sample file before edit data 
     fis = new FileInputStream(tongHop); 
     BufferedInputStream bis = new BufferedInputStream(fis); 
     XSSFWorkbook workbook = new XSSFWorkbook(fis); 
     Sheet sheet = workbook.getSheetAt(0); 
     fis.close(); 
     int rowNum = 9; 
     Row row; 

     ArrayList<LLThoaiThang> listThoaiThang; 
     for (LLThoaiNoiHat tnh : llList) { 
      row = sheet.getRow(rowNum++); 
      int colNum = 2; 
      listThoaiThang =tnh.getListThoaiThang(); 
      for (LLThoaiThang tt :listThoaiThang) { 
       int aa = tt.getThang()/2+3; 
       Cell x1 = row.getCell(aa); 
       x1.setCellValue(tt.getNoiBo()); //only first cell ok 
       Cell x2 = row.getCell(aa+1); 
       x2.setCellValue(tt.getNgoaiMang());//this cell cannot change value and all cell after cannot change value 
      } 

     } 

     FileOutputStream outFile = new FileOutputStream(tongHop); 
     workbook.write(outFile); 
     outFile.close(); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 
+2

歡迎堆棧溢出!尋求調試幫助的問題(「爲什麼這個代碼不工作?」)必須在問題本身中包含所需的行爲,特定的問題或錯誤以及必要的最短代碼**。沒有明確問題陳述的問題對其他讀者無益。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –

回答

0

在這裏我發現的問題是,如果你設置了數字小值作爲字符串的細胞後,將不會更新,我認爲這是Apache POI的問題,所以答案是:

for (LLThoaiThang tt :listThoaiThang) { 
       int aa = tt.getThang()/2+3; 
       Cell x1 = row.getCell(aa); 
       if(!tt.getNoiBo().isEmpty()) 
        x1.setCellValue(Double.parseDouble(tt.getNoiBo())); //only this cell ok 
       Cell x2 = row.getCell(aa+1); 
       if(!tt.getNgoaiMang().isEmpty()) 
        x2.setCellValue(Double.parseDouble(tt.getNgoaiMang()));; 
} 
相關問題