2016-11-04 163 views
0

我使用Apache POI來生成單元格,但我注意到它似乎將所有單元格設置爲相同的值。在這種情況下,我不明白爲什麼。當我將我的工作表保存到文件中時,我可以看到單元格有值,但是在查看生成的文件時它是空的。任何幫助將不勝感激。Apache POI將單元格設置爲0

這裏是調試的屏幕時,我可以看到,某些細胞具有(在調試窗口,如「5」)的值:

enter image description here

這裏是我使用的代碼產生片:

@Override 
public void generate(TableRepresentation tableRepresentation) { 
    int rows = tableRepresentation.getRows(); 
    int columns = tableRepresentation.getColumns(); 
    final HSSFWorkbook workbook = new HSSFWorkbook(); 
    final HSSFSheet sheet = workbook.createSheet(); 
    for (int i = 0; i < rows; i++) { 

     final HSSFRow row = sheet.createRow(i); 
     for (int j = 0; j < columns; j++) { 
      row.createCell(j).setCellValue(tableRepresentation.get(i, j)); 
     } 

    } 

    try { 
     final FileOutputStream fos = new FileOutputStream("tjockis.xls"); 

     workbook.write(fos); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    System.out.println("tjockis.xls" + " written successfully"); 

} 

public interface TableRepresentation { 
    int getRows(); 

    int getColumns(); 

    String get(int i, int j); 
} 

編輯:

如何導出表如下

這裏是:

enter image description here

回答

0

如果你做任何變量作爲最終,你不能改變最終的變量的值(這將是不變)。

A final變量只能分配一次,其值不會改變,並且可以幫助避免編程錯誤。

一旦最終變量已分配,它始終包含相同的值。如果一個最後的變量持有對某個對象的引用,則該對象的狀態可能會通過對該對象的操作而改變,但該變量將始終引用相同的對象。

+0

那又有什麼問題?最終僅限於其定義的範圍。 –

+0

@why_vincent即使僅限於第一次分配值時不能更改的範圍。刪除所有最終關鍵字並嘗試。將最終關鍵字僅用於需要常量的地方 – Karthik

+0

1:查看調試窗口,單元格具有不同的值。如果他們沒有被分配到不同的值,那又怎麼會發生? 2:試過你說的話,但它不會改變任何東西。 –

相關問題