tags:

views:

2322

answers:

3

Basic question: How do I load an Excel template for use with POI and then save it to an XLS file?

Edit:

The answer is:

FileInputStream inputStream = new FileInputStream(new File(templateFile));
Workbook workbook = new HSSFWorkbook(inputStream);

(Just load the template as a workbook and then write the workbook as an XLS file elsewhere.)

+2  A: 

Have you tried loading it up as a standard .xls using POI, amending it and then saving it ?

This is the approach I've used for inserting macros in a POI-generated .xls. I create the file with the macro (admittedly as an .xls) and then load it into my app, populate with data and save as a newly-created .xls. That all worked fine.

Brian Agnew
+1  A: 

You can directly load an .xls that will act as the template, and modify it.

POIFSFileSystem fs = new POIFSFileSystem(
             new FileInputStream("template.xls"));
HSSFWorkbook wb = new  HSSFWorkbook(fs, true);

Will load an xls, preserving its structure (macros included). You can then modify it,

HSSFSheet sheet1 = wb.getSheet("Data"); ...

and then save it.

FileOutputStream fileOut = new FileOutputStream("new.xls"); 
wb.write(fileOut);
fileOut.close();

Hope this helps

Guillaume

PATRY
A: 

Nice tutorial. It seems is gonna help me.

But...

I want to write some data (text) in a textField (object in my canvas, rectangle with text) into my excel template.

Can i do this with POI?

If I can... ¿How?

Welcome any help‼

Saludos

Bonifacio Velázquez