先引入dll文件:
Microsoft.Office.Interop.Word.dll
Microsoft.Office.Interop.Word.ApplicationClass wordapp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document doc = null;
object nothing = System.Reflection.Missing.Value;
object filePath = “模板路径”;
doc = wordapp.Documents.Open(ref filePath, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);
doc.Paragraphs.Last.Range.Text = “输出结果”;
//直接打开word文件
doc = wordapp.ActiveDocument;
wordapp.Visible = true;
//保存到某一路径
object filePath2="保存地址";
doc.SaveAs(ref filePath2, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);
doc.Close(ref nothing, ref nothing, ref nothing);
wordapp.Quit(ref nothing, ref nothing, ref nothing);