在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
//filePath:word文档的路径;strOld:需要替换的内容;strNew:替换的新内容; public static void InitWord(string filePath, List<string> strOld, List<string> strNew) { Microsoft.Office.Interop.Word._Application app = new Microsoft.Office.Interop.Word.ApplicationClass(); object nullobj = System.Reflection.Missing.Value; object file = filePath; Microsoft.Office.Interop.Word._Document doc = app.Documents.Open( ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj) as Microsoft.Office.Interop.Word._Document; //app.Selection.Find.ClearFormatting(); //app.Selection.Find.Replacement.ClearFormatting(); if (strOld.Count > 0) { for (int i = 0; i < strOld.Count; i++) { app.Selection.Find.Text = strOld[i]; app.Selection.Find.Replacement.Text = strNew[i]; object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll; app.Selection.Find.Execute(ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref objReplace, ref nullobj, ref nullobj, ref nullobj, ref nullobj); } } //格式化 //doc.Content.AutoFormat(); //清空Range对象 //Microsoft.Office.Interop.Word.Range range = null; //保存 doc.Save(); doc.Close(ref nullobj, ref nullobj, ref nullobj); app.Quit(ref nullobj, ref nullobj, ref nullobj); }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论