往书签中写入数据
1 把书签的名字通过枚举的方式读出来,写到数组里(图/表格等特殊数据书签要处理掉)
2 读取数据库数据表内容写入书签初.注意技巧.
a 检查文档书签集合中存在书签
b 获取文档书签,并选择他,写入数据到selection
c 移动书签的end到合适位置,否则读书签数据永远只读到书签定义处的字符.
d 对于图/表格等的插入需要特殊处理.
e 扫尾 另存.不要覆盖原来模板哦
-
- public string SetDocumentBookmarkData(string FileName,System.Data.DataTable dt)
- {
-
- Microsoft.Office.Interop.Word.Document wDoc = null;
- Microsoft.Office.Interop.Word.Application wApp = null;
- this.OpenWordDoc(FileName, ref wDoc, ref wApp);
- object oEndOfDoc = "\\endofdoc";
- object missing = System.Reflection.Missing.Value;
-
-
- System.Collections.IEnumerator enu = wApp.ActiveDocument.Bookmarks.GetEnumerator();
-
- string[] strbook=new string[dt.Columns.Count-1];
- int i=0;
- Microsoft.Office.Interop.Word.Bookmark bk=null;
- while (enu.MoveNext())
- {
- bk = (Microsoft.Office.Interop.Word.Bookmark)enu.Current;
-
- if (bk.Name.ToString().Trim()!="Table")
- {
- strbook[i] = bk.Name.ToString();
- i++;
- }
- }
-
- object tempobject=null;
- int length = 0;
- for (i = 0; i < strbook.Length;i++ )
- {
- tempobject = strbook[i].ToString();
-
- if (wApp.ActiveDocument.Bookmarks.Exists(strbook[i].ToString()))
- {
- wApp.ActiveDocument.Bookmarks.get_Item(ref tempobject).Select();
-
- wApp.Selection.Text = dt.Rows[0][strbook[i]].ToString();
-
- length = dt.Rows[0][strbook[i]].ToString().Length;
- wApp.ActiveDocument.Bookmarks.get_Item(ref tempobject).End = wApp.ActiveDocument.Bookmarks.get_Item(ref tempobject).End + length;
- }
-
- }
-
- InsertTabletoBookmark("Table",ref wDoc, ref wApp);
-
-
-
- object o = null;
-
- string guid = System.Guid.NewGuid().ToString();
- object sFileName = "D:/Test/office/word/" + guid + ".doc";
-
- if (wDoc.SaveFormat == (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument)
- {
- wDoc.Application.ActiveDocument.SaveAs(ref sFileName, ref missing, ref missing,
- ref missing, ref missing, ref missing,
- ref missing, ref missing,
- ref missing, ref missing,
- ref missing, ref missing, ref missing,
- ref missing, ref missing, ref missing);
- }
-
- wDoc.Close(ref missing, ref missing, ref missing);
- wApp.Quit(ref missing, ref missing, ref missing);
-
- if (wDoc != null)
- {
- System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc);
- wDoc = null;
- }
-
- if (wApp != null)
- {
- System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp);
- wApp = null;
- }
-
- GC.Collect();
-
- return guid + ".doc";
- }
阅读(3568) | 评论(1) | 转发(0) |