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);
    
            }
  • 相关阅读:
    Enables DNS lookups on client IP addresses 域名的分层结构
    移除 URL 中的 index.php
    Design and Architectural Goals
    The Model represents your data structures.
    If a cache file exists, it is sent directly to the browser, bypassing the normal system execution.
    UNION WHERE
    cookie less
    http 2
    UNION DISTINCT
    联合约束 CONCAT()
  • 原文地址:https://www.cnblogs.com/AlexOneBlogs/p/8483914.html
Copyright © 2011-2022 走看看