Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

append data into xlsx file through java

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?

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) {
    }
}
like image 400
Sunil Avatar asked Dec 28 '22 11:12

Sunil


1 Answers

The first thing U've to do :

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

To append to an existing file you need to reach the end of the rows in that particular workbook sheet. 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 more information refer to this page

like image 167
Venkat Avatar answered Jan 06 '23 06:01

Venkat