Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Create docx files using c# without com

0.00/5 (No votes)
26 Dec 2008 1  
Create and modify open xml (docx) file using c#/vb without ms word installed
DocxConsoleApp

Introduction

This article shows how to use Invoke Docx Library to render docx (open xml) files without ms word installed.

Background

Few monthes ago i had to add docx rendering feature to one of my projects. Since docx is zipped xml format i decided to automate all this an create template processing engine. After i used it in several projects and now decided to ship this library as a separate product. And for free. This article tells how to use it to create word documents. To get more info follow http://invoke.co.nz/products/docx.aspx

Using the code

I tried to make this library as simple as possible. In most of cases you will need to work with two classes only - DocumentRenderer which implements actual API and DocumentDataSource which is container for infomation you'll put inside the template. If you'll need some extra capabilities - you'll have to use DataItem class. See samples or download lib + samples to get more info. The library can work easily with DataTable, anonymous classes, parses objects using reflection, has implicit conversion for several types and extensible type system that allows you to process own data types. It can work with ASP.NET without temporary files creation (using MemoryStream).

Download this and some other samples. Please download library from this site till i synchronize libraries EULA with codeproject licence

Also new features are planned soon, so if you think this lib lacks some feature - feel free to send feature request, this is important for us. Steps to make it work:

1. Create blank console (or web, or any you want) application.

DocxConsoleApp

2. Add reference to Invoke.Docx.dll

DocxConsoleApp

3. Create word template

DocxConsoleApp

as you see parameters as surrounded by %% symbols which let the library know that this is parameter. %TOTAL@C2% means that the TOTAL field should be filled in in "C2" (money) format. %SERVICES.NAME% mean that this field belongs to SERVICES table and will be repeated several times.
See more help for fields at http://www.invoke.co.nz/products/help/docx_structure_of_parameters.aspx and at http://www.invoke.co.nz/products/help/docx_tables.aspx

4. Type this code into Main method + add usings

            File.Copy("order_template.docx", "order.docx", true);

            DocumentDataSource source = new DocumentDataSource();

            source["DATE"] = DateTime.Now;
            source["CUSTOMER"] = "Some corp.";
            source["DIRECTOR_NAME"] = "Joe Bloggs";
            source["TOTAL"] = 17;
            source["SERVICES"] =
                DataItem.ConvertFrom(
                    new []
                    {
                        new {  
                            Name = "Some service",
                            Amount = 1,
                            Price = 2,
                            Total = 2.0
                        },
                        new {  
                            Name = "Some service",
                            Amount = 3,
                            Price = 5,
                            Total = 15.0
                        }
                    }
                );


            DocumentRenderer.ProcessDocument("order.docx", source);
// note: this line might cause exception is word isn't installed on your computer
            Process.Start("order.docx");		


And get the document!
DocxConsoleApp

seems that's all. If you have any questions or suggestions - email me to support @ invoke.co.nz. Also you can track changes here or here. More features pending!


License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here