Page 1 of 1

Why is it overwriting my styles.xml part ?

PostPosted: Thu Apr 14, 2011 2:06 am
by Squ36
Hi, it's me again :D

I've come with another mystery for you Jason !

Here's my code :
Code: Select all
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
factory = Context.getsmlObjectFactory();

Styles styles = new Styles(new PartName("/xl/styles.xml"));
styles.setJaxbElement(createStyles());
pkg.addTargetPart(styles);

WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet 1", 1);
SheetData sheetData = sheet.getJaxbElement().getSheetData();

for(int i = 0; i < 5; i++)
{
    Row row = factory.createRow();
    for(int j = 0; j < 5; j++)
    {
        Cell cell = factory.createCell();
        if(i==0)
        {
            cell.setS(Long.valueOf(0)); //Style with id 0 is header cell
        }
        else
        {
            cell.setS(Long.valueOf(1)); //Style with id 1 is standard cell
        }
        cell.setV("value");
        row.getC().add(cell);
    }
    sheetData.getRow().add(row);
}           

SaveToZipFile saver = new SaveToZipFile(pkg);
saver.save(filepath);


For some reason, this gives me a perfectly good file but without styles.
When I look at the debug info in the console (sent as attachment), in the penultimate paragraph, there is this line bugging me :
Code: Select all
13.04.2011 16:07:31 *DEBUG* PartName: Trying to create part name /xl/styles.xml (PartName.java, line 151)

This line also appears in the 3rd paragraph, which, if I understood correctly, is around the moment where I add my own styles.xml file.
So, to sum up, I add my own styles, then xlsx4j says "What the hell, I don't like you, I'll make my own styles.xml and ignore yours !!!"

What's wrong doctor ???

Re: Why is it overwriting my styles.xml part ?

PostPosted: Thu Apr 14, 2011 9:34 am
by jason
Squ36 wrote:When I look at the debug info in the console (sent as attachment), in the penultimate paragraph, there is this line bugging me :

Code: Select all
13.04.2011 16:07:31 *DEBUG* PartName: Trying to create part name /xl/styles.xml (PartName.java, line 151)


This line also appears in the 3rd paragraph, which, if I understood correctly, is around the moment where I add my own styles.xml file.
So, to sum up, I add my own styles, then xlsx4j says "What the hell, I don't like you, I'll make my own styles.xml and ignore yours !!!"


Don't worry about that, parts are accessed via a PartName, and it is just making one in order to access your part.

Unzip your resulting xslx and you'll see your part :-)

If your styles aren't being honoured, that's another problem. To find out why, examine your xml (in the styles part and sheet1.xml), and compare it to a working example emitted by Excel.

cheers .. Jason

Re: Why is it overwriting my styles.xml part ?

PostPosted: Thu Apr 14, 2011 11:33 pm
by Squ36
Okay !

I believe I know why this doesn't work. The xl/_rels/workbook.xml.rels does not contain all the needed Relationships :
Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>
</Relationships>

versus an Excel file :
Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet3.xml"/>
  <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet2.xml"/>
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>
  <Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>
  <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
  <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
</Relationships>


I tried adding them into the workbook relationships :
Code: Select all
WorkbookPart wbp = (WorkbookPart) pkg.getParts().get(new PartName("/xl/workbook.xml"));

SharedStrings shared = new SharedStrings(new PartName("/xl/sharedStrings.xml"));
shared.setJaxbElement(buildSharedStrings());
wbp.addTargetPart(shared);
Styles styles = new Styles(new PartName("/xl/styles.xml"));
styles.setJaxbElement(createStyles());
wbp.addTargetPart(styles);

When I do that, the Excel file created is empty, but with styles working... (text wrapped, colored...)

Any idea of what I'm missing ?