2010-07-05 436 views
3

我使用Apache POI写入.xlsx文件。我可以写入.xlsx文件,但我无法附加新内容。我如何在.xlsx文件中添加新内容?通过java将数据追加到xlsx文件中

我的代码是:

public static void write(){ 
    try {   
     Workbook[] wbs = new Workbook[]{new XSSFWorkbook()}; 
     Workbook workbook=wbs[0]; 
     org.apache.poi.ss.usermodel.Sheet sheet = workbook.createSheet(); 
     System.out.println(sheet.getSheetName()); 
     Row row = sheet.createRow(2); 
     for(int i=0;i<10;i++){ 
       Cell cell=row.createCell(i); 
       cell.setCellValue("Sun System"); 
     } 
     FileOutputStream fout=new FileOutputStream("D:/Test.Xlsx"); 
     workbook.write(fout); 
     fout.close(); 
    } catch (Exception e) { 
    } 
} 
+1

什么样的内容?细胞?列?工作表?而关于*无法* - 由于错误/异常或因为您需要额外的知识? – 2010-07-05 08:26:45

回答

6

第一你需要做的事情:

当你使用Excel 2007格式时,使用XSSF-Implementations更明智,因为你已经使用了abstr行动实施。使用任何实现时请务必记住这一点。

要附加到现有文件,您需要到达该特定工作簿表中行的末尾。这可以通过以下方式实现:

int rows = sheet.getPhysicalNumberOfRows(); // or sheet.getLastRowNum(); 

之后,您可以使用XSSF-实现类创建新单元。欲了解更多信息,请参阅this page