亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

C#讀取和寫入word2003的書簽內容(怪,新,全)

系統 1989 0
文/Andmm? 出處/博客園

目前的項目又要對word2003進行編程,主要功能是讀取和插入標簽的數據.具體代碼如下:
(打開word文檔與網上雷同)

引用部分:



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Office;
using Microsoft.Office.Core;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Word;



打開word2003文檔,項目中的文檔模板實際是doc的文檔,dot的很不好用.

做doc文檔模板的時候不要用空格留空白,使用段落縮進的方式.標簽的添加等文檔格式完全編輯好再添加.

對于要預留空白的內容,通過插入1個只有1個單元格(無邊框)的方式來進行.這樣保證單元格以后內容位置固定,不會因為插入了文字內容而移動位置,這隊固定格式公文很有好處.

打開word模板和word文件



#region 打開word模板和word文件
? ? ? ? public static void OpenWordDot()
? ? ? ? {

? ? ? ? }

? ? ? ? private void OpenWordDoc(string FileName, ref Microsoft.Office.Interop.Word.Document wDoc, ref? Microsoft.Office.Interop.Word.Application WApp)
? ? ? ? {
? ? ? ? ? ? if (FileName == "") return;
? ? ? ? ? ? Microsoft.Office.Interop.Word.Document thisDocument = null;
? ? ? ? ? ? Microsoft.Office.Interop.Word.FormFields formFields = null;
? ? ? ? ? ? Microsoft.Office.Interop.Word.Application thisApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
? ? ? ? ? ? thisApplication.Visible = true;
? ? ? ? ? ? thisApplication.Caption = "";
? ? ? ? ? ? thisApplication.Options.CheckSpellingAsYouType = false;
? ? ? ? ? ? thisApplication.Options.CheckGrammarAsYouType = false;

? ? ? ? ? ? Object filename = FileName;
? ? ? ? ? ? Object ConfirmConversions = false;
? ? ? ? ? ? Object ReadOnly = true;
? ? ? ? ? ? Object AddToRecentFiles = false;

? ? ? ? ? ? Object PasswordDocument = System.Type.Missing;
? ? ? ? ? ? Object PasswordTemplate = System.Type.Missing;
? ? ? ? ? ? Object Revert = System.Type.Missing;
? ? ? ? ? ? Object WritePasswordDocument = System.Type.Missing;
? ? ? ? ? ? Object WritePasswordTemplate = System.Type.Missing;
? ? ? ? ? ? Object Format = System.Type.Missing;
? ? ? ? ? ? Object Encoding = System.Type.Missing;
? ? ? ? ? ? Object Visible = System.Type.Missing;
? ? ? ? ? ? Object OpenAndRepair = System.Type.Missing;
? ? ? ? ? ? Object DocumentDirection = System.Type.Missing;
? ? ? ? ? ? Object NoEncodingDialog = System.Type.Missing;
? ? ? ? ? ? Object XMLTransform = System.Type.Missing;

? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Microsoft.Office.Interop.Word.Document wordDoc =
? ? ? ? ? ? ? ? thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
? ? ? ? ? ? ? ? ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
? ? ? ? ? ? ? ? ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
? ? ? ? ? ? ? ? ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
? ? ? ? ? ? ? ? ref NoEncodingDialog, ref XMLTransform);

? ? ? ? ? ? ? ? thisDocument = wordDoc;
? ? ? ? ? ? ? ? wDoc = wordDoc;
? ? ? ? ? ? ? ? WApp = thisApplication;
? ? ? ? ? ? ? ? formFields = wordDoc.FormFields;
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ?
? ? ? ? ? ? }

? ? ? ? }

? ? ? ? #endregion



讀取文檔中標簽數據處理方法
1 獲取文檔中的標簽列表.把文檔對象的書簽列表讀到IEnumerator中.
2 通過枚舉推進的方式讀取每個書簽.
3 關閉文檔



