I am able to save the exported spreadsheet with all template variables replaced, and have been able to duplicate individual worksheets in the document using the XmlUtils.deepCopy() method.
I would like to duplicate the entire SpreadsheetMLPackage object in memory. I need to generate multiple spreadsheet files and would rather generate them from an in-memory template rather than load the template file using SpreadsheetMLPackage.load() repeatedly.
Does someone have code that does this? I've tried to use XmlUtils.deepCopy() to clone the Workbook, but the following code generates a corrupt file. I understand there is more that needs to be done as far as copying styles, properties, etc... and was hoping someone already went through this step. If you don't have the code to duplicate the workbook, I would appreciate a list of things that need to be copied.
Thanks.
- Code: Select all
def inputFilePath = "src/POWorksheetTemplate.xlsx"
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.load(new File(inputFilePath))
SpreadsheetMLPackage newPkg = new SpreadsheetMLPackage()
newPkg.wb = new WorkbookPart()
def clonedWb = XmlUtils.deepCopy(pkg.wb.getJaxbElement(), pkg.wb.getJAXBContext())
newPkg.wb.setJaxbElement(clonedWb)
newPkg.addTargetPart(newPkg.wb);
def outputFilePath = "target/test.xlsx";
def outputFile = new File(outputFilePath)
newPkg.save(outputFile)