winform 替换word中指定内容



 需要引用两个dll:Interop.Microsoft.Office.Core.dll和Microsoft.Office.Interop.Word.dll (可以到我的资源里面去下载)


try
            {
                //创建word应用程序对象

                Microsoft.Office.Interop.Word.Application G_WordApplication =
                    new Microsoft.Office.Interop.Word.Application();
                //创建object对象
                object P_FilePath = @"C:\Users\Lming\Desktop\aaa.docx";
                //object P_FilePath = System.Windows.Forms.Application.StartupPath.ToString() + "\\word.doc";
                object G_Missing = System.Reflection.Missing.Value;

                //打开word文档
                Microsoft.Office.Interop.Word.Document P_Document = G_WordApplication.Documents.Open(
                    ref P_FilePath, ref G_Missing, ref G_Missing, ref G_Missing,
                    ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                    ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                    ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing);
                //获得文档范围
                Microsoft.Office.Interop.Word.Range P_Range =
                    P_Document.Range(ref G_Missing, ref G_Missing);
                //得到find对象
                Microsoft.Office.Interop.Word.Find P_Find = P_Range.Find;
                this.Invoke(
                    (MethodInvoker)(() =>
                    {
                        P_Find.Text = "@aaaa";                //设置查找的文本
                        P_Find.Replacement.Text = "替换的文本"; //替换的文本
                       
                        //P_Find.Text = "{甲方}";                //设置查找的文本
                        //P_Find.Replacement.Text = "王小斌"; //替换的文本
                        //P_Find.Text = "{编号}";                //设置查找的文本
                        //P_Find.Replacement.Text = "ISO1101"; //替换的文本
                    }));
                //定义替换方式对象
                object P_Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
                //执行替换
                bool P_bl = P_Find.Execute(ref G_Missing,
                    ref G_Missing, ref G_Missing, ref G_Missing,
                    ref G_Missing, ref G_Missing, ref G_Missing,
                    ref G_Missing, ref G_Missing, ref G_Missing,
                    ref P_Replace, ref G_Missing, ref G_Missing,
                    ref G_Missing, ref G_Missing);
                //保存文档
                G_WordApplication.Documents.Save(ref G_Missing, ref G_Missing);
                //关闭文档
                ((Microsoft.Office.Interop.Word._Document)P_Document).Close(ref G_Missing, ref G_Missing, ref G_Missing);
                //退出word应用程序
                ((Microsoft.Office.Interop.Word._Application)G_WordApplication).Quit(ref G_Missing, ref G_Missing, ref G_Missing);

            }
            catch
            {
                var a = "";
            }

*****************************************************************在指定位置添加表格*********************************************************************************************************

 private void addTable(int maxRowIndex, int maxColumnIndex)
        {
            MsWord.Application wordApp = new MsWord.Application();
            MsWord.Document wordDoc = wordApp.Documents.Open(string.Format("{0}\\aaa.docx", AppDomain.CurrentDomain.BaseDirectory),
                        ref oMissing, true,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, false, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing);
            try
            {
                DateTime startTime = DateTime.Now;
                //string tableStr = "<table>";
                string tableStr = "<table style='font-family:华文细黑;font-size:10pt'>";
                for (int i = 1; i <= maxRowIndex; i++)
                {
                    string rowStr = "<tr>";
                    for (int j = 1; j <= maxColumnIndex; j++)
                    {
                        //rowStr += string.Format("<td>{0}-{1}</td>", i, j);
                        rowStr += "<td>阀手动阀手动阀上的</td>";

                    }
                    rowStr += "</tr>";
                    tableStr += rowStr;
                }
                tableStr += "</table>";
                var range = wordDoc.Bookmarks["Aldsfjsa"].Range;
                PasteHTML(range, tableStr);

                //
                //wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
                //
                TimeSpan timeSpan = DateTime.Now - startTime;
                MessageBox.Show(timeSpan.TotalMilliseconds.ToString());

                //wordDoc.SaveAs2(string.Format("{0}\\newword_{1}.doc", AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyyMMddHHssmm")));
                wordDoc.SaveAs2(string.Format("{0}\\newword_{1}", AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyyMMddHHssmm")), Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF);
                wordApp.Visible = true;
            }
            catch
            {
                wordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                wordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
            }
            finally
            {
                wordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                wordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
            }
        }
        public void PasteHTML(MsWord.Range range, string html)
        {
            Clipboard.Clear();

            //Clipboard.SetData("HTML Format", HtmlClipboardData(html));
            Encoding encoding = Encoding.GetEncoding("utf-8");
            int htmlLen = encoding.GetByteCount(html);
            Clipboard.SetData("HTML Format", string.Format("Version:0.9\nStartHTML:80\nEndHTML:{0,8}\nStartFragment:80\nEndFragment:{0,8}\n", 80 + htmlLen) + html + "<");
            range.PasteExcelTable(false, true, false);
            range.Font.Size = 9F;
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值