Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 235
Arthur S. Avatar asked Sep 07 '11 08:09

Arthur S.


People also ask

How do I create a DOCX template?

Open the Word document that you want to save as a template. On the File menu, click Save as Template. In the Save As box, type the name that you want to use for the new template. (Optional) In the Where box, choose a location where the template will be saved.

How do you create a document from a template?

Create a template based on an existing template or documentClick the File tab, and then click New. Under Available templates, click New from existing. Click a template or a document that is similar to the one that you want to create, and then click Create New.

How do I convert a template to a Word document?

In the Word Options dialog box, (1) click Add-ins in the left bar, (2) select Templates from the Manage drop down list, and (3) click Go. 3. A new dialog box should appear for Templates and Add-ins. Check the “Automatically update document” styles option and click the Attach button.

Is DOCX a template?

A DOCX template is a HotDocs template you create in Microsoft Word using the Author tab that appears on the Word ribbon. A DOCX template differs from a plain text template from which HotDocs assembles a final document that lacks any textual formatting. You create and edit a plain text template with HotDocs Composer.


1 Answers

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.

like image 135
Tim Rogers Avatar answered Oct 03 '22 07:10

Tim Rogers