zoukankan      html  css  js  c++  java
  • C#动态给Word文档填充内容

            //filePath:word文档的路径;strOld:需要替换的内容;strNew:替换的新内容;
    //注意:strOld中的字符数量要与新的strNew中的一一对应
            public static void InitWord(string filePath, List<string> strOld, List<string> strNew)
            {
                Microsoft.Office.Interop.Word._Application app = new Microsoft.Office.Interop.Word.ApplicationClass();
                object nullobj = System.Reflection.Missing.Value;
                object file = filePath;
                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) as Microsoft.Office.Interop.Word._Document;
    
                //app.Selection.Find.ClearFormatting();
                //app.Selection.Find.Replacement.ClearFormatting();
    
                if (strOld.Count > 0)
                {
                    for (int i = 0; i < strOld.Count; i++)
                    {
                        app.Selection.Find.Text = strOld[i];
                        app.Selection.Find.Replacement.Text = strNew[i];
                        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);
    
            }
  • 相关阅读:
    django-form表单
    django-登录页面添加验证码
    opensatck误删除service项目
    运维脚本
    大数据平台架构组件选择与运用场景
    HDP2.4安装(五):集群及组件安装
    Spark(三): 安装与配置
    Spark(二): 内存管理
    Spark(一): 基本架构及原理
    ElasticSearch基本使用
  • 原文地址:https://www.cnblogs.com/AlexOneBlogs/p/8483914.html
Copyright © 2011-2022 走看看