码迷,mamicode.com
首页 > Windows程序 > 详细

c#读取Word模板,利用书签替换内容包括表格

时间:2016-08-09 14:48:23      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

 //生成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\tz103.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;
            }

 

c#读取Word模板,利用书签替换内容包括表格

标签:

原文地址:http://www.cnblogs.com/colder/p/5753159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!