0
我使用Apache POI來生成單元格,但我注意到它似乎將所有單元格設置爲相同的值。在這種情況下,我不明白爲什麼。當我將我的工作表保存到文件中時,我可以看到單元格有值,但是在查看生成的文件時它是空的。任何幫助將不勝感激。Apache POI將單元格設置爲0
這裏是調試的屏幕時,我可以看到,某些細胞具有(在調試窗口,如「5」)的值:
這裏是我使用的代碼產生片:
@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);
}
編輯:
如何導出表如下這裏是:
那又有什麼問題?最終僅限於其定義的範圍。 –
@why_vincent即使僅限於第一次分配值時不能更改的範圍。刪除所有最終關鍵字並嘗試。將最終關鍵字僅用於需要常量的地方 – Karthik
1:查看調試窗口,單元格具有不同的值。如果他們沒有被分配到不同的值,那又怎麼會發生? 2:試過你說的話,但它不會改變任何東西。 –