C# winform 转换word文件文字

/// <summary>
        /// 转换word文件文字
        /// </summary>
        /// <param name="filename"></param>
        private void ConvertTo(string filename)
        {
            //创建word应用程序对象
            Microsoft.Office.Interop.Word.Application G_WordApplication =
                new Microsoft.Office.Interop.Word.Application();
            //创建object对象
            object P_FilePath = filename;
            //@"E:\合同\合同\房屋托管协议1.doc"

            //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)(() =>
                {
                    if (isAudit)
                    {
                        string[] strs = this.cmbFile.Text.Split('-');
                        string write = users.Name;//需要填写者
                        var items = (from i in cepg.CConvertDetail
                                     from j in cepg.CConvert
                                     where i.WriteUser == write && i.ConvertID == j.ID && j.FileName == this.cmbFile.Text && i.Status == 0
                                     select new
                                     {
                                         i.Type,
                                         i.Value,
                                     }).ToList();
                        if (items.Count > 0)
                        {
                            foreach (var item in items)
                            {
                                P_Find.Text = item.Type;                //设置查找的文本
                                P_Find.Replacement.Text = item.Value; //替换的文本
                            }
                        }
                    }
                }));
            //定义替换方式对象
            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);
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值