5

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?

0

3 Answers 3

7

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();
Sign up to request clarification or add additional context in comments.

3 Comments

This is not working. it gives an error : Could not find 'internal references' EXTERNALBOOK
@TheGuest Make sure you're using the latest version of Apache POI, and make sure your file isn't corrupt!
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.
4
//we can create any number of sheets for single workbook
HSSFSheet sheet1= workBook.createSheet("sheet1");
HSSFSheet sheet2= workBook.createSheet("sheet2");
HSSFSheet sheet3= workBook.createSheet("sheet3");

Comments

4
HSSFSheet sheet1= workBook.createSheet("sheet1");
HSSFSheet sheet2= workBook.createSheet("sheet2");
HSSFSheet sheet3= workBook.createSheet("sheet3");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.