zoukankan      html  css  js  c++  java
  • c#读取Word模板,利用书签替换内容包括表格

     //生成WORD程序对象和WORD文档对象
                Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document doc = new Document();
                object miss = System.Reflection.Missing.Value; 
    
                try
                {
                //打开模板文档,并指定doc的文档类型
                //object objTemplate = System.Windows.Forms.Application.StartupPath + @"UploadFiles	z103.doc";
    
                //路径一定要正确
    
                object objTemplate = @"c:\测试.docx";
    
                object objDocType = WdDocumentType.wdTypeDocument;
    
                object objfalse = false;
    
                object objtrue = true;
    
                doc = (Document)appWord.Documents.Add(ref objTemplate, ref objfalse, ref objDocType, ref objtrue);
    
                //获取模板中所有的书签
    
                Bookmarks odf = doc.Bookmarks;
    
                string[] testTableremarks = { "FirstParty", "SecondParty", "FirstPartySign", "SecondPartySign" };
    
                string[] testTablevalues = { "嘉实多(深圳)有限公司广州分公司", "广州嘉通", "嘉实多(深圳)有限公司广州分公司", "广州嘉通贸易有限公司" };
    
    
    
                //循环所有的书签,并给书签赋值
    
                for (int oIndex = 0; oIndex < testTableremarks.Length; oIndex++)
                {
    
                    object obDD_Name = "";
    
                    obDD_Name = testTableremarks[oIndex];
    
                    //doc.Bookmarks.get_Item(ref obDD_Name).Range.Text = p_TestReportTable.Rows[0][testTablevalues[oIndex]].ToString();//此处Range也是WORD中很重要的一个对象,就是当前操作参数所在的区域
    
                    odf.get_Item(ref obDD_Name).Range.Text = testTablevalues[oIndex];
    
                }
    
    
                //附件,插入表格
                //这里简单生成样例数据表,工作中要以实际的数据集为准
                System.Data.DataTable dt = new System.Data.DataTable();
                dt.Columns.Add("name", typeof(string));
                dt.Columns.Add("age", typeof(string));
    
                DataRow dr = dt.NewRow();
                dr["name"] = "姓名"; dr["age"] = "年龄";
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["name"] = "张三"; dr["age"] = "20";
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["name"] = "李四"; dr["age"] = "25";
                dt.Rows.Add(dr);
    
    
    
                //附件一
                object obAttachMent = "Attachment1";
                //创建Word表格,并指定标签
                Microsoft.Office.Interop.Word.Table dtWord = doc.Tables.Add(odf.get_Item(ref obAttachMent).Range, dt.Rows.Count, dt.Columns.Count);
    
                dtWord.Borders.InsideLineStyle = WdLineStyle.wdLineStyleDot;
                dtWord.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDot;
    
                //循环往表格里赋值
                for (int i = 1; i <= dt.Rows.Count; i++)
                {
                    for (int j = 1; j <= dt.Columns.Count; j++)
                    {
                        dtWord.Rows[i].Cells[j].Range.Text = dt.Rows[i - 1][j - 1].ToString();
                    }
                }
            
    
    
                //第四步 生成word,将当前的文档对象另存为指定的路径,然后关闭doc对象。关闭应用程序
    
                object filename = "c:\" + DateTime.Now.ToString("yyyy-MM-dd") + ".docx";//HttpContext.Current.Server.MapPath("f:\") + "Testing_" + DateTime.Now.ToShortDateString() + ".doc"; 
                object Password = "P@55w0rd";
    
                //对Word文档进行加密保护,不允许编辑
                if (Password !=null)
                     {
                         doc.Protect(WdProtectionType.wdAllowOnlyReading, ref objfalse, ref Password, ref miss, ref miss);
                     }
    
                doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss,
                    ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
    
               
                object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
    
                doc.Close(ref doNotSaveChanges, ref miss, ref miss);
    
                appWord.Application.Quit(ref miss, ref miss, ref miss);
    
                doc = null;
    
                appWord = null;
    
                 MessageBox.Show("生成成功!");
    
                System.Diagnostics.Process.Start(filename.ToString());//打开文档
    
                }
                catch (Exception)
                {
                    doc.Close(ref miss, ref miss, ref miss);
                    appWord.Application.Quit(ref miss, ref miss, ref miss);
                    doc = null;
                    appWord = null;
                }
  • 相关阅读:
    application.properties多环境配置文件、jar包外部配置文件、配置项加密、程序中配置使用
    SpringBoot项目打war包部署Tomcat教程
    spring boot 集成 redis lettuce
    spring boot maven打包可运行jar包
    IDEA项目搭建十四——Web站点Controller基类及布局页静态资源设计
    Nginx Windows详细安装部署教程
    多线程编程CompletableFuture与parallelStream
    IDEA项目搭建十三——服务消费端与生产端通信实现
    【异常】MySQL建表报错:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"order"' at line 1
    【警告】mysql链接警告信息:Establishing SSL connection without server's identity verification is not recommended
  • 原文地址:https://www.cnblogs.com/colder/p/5753159.html
Copyright © 2011-2022 走看看