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);
    
            }
  • 相关阅读:
    Go语言入门
    简述cookies 和 session
    Linux inode 理解
    BZOJ 1012 最大数maxnumber
    BZOJ 1087 互不侵犯king
    CSS从大图中抠取小图完整教程(background-position应用)
    javascript中i++与++i
    脱离文档流分析
    在Windows上以zip压缩包方式安装mysql
    centos7 python2.7下安装paramiko模块
  • 原文地址:https://www.cnblogs.com/AlexOneBlogs/p/8483914.html
Copyright © 2011-2022 走看看