欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# 操作word

程序员文章站 2022-06-02 21:18:12
...
需要单独引入的是COM中的Microsoft Office 12.0 Object Library

如果需要部署到服务器,服务器也需要安装office对应版本,否则会出现一大堆问题
https://www.cnblogs.com/5426z/articles/4865312.html

string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            path = path.Replace("Debug", "doc");
            path = path.Replace("bin\\", "");

            Microsoft.Office.Interop.Word._Application app = null;
            Microsoft.Office.Interop.Word._Document doc = null;
            //将要导出的新word文件名
            string newFile = path + "Replace.docx";
            string physicNewFile = path + "购买协议.docx";
            try
            {
                app = new Application();//创建word应用程序
                object fileName = physicNewFile;//模板文件
                //打开模板文件
                object oMissing = System.Reflection.Missing.Value;
                doc = app.Documents.Open(ref fileName,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                //构造数据
                Dictionary<string, string> datas = new Dictionary<string, string>();
                datas.Add("{name}", "刘德华");
                datas.Add("{amount}", "30000");
                datas.Add("{stratdate}", "2019年1月17日");
                datas.Add("{enddate}", "2019年3月18日");
                datas.Add("{dateline}", "3个月");
                datas.Add("{loanrate}", "11%");
                datas.Add("{one}", "第一期");
                datas.Add("{two}", "第二期");
                datas.Add("{three}", "第三期");
                datas.Add("{oneamount}", "0.83");
                datas.Add("{twoamount}", "0.83");
                datas.Add("{threeamount}", "100.83");


                object replace = WdReplace.wdReplaceAll;
                foreach (var item in datas)
                {
                    app.Selection.Find.Replacement.ClearFormatting();
                    app.Selection.Find.ClearFormatting();
                    app.Selection.Find.Text = item.Key;//需要被替换的文本
                    app.Selection.Find.Replacement.Text = item.Value;//替换文本 

                    //执行替换操作
                    app.Selection.Find.Execute(
                    ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref replace,
                    ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing);
                }

                //对替换好的word模板另存为一个新的word文档
                doc.SaveAs(newFile,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

            }
            catch (Exception ex)
            {

            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();//关闭word文档
                }
                if (app != null)
                {
                    app.Quit();//退出word应用程序
                }
            }        



参考:
https://www.cnblogs.com/lee24789229/p/7613598.html