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

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條評論
主站蜘蛛池模板: 免费播放一区二区三区 | 久久精品国产欧美 | 97看片网| 久久只精品| 亚洲成在人线中文字幕 | 国产午夜精品福利视频 | 奇米影视四色7777 | 久久成人免费观看全部免费 | 亚洲激情小视频 | 国产在线一区视频 | 精品国产品香蕉在线观看 | 99久久综合精品免费 | 久久午夜神器 | 久草新免费 | 一级毛片黄色片 | 久久这里只有免费精品6www | 欧美高清无砖专区欧美精品 | 波多野结衣在线一区二区 | 九九精品在线观看 | 国产精品视频在线免费观看 | 久久好看视频 | 久久综合网址 | 99看片网| 久久青草免费91线频观看不卡 | 久久99精品久久久久久野外 | 2021中文字幕亚洲精品 | 国产高清在线精品二区一 | 午夜视频免费国产在线 | 日本精品在线观看 | 五月婷婷之综合激情 | 亚洲伊人久久综合一区二区 | 亚洲综合专区 | 香蕉在线精品一区二区 | 热久久只有精品 | 一级毛片在线免费观看 | 欧美激情视频二区三区 | 香蕉在线精品亚洲第一区 | 激情在线观看视频 | 成人国产精品视频频 | 亚洲天堂h | 亚洲香蕉久久一区二区三区四区 |