I'm trying to create a new sheet in an existing excel workbook using apache POI for java but have been unsuccessful so far. Can anyone please tell me how it is done?
3 Answers
It's very easy. It's just like adding a new sheet to a new workbook, only you start with the existing workbook rather than a new one
Workbook wb = WorkbookFactory.create(new File("/path/to/existing"));
Sheet s = wb.createSheet();
// Do something with the new sheet
FileOutputStream out = new FileOutputStream("/path/to/new/version");
wb.write(out);
out.close();
3 Comments
The Guest
This is not working. it gives an error : Could not find 'internal references' EXTERNALBOOK
Gagravarr
@TheGuest Make sure you're using the latest version of Apache POI, and make sure your file isn't corrupt!
The Guest
1. I am using the latest poi. 2. The file is not corrupt, I am CREATING a new Workbook instance (instead of loading an existing workbook) and trying to copy a sheet from a workbook which already exists on the disk. I guess this is a bug in Apache POI especially when creating HSSF workbook. I tried with XSSF, but I get a different error. I am trying to copy a sheet with macro from the existing workbook to a workbook that I am creating.