Search code examples
xmlexcelthemesapache-poixssf

Unable to simply create Theme in new ApachePOI XSSFWorkbook


Is it is possible to create empty Theme if I create XSSFWorkbook by constructor XSSFWorkbook()? I can create new Theme, but not completely. I can create two entries in files content_types.xml and /xl/_rels/workbook.xml.rels:

XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
POIXMLDocumentPart docThemePart = xssfWorkbook.createRelationship(XSSFRelation.THEME, XSSFFactory.getInstance(), 1);

Normally it’s not possible in ApachePOI, because it causes error. I had to MODIFY(!) ApachePOI ThemesTable class - I added constructor:

public ThemesTable() { super(); }

Then function above creates file /xl/theme/theme1.xml too, but it’s empty. How to fill this file? If I get Theme

ThemesTable theme = (ThemesTable) docThemePart;

and change it, content is not wrote to theme.xml. How to fill this file? I checked Zip creating. Method marshall() in ZipPartMarshaller class gets input stream from some workbook parts. And for Theme input stream is empty. Maybe that’s the problem? When I tried (for tests) to create other object:

xssfWorkbook.createRelationship(XSSFRelation.CHART, XSSFFactory.getInstance());

everything was good.

What to do now?


Solution

  • You'd need to override the commit method too

    Your simplest bet is to grab a recently nightly build (until 3.13 beta 2 is out), then use the StylesTable.ensureThemesTable() method to have an empty Themes Table added for you