//獲取標簽數據
? ? ? ? public string GetDocumentBookmarkData(string FileName)
? ? ? ? {

? ? ? ? ? ? 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;

? ? ? ? ? ? string str="";

? ? ? ? ? ? System.Collections.IEnumerator enu=wApp.ActiveDocument.Bookmarks.GetEnumerator();

? ? ? ? ? ? while(enu.MoveNext())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Microsoft.Office.Interop.Word.Bookmark bk = (Microsoft.Office.Interop.Word.Bookmark)enu.Current;

? ? ? ? ? ? ? ? str += "{"+bk.Name.ToString() + ":"+bk.Range.Text+"}";
? ? ? ? ? ? }

? ? ? ? ? ? object o = null;

? ? ? ? ? ? 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 str;
? ? ? ? }



往書簽中寫入數據
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 = bk.Name.ToString();
? ? ? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }

? ? ? ? ? ? object tempobject=null;
? ? ? ? ? ? int length = 0;
? ? ? ? ? ? for (i = 0; i < strbook.Length;i++ )
? ? ? ? ? ? {
? ? ? ? ? ? ? ? tempobject = strbook.ToString();

? ? ? ? ? ? ? ? if (wApp.ActiveDocument.Bookmarks.Exists(strbook.ToString()))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? wApp.ActiveDocument.Bookmarks.get_Item(ref tempobject).Select();

? ? ? ? ? ? ? ? ? ? wApp.Selection.Text = dt.Rows[0][strbook].ToString();

? ? ? ? ? ? ? ? ? ? length = dt.Rows[0][strbook].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";
? ? ? ? }



表格的插入方法
1? 表格的插入很簡單,圖表可參照
2? 表格插入后如何獲取表格請注意.用書簽的Range對象的Tables集合
插入表格數據



#region 插入表格數據
? ? ? ? private bool InsertTabletoBookmark(string objTable,ref Microsoft.Office.Interop.Word.Document wDoc, ref? Microsoft.Office.Interop.Word.Application WApp)
? ? ? ? {
? ? ? ? ? ? object oEndOfDoc = "\\endofdoc";
? ? ? ? ? ? object missing = System.Reflection.Missing.Value;

? ? ? ? ? ? object objBookmark = objTable;

? ? ? ? ? ? WApp.ActiveDocument.Bookmarks.get_Item(ref objBookmark).Select();

? ? ? ? ? ? Microsoft.Office.Interop.Word.Table table = wDoc.Tables.Add(WApp.Selection.Range, 3, 4, ref missing, ref missing);
? ? ? ? ? ? table.Cell(1, 1).Range.Text = "表:" + WApp.ActiveDocument.Bookmarks.get_Item(ref objBookmark).Range.Tables[1].Rows.Count;

? ? ? ? ? ? return true;
? ? ? ? }#endregion



結束語
由于生成文檔用戶修改后往往會丟掉書簽,數據就讀不到了,所以生成文檔還不能提交給用戶修改保存,那位有辦法的歡迎指教.

C#讀取和寫入word2003的書簽內容(怪,新,全)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日韩一区二区三区不卡 | 午夜婷婷| 国产成人综合自拍 | 国产亚洲新品一区二区 | 五月天婷婷在线播放 | 一级毛片视频播放 | 天天干免费视频 | 麻豆va一区二区三区久久浪 | 国产久草视频 | 又爽又黄又无遮挡的视频在线观看 | 日韩视频久久 | 欧美日韩一区二区在线观看视频 | 在线a视频 | 一级毛片看看 | 1000部羞羞禁止免费观看视频 | 中国一级毛片视频 | 苦瓜se影院在线视频网站 | 国产尤物| 日本aa在线观看 | 国产五月色婷婷六月丁香视频 | 99人中文字幕亚洲区 | 四虎最新在线 | 日韩在线视频一区 | 四虎影院观看视频在线观看 | 四虎精品免费国产成人 | 婷婷精品视频 | 日本欧美强乱视频在线 | 奇米色7777| 一级特黄aa大片欧美小说 | 开心激情五月婷婷 | 色综合亚洲七七久久桃花影院 | 最新777奇米影视四色 | 国产97在线视频观看 | 欧洲在线免费视频 | 亚洲国产成人久久一区二区三区 | 99视频精品在线 | 亚洲一区二区三区免费在线观看 | 国产精品二区页在线播放 | 午夜一级毛片看看 | 成人永久福利在线观看不卡 | 愉拍自拍视频在线播放 |