HOW TO generate Open XML file in C# in 4 minutes?

August 21, 2009 | by Maciej Greń

View Comments

document_logo

Do you know what is Open XML format? Why is it so good? This is an open document format based on XML. In this format you can save your documents, spreadsheets or presentations using various application like MS Office 2007,MS Works 9.0, Open Office.org 3 and many other applications. This format is also great for reading, generating and manipulating document  files for software developers. Now you don’t need several varied libraries or SDKs to work with various formats of documents for e.g. presentation in PowerPoint or spreadsheet in Open Office Calc. You can use only one Open XML format to work with those files and the best tool for it is Open XML  Format SDK 2.0.

Open XML Format SDK 2.0 and Document Reflector in few words.

Open XML  Format SDK developed by Microsoft allows creating the inner structure for MS Office 2007 files (Word, Excel, PowerPoint) in Open XML format. Microsoft released two versions of SDK.

The latest version 2.0 was released in April 2009 as the Community Technology Preview (CTP).   This version is big step forward compared to 1.0. In version 1.0 we had to create the XML code from the scratch and that was a very error prone process. In version 2.0 the situation has changed because it has strongly typed document object model (DOM). This is most important feature. However, there are more interesting new features in this version that improve creating Open XML files:

  • tool for comparing Open XML files,
  • a Class Explorer that helps us understand the markup and determine which classes to use in the strongly typed DOM,
  • and a very nice tool – the Document Reflector.

Recently, I’ve  had the pleasure to use and test Document Reflector to generate Excel reports with charts.  This tool is what some people call a “lazy developer oriented tool”.  Why? Because you don’t have to waste time thinking about the classes you need to create for generating an Open XML file. All you have to do is create an Open XML file using e.g. Office 2007 application.  Next, use a  file (SDK) created in the Document Reflector to generate the necessary Open XML SDK  C# code for you.

Quick Open XML file generation

First I’d like to present how we can generate Open XML file using Open XML SDK and Document Reflector in maximum 4 minutes. We need the latest version of SDK (you can download it form here), Visual Studio 2008 and some tool to make Open XML file e.g. MS Office 2007.

When you download and install SDK, you can make sample Open XML file. Because I’ve worked with Excel 2007 I create sample xlsx file and generate C# code that creates new xlsx file using Open XML SDK.  Watch video below how to do this:

Generated code structure

To use the Document Reflector, you have to open a document. You will see small tree with root node named Package. Click on this node and Document Reflector presents you C# complete code to generate document. Code consist of one class GeneratedClass which contains:

  1. main method CreatePackage(string filePath) which create appropriate Open XML document (in ours example SpreadsheetDocument),
  2. AddParts(package) – static method which adds all necessary parts of the document,
  3. several static methods named according to pattern GeneratePartNamePartPartNumber() which generate content of specific part.

If you want to change the content of some part you must do it in appropriate GeneratePart() method. You can also use content of those methods to build your application from selected parts.

In Package node you have all methods and if you want only some part of code you must find it usually in hundreds lines of code. You don’t have to do it in this way. The tree give you possibility to generate only some part of document. You can click on an appropriate part node, and the document reflector will generate the code to create only the part of document.

Advantages

The list of advantages is short but very important for developers, especially newbies. Document Reflector in Open XML Format SDK is very useful tool because:

  • You can create very complex Open XML file or part of it very quick.
  • You don’t have to study SDK documentation – you can learn SDK from working example.
  • Generated code has very simple and clear structure to use.
  • You don’t make any mistake in code if you use method “copy-paste”.

Disadvantages

There are a few of them but they can irritate:

  • You can observe that Document Reflector generate redundant code. For example when you want have chart referenced to values in cells in spreadsheet you should get data from cells using formula e.g. A1: B4. Generated code will have no reference but absolute values.
  • If you don’t know SDK very well you don’t know what generated code do.

When should you use this tool?

In my opinion Document Reflector is a fanatastic tool but not for generating complete Open XML files. In my practice, I used this tool many times only to generate some complex parts of spreadsheets like charts or additional sheets. In this way Reflector did the worse work for me.  Later I just pasted it to my project and used it without any issues.While doing it I also noticed that  I need to use SDK documentation often because I didn’t understand what happens in the code. But thanks to this disadavantage I could better learn SDK.

My summary: Thanks to Open XML SDK 2.0 and Document Reflector creating Open XML documents have never been that easy!

Share your experience with us below or meet us on Twitter: @GOYELLO.

Thanks to Karol Świder for helping me write this blog post.

blog comments powered by Disqus