2014-12-03 167 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++; 
}