Arthur S.
Arthur S.

Reputation: 482

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

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?

Upvotes: 14

Views: 22687

Answers (4)

Manuel Amstutz
Manuel Amstutz

Reputation: 1388

I encountered the same problem and couldn't find a suitable template engine for our software. That's why I started this open-source project.

DocxTemplater Free, OpenSource

Upvotes: 0

SMUsamaShah
SMUsamaShah

Reputation: 7880

While trying to do the same I have found some libraries to create/edit DOC or DOCX in .Net

Upvotes: 5

Tim Rogers
Tim Rogers

Reputation: 21713

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.

Upvotes: 12

Davide Piras
Davide Piras

Reputation: 44595

for the new document generation from a template file (.dot) should be very easy, I think it's a parameter you specify in the word application file open or so, when you pass the path of the .dot file, telling word to create a new .doc based on that file and not edit the actual template document.

for form fields and bookmarks filling, lots of examples online on how to do it from C#, see here:

MS Word Office Automation - Filling Text Form Fields And Check Box Form Fields And Mail Merge

Upvotes: 0

Related Questions