How to create *.docx files from a template in C#

Arthur S. picture Arthur S. · Sep 7, 2011 · Viewed 16.4k times · Source

I have a working ASP.NET MVC web application to manage projects and customers. Now I want to generate a word file for some customers. In this file should be displayed some data about the customer. Every generated file should have the same data and the same design. So I want to craete a new Word Template with the fields and want to fill the placeholders programmatically.

My problem is that I couldn't find a clear way to do that. Does anybody know a good learning resources?

Answer

Tim Rogers picture Tim Rogers · Sep 7, 2011

Try this page:

Building Office Open XML Files

Open XML files (docx) are ZIP packages containing XML files. In your case, I would create a copy of your original template, then use the System.IO.Packaging API to open the file and modify it. By opening, the correct XML file and replacing certain placeholders in XML, you should be able to achieve the result you want.