Project interface created based on reference download link: Method to automatically create Word document via template in C#, but the resource is a txt file, need to type the code manually, considered as practice.
Before starting, we need to install Microsoft.Office.Interop.Word from NuGet. Below is the code needed, just copy it directly. It is slightly different from using NPOI.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// report.cs
using System;
using System.Diagnostics;
using Microsoft.Office.Interop.Word;

namespace export_doc
{
public class Report
{
private _Application _wordApp = null;
private _Document _wordDoc = null;

public _Application Application
{
get => _wordApp;
set => _wordApp = value;
}

public _Document Document
{
get => _wordDoc;
set => _wordDoc = value;
}
/// <summary>
/// use template to create new file
/// </summary>
/// <param name="path"></param>
public void CreateNewDocument(string path)
{
KillWinWordProcess();
_wordApp = new ApplicationClass()
{
DisplayAlerts = WdAlertLevel.wdAlertsNone,
Visible = false
};

object missing = System.Reflection.Missing.Value;
object templateName = path;
_wordDoc = _wordApp.Documents.Open(ref templateName, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);

}
/// <summary>
/// save new file
/// </summary>
/// <param name="path"></param>
public void SaveDocument(string path)
{
object fileName = path;
object format = WdSaveFormat.wdFormatDocument;//save foramt
object missing = System.Reflection.Missing.Value;
_wordDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing);

//close _wordDoc , _wordApp
object saveChanges = WdSaveOptions.wdSaveChanges;
object originalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
object routeDocument = false;
_wordDoc.Close(ref saveChanges,ref originalFormat,ref routeDocument);
_wordApp.Quit(ref saveChanges,ref originalFormat,ref routeDocument);
}
/// <summary>
/// Insert value to bookMark
/// </summary>
/// <param name="bookMark"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool InsertValue(string bookMark, string value)
{
object bkObj = bookMark;
if (_wordApp.ActiveDocument.Bookmarks.Exists(bookMark))
{
_wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select();
_wordApp.Selection.TypeText(value);
return true;
}
return false;
}
/// <summary>
/// insert table & bookmark
/// </summary>
/// <param name="bookMark"></param>
/// <param name="rows"></param>
/// <param name="columns"></param>
/// <param name="width"></param>
/// <returns></returns>
public Table InsertTable(string bookMark, int rows, int columns, float width)
{
object missing = System.Reflection.Missing.Value;
object toStart = bookMark;
Range range = _wordDoc.Bookmarks.get_Item(ref toStart).Range;
Table newTable = _wordDoc.Tables.Add(range, rows, columns, ref missing, ref missing);
//set table format
newTable.Borders.Enable = 1;
newTable.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth050pt;
if (width != 0)
newTable.PreferredWidth = width;
newTable.AllowPageBreaks = false;
return newTable;
}
/// <summary>
/// merge cells
/// </summary>
/// <param name="table"></param>
/// <param name="row1"></param>
/// <param name="column1"></param>
/// <param name="row2"></param>
/// <param name="column2"></param>
public void MergeCell(Microsoft.Office.Interop.Word.Table table, int row1, int column1, int row2, int column2)
{
table.Cell(row1,column1).Merge(table.Cell(row2,column2));
}
/// <summary>
/// set table alignment vertical % horizontal
/// </summary>
/// <param name="table"></param>
/// <param name="align"></param>
/// <param name="vertical"></param>
public void SetParagraph_Table(Table table, int align, int vertical)
{
switch (align)
{
case -1:
table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//left align
break;
case 0:
table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//center align
break;
case 1:
table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;//right align
break;
}

switch (vertical)
{
case -1:
table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalTop;//top align
break;
case 0:
table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//center align
break;
case 1:
table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom;//bottom align
break;
}
}
/// <summary>
/// set font name
/// </summary>
/// <param name="table"></param>
/// <param name="fontName"></param>
/// <param name="size"></param>
public void SetFont_Table(Table table, string fontName, double size)
{
if (size!=0)
{
table.Range.Font.Size = Convert.ToSingle(size);
}

if (!string.IsNullOrEmpty(fontName))
{
table.Range.Font.Name = fontName;
}
}
/// <summary>
/// set outline border
/// </summary>
/// <param name="n"></param>
/// <param name="use"></param>
public void UseBorder(int n, bool use)
{
// allow has border , default no borders , 1 as line border , 2,3 as dash border
_wordDoc.Content.Tables[n].Borders.Enable = use ? 1 : 2;
}
/// <summary>
/// add row
/// </summary>
/// <param name="n"></param>
public void AddRow(int n)
{
object missing = System.Reflection.Missing.Value;
_wordDoc.Content.Tables[n].Rows.Add(ref missing);
}
/// <summary>
/// add rows
/// </summary>
/// <param name="n">table number</param>
/// <param name="rows">add rows count</param>
public void AddRow(int n, int rows)
{
object missing = System.Reflection.Missing.Value;
Table table = _wordDoc.Content.Tables[n];
for (int i = 0; i < rows; i++)
{
table.Rows.Add(ref missing);
}
}
/// <summary>
/// Insert value to cell
/// </summary>
/// <param name="table">target table</param>
/// <param name="row">locate row</param>
/// <param name="column">locate column</param>
/// <param name="value">value</param>
public void InsertCell(Table table, int row, int column, string value)
{
table.Cell(row, column).Range.Text = value;
}
/// <summary>
/// Insert value to cell
/// </summary>
/// <param name="n">table number</param>
/// <param name="row">locate row</param>
/// <param name="column">locate column</param>
/// <param name="value">value</param>
public void InsertCell(int n, int row, int column, string value)
{
_wordDoc.Tables[n].Cell(row, column).Range.Text = value;
}
/// <summary>
/// Insert value to cell
/// </summary>
/// <param name="n">table number</param>
/// <param name="row">locate row</param>
/// <param name="columns">locate column</param>
/// <param name="values">value array</param>
public void InsertCell(int n, int row, int columns, string[] values)
{
Table table = _wordDoc.Content.Tables[n];
for (int i = 0; i < columns; i++)
{
table.Cell(row, columns).Range.Text = values[i];
}
}
/// <summary>
/// Insert Picture
/// </summary>
/// <param name="bookMark">target bookMark</param>
/// <param name="picturePath">picture path</param>
/// <param name="width">picture width</param>
/// <param name="height">picture height</param>
public void InsertPicture(string bookMark, string picturePath, float width, float height)
{
object missing = System.Reflection.Missing.Value;
object toStart = bookMark;
object linkToFile = false;//the picture is link
object saveWithDocument = true; // save with document
object range = _wordDoc.Bookmarks.get_Item(ref toStart).Range;//insert picture locate
_wordDoc.InlineShapes.AddPicture(picturePath, ref linkToFile, ref saveWithDocument, ref range);
_wordDoc.Application.ActiveDocument.InlineShapes[1].Width = width;//set picture width
_wordDoc.Application.ActiveDocument.InlineShapes[1].Height = height;//set picture height
}
/// <summary>
/// Insert Text
/// </summary>
/// <param name="bookMark">target bookMark</param>
/// <param name="text">value</param>
public void InsertText(string bookMark, string text)
{
object toStart = bookMark;
object range = _wordDoc.Bookmarks.get_Item(ref toStart).Range;
Paragraph wp = _wordDoc.Content.Paragraphs.Add(ref range);
wp.Format.SpaceBefore = 6;
wp.Range.Text = text;
wp.Format.SpaceAfter = 24;
wp.Range.InsertParagraphAfter();
_wordDoc.Paragraphs.Last.Range.Text = "\n";
}
/// <summary>
/// kill word process
/// </summary>
public void KillWinWordProcess()
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD");
foreach (Process process in processes)
{
var b = process.MainWindowTitle == "";
if (b)
{
process.Kill();
}
}
}
}


}

// program.cs
namespace export_doc
{
internal class Program
{
public static void Main(string[] args)
{
Report report = new Report();
report.CreateNewDocument(@"xxxxx.docx");
report.InsertValue("warning_level", "Ⅱ");

report.SaveDocument(@"xxxx.docx");
}
}
}

Image Description