Page 1 of 1

Binding XML to an Excel with an XML Source

PostPosted: Wed Oct 21, 2020 8:27 pm
by ssandajan01
Hi All,

We are currently using the Docx4j for binding an XML into a word file with an XLM Source. Attached a code snippet for reference.
Code: Select all
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(templateStream);
            Docx4J.bind(wordMLPackage, xmlDocument, Docx4J.FLAG_BIND_INSERT_XML);


We are trying to achieve this same behavior with an Excel file. Attached a code snippet for reference
Code: Select all
            SpreadsheetMLPackage spreadsheetMLPackage = SpreadsheetMLPackage.load(file);
            Docx4J.bind(spreadsheetMLPackage, xmlDocument, Docx4J.FLAG_BIND_INSERT_XML);


But is shows an error when binding an xml to an excel file.

Can you provide a documentation or other forums that is related to this topic?

Also, is this scenario doable in the docx4j?


Regards,
Sandy

Re: Binding XML to an Excel with an XML Source

PostPosted: Thu Oct 22, 2020 6:13 am
by jason
Hi Sandy,

Docx4J.bind only supports binding in Word documents, not Excel.

But as you'd be aware, data binding in Excel documents is a thing: https://support.microsoft.com/en-us/off ... e9752a7d80

Parts of docx4j's implementation of data binding for docx could be re-used in adding support for xlsx, but it would still be in the order of one week of work. There is complexity in repeating elements (termed XML tables)

Re: Binding XML to an Excel with an XML Source

PostPosted: Thu Oct 22, 2020 12:54 pm
by ssandajan01
Hi Jason,

Thank you for the fast response.

I think I didn't clearly state our actual scenario, for it to be clear i'll try to elaborate more.

Pre-requisite:
    Excel File with an XML source (TEMPLATE)
    XML file (VALUES)
Scenario:
    Get the TEMPLATE
    Get the VALUES
    Bind the VALUES into the TEMPLATE
    Save the TEMPLATE with VALUES
Parts of docx4j's implementation of data binding for docx could be re-used in adding support for xlsx, but it would still be in the order of one week of work.

Based from the quote above,
    Does Docx4j have a documentation so that we could do it in our code?
    Should we just backtrack the process of Doc4xj.bind and check how to implement it into an excel file?
    Is there any other forum related to this scenario that i can check?
    Would you be adding a support for this kind of feature?

Regards,
Sandy

Re: Binding XML to an Excel with an XML Source

PostPosted: Mon Oct 26, 2020 10:24 am
by jason
I think the scenario you describe matches my understanding.

The Word and Excel implementations are similar in that they both use xpaths to point at the target data, so that much can be re-used.

But the rest is a bit different. (Excel stores most of the binding info in /xl/tables)

As mentioned previously, the complexity is in repeating elements (XML tables)

The code for binding docx with xml is at https://github.com/plutext/docx4j/tree/ ... atastorage but that code does go beyond a basic implementation, since it includes OpenDoPE conditionals, importing escaped XHTML etc.

I think this is the first time binding for xlsx has come up in the context of docx4j, so there's not much I can point you to, I'm afraid.

As far as adding support for this, we might consider doing it on a fee-for-service basis, but not otherwise in the near term. Feel free to email me if you'd like to discuss further.