zoukankan      html  css  js  c++  java
  • 替换word文件内容

    /***-------------------------------------------Word-----------------------------------***/
    private void WordReplace(string filePath, string strOld, string strNew)
    {
    ///实例化顶级对象
    Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.ApplicationClass();
    object nullobj = System.Reflection.Missing.Value;
    object file = filePath;
    //实例化Document对象
    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);
    //做替换操作
    //doc.Content.Text = doc.Content.Text.Replace(strOld, strNew);
    app.Selection.Find.ClearFormatting();
    app.Selection.Find.Replacement.ClearFormatting();
    app.Selection.Find.Text
    = strOld;
    app.Selection.Find.Replacement.Text
    = strNew;

    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);
    }

  • 相关阅读:
    计算机通信
    笔记0402
    笔记0414
    vb6 VS vb.net int数据类型
    jQuery好网站
    jQuery实现图片翻滚
    jQuery获取一组单选按钮的值
    Redhat安装、使用及服务器应用FAQ
    js判断变量是否赋值(存在)
    IDE 与SATA区别
  • 原文地址:https://www.cnblogs.com/endsock/p/2022700.html
Copyright © 2011-2022 走看看