Page 1 of 1

Duplicating/Cloning a spreadsheet in memory

PostPosted: Sat Nov 17, 2012 3:42 am
by brad_c
I am building an export process for a client using xlsx4j, and am using a template xlsx file to generate the export.

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.


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())
      def outputFilePath = "target/test.xlsx";
      def outputFile = new File(outputFilePath)

Re: Duplicating/Cloning a spreadsheet in memory

PostPosted: Tue Nov 20, 2012 2:21 pm
by jason
OpcPackage contains a method clone(), since docx4j 2.7.2.

SpreadsheetMLPackage extends OpcPackage

hope this helps .. Jason

Re: Duplicating/Cloning a spreadsheet in memory

PostPosted: Wed Nov 21, 2012 1:56 am
by brad_c
Thanks Jason.

I guess that clone() would probably perform reasonably similar to a deep copy approach.

Keeping the disk IO out of the process is my main concern, so that should be fine.