So far every example I was able to find here or elsewhere (like https://stackoverflow.com/a/72016283/13831836) was a barebones code that creates something that may or may not be opened by MS Word, knowing Microsoft's attitude towards enforcing standards, but is not a valid document. And by valid I mean:
- Libre Office opens it and displays more or less correctly (a must)
- Open Office opens it and displays more or less correctly (a must)
- MS Open XML Productivity Tool validates it (nice to have)
document in question is rather simple, a short paragraph followed by a large table (600+ rows) followed by another short paragraph.
I attempted to fix errors displayed by Productivity Toolkit on my own, alas it turned out to be too difficult without knowing much more about the SDK and the format than I do. Most of the errors have to do with invalid children of a table, a row, etc., or missing Styles and so on, some of them I could fix with the help of internet search, but not all.
I am not against using libraries built on top of The Open XML SDK provided the license is right, but again I wasn't able to find one that actually works yet.
If you want to support Libre Office, Open Office, and Word, then
Given: One has identified that a document we created with LibreOffice also works correctly in OpenOffice and Word.
One may consider using the document created in LibreOffice as a template, and modify it using NuGet package DocumentFormat.OpenXml..
If desired, one can create the entire document programmatically. One may start by reading Welcome to the Open XML SDK for Office, as well, as the articles it references. However, there is an easier way. One can download and install the Open XML SDK 2.5 Productivity Tool which one can use to generate the code. Then, one can then modify the code to suit one's needs. One must understand that the Open XML SDK Productivity Tool hasn't been updated in a while and may not support the latest versions of OpenXML (ie: the latest version of DocumentFormat.OpenXml, LibreOffice, MS Office, etc...). The versions I used for testing, are mentioned throughout the post.
Pre-requisite:
Create a new Windows Forms App (name: OpenXmlLibreOfficeTest)
Note: For Framework, ensure you select .NET 8.
Open Solution Explorer
Download/install NuGet package: DocumentFormat.OpenXml (v. 2.19.0)
Add a class to the project (name: HelperOpenXml.cs)
HelperOpenXml.cs:
In the OP, you stated that you have a document that has a paragraph, then a table, then another paragraph. I created a document using LibreOffice (v.7.5.9) for testing (name: Test.docx) which looks like:
Open the .docx file using the Open XML SDK 2.5 Productivity Tool (ex: Test.docx).
Click Reflect Code
Copy all of the code shown in the Open XML SDK 2.5 Productivity Tool to our class (ex: HelperOpenXml.cs) and then rename the namespace,
GeneratedCode, to OpenXmlLibreOfficeTest and class GeneratedClass to HelperOpenXml.
Alternatively, one can first copy the using directives shown in Open XML SDK 2.5 Productivity Tool, and then copy the code within the class (GeneratedClass) to our class (ex: HelperOpenXml.cs) - this will eliminate the need to rename the namespace and class.
Build your application
You may see errors such as the following:
If so, add the following using directives to your class (ex: HelperOpenXml.cs):
Then, Build your application
To create the .docx file programmatically, just call
CreatePackage.Usage:
Note: I've added a button (name: buttonCreateDocument) to the form and double-clicked it to create the event handler.
Now, one can modify the code within the class (ex: HelperOpenXml.cs) as desired. For example, you'll notice that all of the table data is, in essence, hard-coded. This code can be re-written.
Perhaps one has data stored in a DataTable or maybe one has a class
Computer
and defines
List<Computer> computers = new List<Computer>();(see List<T>) As you probably already know, one would modify the code to create the table rows within a loop.