Create and Edit a Word Document with C#
How to create a word document from a file template and edit the new file with custom information.
In this article, I want to share how to create a word document from a file template and edit the new file with custom information.
This is a common task but it could take more time than normal when we are working in real project
To save some time and avoid googling each time we need it, I have created a repository in GitHub with a generic code that we can copy in our C# project.
Take a look at entire example here.
The code has a C# class “Word Generator.cs” that encapsulates the next actions:
- Inserts single text (text, number, dates, etc)
- Inserts bullets and nested bullets in a specific area
- Inserts a table with rows and columns dynamically
The “WordGenerator.cs” use “DocumentFormat.Open.XML”.
Also, you can find in “Program.cs” the implementation of this actions/methods.
The example code basically gets a file template (previously configurated) with TAGS(Plan Text Content Control) , TABLES (with name configured in right click > table properties > title) and inserts the values in those areas.
The example already includes a File Template “Template1.docx” with the configuration mentioned above.
After executing the code, you will get a new file with content.
Review and copy the code from “Program.cs” in order the implement the action you need.
Sé productivo. Sé extraordinario. Sé INAVANT.