Question

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.)

Was it helpful?

Solution

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.

OTHER TIPS

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.

You can also use internal template as a resource.

InputStream fis = ChartSample.class.getResourceAsStream("/templates.xls");
HSSFWorkbook wb = new HSSFWorkbook(fis);        
fis.close();
HSSFSheet sh = wb.getSheetAt(0); 
//Here you go

And save that:

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

You can create a XLS file from a XLS template.

But, to do this, you need to create a copy of the template every time you need to use the template. If not, you will edit the original template (what you not want).

So, you need first get your template file:

URL url = Thread.currentThread().getContextClassLoader().getResource("templates/template.xls");
File file = new File(url.getPath());

Copy the template file:

try (FileOutputStream fileOutputStream = new FileOutputStream("/home/jake/fileCopiedFromTemplate.xls")) {

    Files.copy(file.toPath(), fileOutputStream);

    Workbook workbook = new HSSFWorkbook();
    workbook.write(fileOutputStream);
}

Access the new copied file:

FileInputStream inp = new FileInputStream("/home/jake/fileCopiedFromTemplate.xls");

Create a Workbook, so you can write in your new file:

Workbook workbook = WorkbookFactory.create(inp);

After write in your workbook:

try (FileOutputStream fileOut = new FileOutputStream("/home/jake/fileCopiedFromTemplate.xls")) {
    workbook.write(fileOut);
}

A tip to create a XLS template file is mark the template with some variable for you localize the position that you would like to fill. Like:

------------------------------------
|   | Columna A     | Column B     |
------------------------------------
| 1 |  Some description            | 
------------------------------------
| 2 | {person.name} | {person.age} |
------------------------------------

For excel-files with .xlsx use the following:

FileInputStream inputStream = new FileInputStream(new File("template.xlsx"));
        @SuppressWarnings("resource")
        Workbook wb = new XSSFWorkbook(inputStream);  
        Sheet sheet = wb.getSheet("sheet1");

You can use XSSF by adding poi-ooxml dependency in your maven pom.xml if you want to save it in 2007+ format.

If you have template.xlsx file with a summary sheet in this xmls and you want to change a particular cell you can do it as:

XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream("template.xlsx"));           
FileOutputStream fileOut = new FileOutputStream("new.xlsx");
XSSFSheet sheet1 = wb.getSheet("Summary");
XSSFRow row = sheet1.getRow(15);
XSSFCell cell = row.getCell(3);
cell.setCellValue("Bharthan");

wb.write(fileOut);
log.info("Written xls file");
fileOut.close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top