Page 1 of 1

Adding Style Information Crashes Excel

PostPosted: Thu Mar 10, 2011 2:38 am
by arimmer
I am using xslx4j to create a simple spreadsheet - but when I try to add style information then try to open the file with Excel 2007 it crashes the application!

Has anyone successfully set style information for cells?

If I just add a 'styles' part with an empty StyleSheet (as below) Excel complains but opens the file (after removing the Style part)

Code: Select all
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();

      WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
    SharedStrings sharedStrings = new SharedStrings(new PartName("/xl/sharedStrings.xml"));
    sharedStrings.setJaxbElement( new CTSst() );
    Styles styles = new Styles(new PartName("/xl/styles.xml"));
    CTStylesheet styleSheet = new CTStylesheet();
    styles.setJaxbElement( styleSheet );

addContent(sheet, sharedStrings, styles);

    Parts parts = pkg.getParts();
    Part workBook = parts.get( new PartName("/xl/workbook.xml") );
    workBook.addTargetPart( sharedStrings );
    workBook.addTargetPart( styles );

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


But if I start putting anything into the Stylesheet then Excel crashes:
Code: Select all
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();

      WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
    SharedStrings sharedStrings = new SharedStrings(new PartName("/xl/sharedStrings.xml"));
    sharedStrings.setJaxbElement( new CTSst() );
    Styles styles = new Styles(new PartName("/xl/styles.xml"));
    CTStylesheet styleSheet = new CTStylesheet();
    styleSheet.setCellXfs( new CTCellXfs() );
    styleSheet.getCellXfs().setCount( 1L );
    styleSheet.getCellXfs().getXf().add( new CTXf() );
    styleSheet.setCellStyles( new CTCellStyles() );
    styleSheet.getCellStyles().setCount( 1L );
    styleSheet.getCellStyles().getCellStyle().add( new CTCellStyle() );
    styleSheet.setCellStyleXfs( new CTCellStyleXfs() );
    styleSheet.getCellStyleXfs().setCount( 1L );
    styleSheet.getCellStyleXfs().getXf().add( new CTXf() );   
    styles.setJaxbElement( styleSheet );

      addContent(sheet, sharedStrings, styles);

    Parts parts = pkg.getParts();
    Part workBook = parts.get( new PartName("/xl/workbook.xml") );
    workBook.addTargetPart( sharedStrings );
    workBook.addTargetPart( styles );

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


I expect I'm missing something really obvious but I just can't find it myself. Any help or advice where to look/what to do would be greatly appreciated.

Re: Adding Style Information Crashes Excel

PostPosted: Thu Mar 10, 2011 8:33 am
by jason
A new xlsx I created in Excel 2007 contains the following styles.xml:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  <fonts count="1">
    <font>
      <sz val="11"/>
      <color theme="1"/>
      <name val="Calibri"/>
      <family val="2"/>
      <scheme val="minor"/>
    </font>
  </fonts>
  <fills count="2">
    <fill>
      <patternFill patternType="none"/>
    </fill>
    <fill>
      <patternFill patternType="gray125"/>
    </fill>
  </fills>
  <borders count="1">
    <border>
      <left/>
      <right/>
      <top/>
      <bottom/>
      <diagonal/>
    </border>
  </borders>
  <cellStyleXfs count="1">
    <xf numFmtId="0" fontId="0" fillId="0" borderId="0"/>
  </cellStyleXfs>
  <cellXfs count="1">
    <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
  </cellXfs>
  <cellStyles count="1">
    <cellStyle name="Normal" xfId="0" builtinId="0"/>
  </cellStyles>
  <dxfs count="0"/>
  <tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleLight16"/>
</styleSheet>
 
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


Using the "Good" cell style adds another font, another fill, another cellStyle another cellXfs and another cellStyleXfs:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  <fonts count="2">
    :
    <font>
      <sz val="11"/>
      <color rgb="FF006100"/>
      <name val="Calibri"/>
      <family val="2"/>
      <scheme val="minor"/>
    </font>
  </fonts>
  <fills count="3">
:
    <fill>
      <patternFill patternType="solid">
        <fgColor rgb="FFC6EFCE"/>
      </patternFill>
    </fill>
  </fills>
  <cellStyleXfs count="2">
:
    <xf numFmtId="0" fontId="1" fillId="2" borderId="0" applyNumberFormat="0" applyBorder="0" applyAlignment="0" applyProtection="0"/>
  </cellStyleXfs>
  <cellXfs count="2">
:
    <xf numFmtId="0" fontId="1" fillId="2" borderId="0" xfId="1"/>
  </cellXfs>
  <cellStyles count="2">
    <cellStyle name="Good" xfId="1" builtinId="26"/>
  </cellStyles>
 
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


Check you've got the minimum required content.

You could read an example styles part in from a file to init it; the following snippet which you could adapt is from docx StyleDefinitionsPart:
Code: Select all
         is = org.docx4j.utils.ResourceUtils.getResource(
               "org/docx4j/openpackaging/parts/WordprocessingML/KnownStyles.xml");                  
         
         JAXBContext jc = Context.jc;
         Unmarshaller u = jc.createUnmarshaller();         
         u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler());

         org.docx4j.wml.Styles styles = (org.docx4j.wml.Styles)u.unmarshal( is );         


Also, if I run the PartsList sample on that xlsx, I get:
Code: Select all
Part /_rels/.rels [org.docx4j.openpackaging.parts.relationships.RelationshipsPart]  containing JaxbElement:org.docx4j.relationships.Relationships
    Part /docProps/app.xml [org.docx4j.openpackaging.parts.DocPropsExtendedPart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties containing JaxbElement:org.docx4j.docProps.extended.Properties
    Part /docProps/core.xml [org.docx4j.openpackaging.parts.DocPropsCorePart] http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties containing JaxbElement:org.docx4j.docProps.core.CoreProperties
    Part /xl/workbook.xml [org.docx4j.openpackaging.parts.SpreadsheetML.WorkbookPart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument containing JaxbElement:org.xlsx4j.sml.Workbook
        Part /xl/worksheets/sheet3.xml [org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet containing JaxbElement:org.xlsx4j.sml.Worksheet
        Part /xl/worksheets/sheet2.xml [org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet containing JaxbElement:org.xlsx4j.sml.Worksheet
        Part /xl/worksheets/sheet1.xml [org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet containing JaxbElement:org.xlsx4j.sml.Worksheet
        Part /xl/sharedStrings.xml [org.docx4j.openpackaging.parts.SpreadsheetML.SharedStrings] http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings containing JaxbElement:org.xlsx4j.sml.CTSst
        Part /xl/styles.xml [org.docx4j.openpackaging.parts.SpreadsheetML.Styles] http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles containing JaxbElement:org.xlsx4j.sml.CTStylesheet
        Part /xl/theme/theme1.xml [org.docx4j.openpackaging.parts.ThemePart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme containing JaxbElement:org.docx4j.dml.Theme


Are you attaching (addTargetPart) your styles part to workbook.xml (consistent with the above structure)?

Re: Adding Style Information Crashes Excel

PostPosted: Fri Mar 11, 2011 5:53 am
by arimmer
Yes I was doing the 'addTarget' part.

Your mention of 'minimum required content' sent me off in the right direction.

Seems, that despite the XSD suggesting all stylesheet contents are optional, if you have any contents you need to have at least one Font, Border and Fill.

When I added these Excel happily opened my spreadsheet with wrapped text.

Thanks very much for your help.