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.