Chinaunix首页 | 论坛 | 博客
  • 博客访问: 512266
  • 博文数量: 60
  • 博客积分: 1445
  • 博客等级: 上尉
  • 技术积分: 507
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-14 19:15
文章分类

全部博文(60)

文章存档

2012年(1)

2011年(7)

2010年(52)

我的朋友

分类: C/C++

2010-07-12 14:07:09

往书签中写入数据
1 把书签的名字通过枚举的方式读出来,写到数组里(图/表格等特殊数据书签要处理掉)
2 读取数据库数据表内容写入书签初.注意技巧.
    a 检查文档书签集合中存在书签
    b 获取文档书签,并选择他,写入数据到selection
    c 移动书签的end到合适位置,否则读书签数据永远只读到书签定义处的字符.
    d 对于图/表格等的插入需要特殊处理.
    e 扫尾 另存.不要覆盖原来模板哦

  1. //设定标签的数据  
  2.        public string SetDocumentBookmarkData(string FileName,System.Data.DataTable dt)  
  3.        {  
  4.            //打开文档  
  5.            Microsoft.Office.Interop.Word.Document wDoc = null;  
  6.            Microsoft.Office.Interop.Word.Application wApp = null;  
  7.            this.OpenWordDoc(FileName, ref wDoc, ref wApp);  
  8.            object oEndOfDoc = "\\endofdoc";  
  9.            object missing = System.Reflection.Missing.Value;  
  10.   
  11.            //设定标签数据  
  12.            System.Collections.IEnumerator enu = wApp.ActiveDocument.Bookmarks.GetEnumerator();  
  13.   
  14.            string[] strbook=new string[dt.Columns.Count-1];  
  15.            int i=0;  
  16.            Microsoft.Office.Interop.Word.Bookmark bk=null;  
  17.            while (enu.MoveNext())  
  18.            {  
  19.                bk = (Microsoft.Office.Interop.Word.Bookmark)enu.Current;  
  20.   
  21.                if (bk.Name.ToString().Trim()!="Table")  
  22.                {  
  23.                    strbook[i] = bk.Name.ToString();  
  24.                    i++;  
  25.                }  
  26.            }  
  27.   
  28.            object tempobject=null;  
  29.            int length = 0;  
  30.            for (i = 0; i < strbook.Length;i++ )  
  31.            {  
  32.                tempobject = strbook[i].ToString();  
  33.   
  34.                if (wApp.ActiveDocument.Bookmarks.Exists(strbook[i].ToString()))  
  35.                {  
  36.                    wApp.ActiveDocument.Bookmarks.get_Item(ref tempobject).Select();  
  37.   
  38.                    wApp.Selection.Text = dt.Rows[0][strbook[i]].ToString();  
  39.   
  40.                    length = dt.Rows[0][strbook[i]].ToString().Length;  
  41.                    wApp.ActiveDocument.Bookmarks.get_Item(ref tempobject).End = wApp.ActiveDocument.Bookmarks.get_Item(ref tempobject).End + length;  
  42.                }  
  43.   
  44.            }  
  45.   
  46.            InsertTabletoBookmark("Table",ref wDoc, ref wApp);  
  47.   
  48.              
  49.            //收尾工作  
  50.            object o = null;  
  51.   
  52.            string guid = System.Guid.NewGuid().ToString();  
  53.            object sFileName = "D:/Test/office/word/" + guid + ".doc";  
  54.              
  55.            if (wDoc.SaveFormat == (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument)  
  56.            {  
  57.                wDoc.Application.ActiveDocument.SaveAs(ref sFileName, ref missing, ref missing,  
  58.                ref missing, ref missing, ref missing,  
  59.                ref missing, ref missing,  
  60.                ref missing, ref missing,  
  61.                ref missing, ref missing, ref missing,  
  62.                ref missing, ref missing, ref missing);  
  63.            }  
  64.   
  65.            wDoc.Close(ref missing, ref missing, ref missing);  
  66.            wApp.Quit(ref missing, ref missing, ref missing);  
  67.   
  68.            if (wDoc != null)  
  69.            {  
  70.                System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc);  
  71.                wDoc = null;  
  72.            }  
  73.   
  74.            if (wApp != null)  
  75.            {  
  76.                System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp);  
  77.                wApp = null;  
  78.            }  
  79.   
  80.            GC.Collect();  
  81.   
  82.            return guid + ".doc";  
  83.        }  


阅读(3526) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

user20412015-04-09 10:37:03

你好有没有关于asp.net的word操作