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 = "

";
                string tableStr = "
";
                for (int i = 1; i <= maxRowIndex; i++)
                {
                    string rowStr = "";
                    for (int j = 1; j <= maxColumnIndex; j++)
                    {
                        //rowStr += string.Format("", i, j);
                        rowStr += "";

                    }
                    rowStr += "

";
                    tableStr += rowStr;
                }
                tableStr += "
{0}-{1}阀手动阀手动阀上的
";
                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;
        }

你可能感兴趣的:(学习)