views:

23

answers:

2

Hello

I am using apache poi for writing into .xlsx file.I can write into .xlsx file but I am unable to append new content.How can I append new content in the .xlsx file

Thanks in advance

My Code is

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) {
    }
}
A: 

You should open the existing file instead of creating a new one if you want to append, see also this stackoverflow question:

http://stackoverflow.com/questions/521274/edit-existing-excel-files-using-jxl-api-apache-poi

Marc van Kempen
A: 

The first thing U've to do :

When you're working with Excel 2007 format, its more wise to use XSSF-Implementations, becoz the implementation that you've used are abstract implementations. Always remember this when using any implementations.

To append to an existing file you've to reach the end of the rows in the particular excel. This can be achieved by

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

After that you can create new cells with the XSSF- Implementation classes. For a glance just refer this page

venJava