2014-12-03 166 views
0

我試圖到Excel從Java應用程序中導出,這裏是我的代碼,但問題是隻有最後一列的值顯示爲顯示此圖像中所有其他列是空的鏈接http://i.imgur.com/mTjCYH3.jpg使用從Java導出後無值在Excel中的Apache POI

我使用POI-3.10.1。

請提出任何改變做我需要做。

公共類爲ExcelExport {

public static void main(String[] args) throws IOException { 

    FileOutputStream fos = null; 
    File file = null; 

    file = new File("D:/ExportExcel.xls"); 
    fos = new FileOutputStream(file); 

    Workbook workbook = new HSSFWorkbook(); 
    Sheet sheet = workbook.createSheet(); 
    Cell cell = sheet.createRow(0).createCell(0); 

    int row = 0; 
    while (row < 5) { 
     for (int column = 0; column < 5; column++) { 
      cell = sheet.createRow(row).createCell(column); 
      cell.setCellValue(row + "," + column); 
     } 
     row++; 
    } 
    workbook.write(fos); 
    fos.close(); 
} 

}

+2

'sheet.createRow(行)'創建一個新的空行。因此每個循環都會刪除該行中的所有值。將這個陳述移出'for'。 – BackSlash 2014-12-03 07:40:13

+0

@BackSlash非常感謝你 – rock 2014-12-03 07:44:15

回答

0

爲@BackSlash建議,我的代碼工作正常

int row = 0; 

while (row < 5) { 
    Row r = sheet.createRow(row); 
    for (int column = 0; column < 5; column++) { 
     cell = r.createCell(column); 
     cell.setCellValue(row + "," + column); 
    } 
    row++; 
}