Written by James Newton-King
Version 2.0 of the Open XML SDK is a big step forward for developers and brings a lot of exciting new features and tools to help work with Open XML documents.
In this article, we will to look at three new tools that are included in version 2.0 the Open XML SDK. Although the 2.0 SDK is not the final release, it works pretty well and there is certainly nothing preventing you from using the tools that are included:
Installing SDK Tools
The Open XML SDK tools are included in the SDK download, which weighs in at about 90 megs. One thing to note when installing the SDK, is to ensure ‘Yes’ is selected when prompted whether to install the Open XML SDK Development Productivity Tools:
The tools that come with the 2.0 SDK are installed to a directory beneath the SDK install location. By default, this is Program Files -> Open XML Format SDK -> V2.0 -> Tools.
Open XML Classes Explorer
With a specification as long and as detailed as the Open XML standard it is difficult to quickly lookup documentation about the markup you are working with. Fortunately, version 2.0 of the Open XML SDK comes with the Open XML Classes Explorer.
The Classes Explorer is a fantastic tool for aiding Open XML development, combining both the new SDK document object model documentation and the Open XML standard in one application.
All document object model classes are displayed in the tree to the left. Note that the Open XML SDK Packaging classes aren’t included in this application because there is no corresponding documentation in the Ecma standard. The documentation for packaging can be found in OpenXMLSDK.chm help file, which is also included with the SDK.
For example, the screenshot above shows help for the Document class. If you are new to Open XML and want to create a Word document then this is a great place to start. In one application you can see the SDK documentation for the Document class, specification documentation for the document XML element and also what valid child content classes can be inserted beneath it.
The second tool included in the SDK, OpenXMLDiff, is all about comparing two Open XML documents and then displaying the differences.
OpenXMLDiff is simple to use, just open two Open XML documents (a source and a target) and then click Compare.
The source document.Hello world.docx, is displayed on the left and the target, Hello dave.docx, is on the right. Content removed from the source document in the target is shown in red, new content is blue and changed content is green. Non-text content that can’t be compared like images are colored gray. In the example above you can see that I have changed “Hello world!” to “Hello Dave!”, added another paragraph and added a header and footer to the page.
As well as showing files that have been added and removed, you can see OpenXMLDiff also compares content inside the XML parts that make up the Open XML document. OpenXMLDiff indents and formats the documents content before displaying it side by side, making it quite easy to read. OpenXMLDiff is also a pretty good tool just for viewing the XML content inside a document as well!
The final SDK tool is the Open XML DocumentReflector.
DocumentReflector is a really interesting application: it analyzes an existing Open XML document and then generates code to recreate the document using the new strongly typed document classes included in the Open XML SDK.
For anyone familiar with Red Gate’s free .NET Reflector program, DocumentReflector performs the same function except instead of decompiling a .NET program back to code, it is decompiling an Open XML document to code.
The DocumentReflector UI is split into a tree of document parts and the XML elements of the content inside them on the left, and the generated code on the right.
To use the DocumentReflector open a document and click on one of the hierarchy of nodes presented on the left. The DocumentReflector will then present you with C# code to generate that node within the document, ready to be copied into your .NET solution.
One of the interesting features of the tool is you can select part or even any XML element inside a part and generate code just for what you have selected.
With code already generated by the tool, it is a simple matter to copy it into your solution and start modifying it and including parameters to pass custom content to include in the final document.
public static Paragraph GenerateParagraph(string paragraphText)
var element =
In the source code snippet above I have modified what DocumentReflector has generated to include a paragraphText parameter and made it the value of the text inserted into the paragraph. Whenever I need a simple text paragraph in a document I am generating, I can call this method and add the new paragraph to my document.
public static void Main(string args)
using (WordprocessingDocument package =
MainDocumentPart mainDocumentPart = package.AddMainDocumentPart();
Document document = new Document(
GenerateParagraph("Welcome to my test document."),
GenerateParagraph("I generated this with help from the Open XML DocumentReflector."),
GenerateParagraph("What a great tool!")));
While this is a very simple example, it would be easy to use DocumentReflector to create more complex scenarios which take collections of data returned from your data layer and generates tables.
DocumentReflector really simplifies the process of creating generated documents.
Open XML SDK Download
The Open XML SDK version 2.0 is currently in CTP. The latest release, which includes the new tools, is available to download here.
I am trying to find a way to create Excel 2007 (.xlsx) files, which can be opened both by Excel itself and via an OLEDB connection from SQL Server.
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'SELECT * FROM [Sheet1$]')
I've manually created an Excel file, saved it, used the DocumentReflector to create some C# code to create such a file, and run that C# code to create a brand new .xlsx file. Excel will happily open this Reflector-generated .xlsx file, but OLEDB refuses to open it.
If I open the .xlsx in Excel, save it as .xls, then again as a .xlsx file (so it's Excel itself that's generating the file, rather than any C# code), then it works fine. I can go into SQL Server, and create a Linked Server to this Excel file.
Pingback from Can we export Excel spreadsheet into XML and apply styleshee | Excel Tips | Office tips and tutorials