Page 1 of 1

How to create xlsx file from template

PostPosted: Sat Oct 04, 2014 12:17 am
by grid
If I have an xlsx file with header (there are complex headers with merged cols) can I use it as template in xlsx4j? Is there instructions/docs somewhere describing how to do this?
I need do upload document header from existing file and then add some rows to it with xlsx4j

Re: How to create xlsx file from template

PostPosted: Sat Oct 04, 2014 12:18 pm
by jason
Yes, start with your existing xlsx.

You can clone it if necessary; OpcPackage (which SpreadsheetMLPackage extends) contains a clone() method.

Re: How to create xlsx file from template

PostPosted: Mon Oct 06, 2014 4:37 pm
by grid
Thanks for your fast reply!
My xlsx document (that I want to use as a template) is not a java object, it's a plain *.xlsx file in filesystem, and I need to get it's representation in my project to modify it and save as another file. As I understand it, method clone() is not suitable fot it. I found this way:

Code: Select all
SpreadsheetMLPackage opcPackagepkg = SpreadsheetMLPackage.load(new File("myXlsxFile.xlsx"));
WorksheetPart sheet = (WorksheetPart) opcPackagepkg.getParts().get(new PartName("/xl/worksheets/sheet1.xml"));
Worksheet sheetfinal = sheet.getJaxbElement();
SheetData sheetData = sheetfinal.getSheetData();


And then add values to concrete rows:

Code: Select all
    sheetData.getRow().get(3).getC().add(1, createCell("12"));
    sheetData.getRow().get(3).getC().add(2, createCell("235gf2"));
    sheetData.getRow().get(3).getC().add(3, createCell("gf2"));
    sheetData.getRow().get(3).getC().add(4, createCell("2gf2"));
    sheetData.getRow().get(3).getC().add(5, createCell("2gjf2"));
    sheetData.getRow().get(3).getC().add(6, createCell("2gf2"));
    sheetData.getRow().get(3).getC().add(7, createCell("2gffh2"));
    sheetData.getRow().get(3).getC().add(8, createCell("2gf2"));


createCell() is a method from example:
https://github.com/plutext/docx4j/blob/ ... sheet.java

And then I add footer:
Code: Select all
    sheetData.getRow().get(4).getC().addAll(0, getFooter());
    sheetData.getRow().get(4).setHt(250.0);
    sheetData.getRow().get(4).setCustomHeight(Boolean.TRUE);


Footer is allways the same but I don't know how much rows will be there before it.

I don't undestand why if I set row heigth this way

Code: Select all
    Row row3 = Context.getsmlObjectFactory().createRow();
    row3.getC().addAll(getFooter());
    row3.setHt(250.0);
    row3.setCustomHeight(Boolean.TRUE);
    sheetData.getRow().add(row3);


It sets heigth of previos row and I need to do this:

Code: Select all
    Row row3 = Context.getsmlObjectFactory().createRow();
    row3.getC().addAll(getFooter());
    sheetData.getRow().add(row3);

    Row row4 = Context.getsmlObjectFactory().createRow();
    row4.setHt(250.0);
    row4.setCustomHeight(Boolean.TRUE);
    sheetData.getRow().add(row4);