程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> 從ASP.NET得到Microsoft Word文檔的代碼

從ASP.NET得到Microsoft Word文檔的代碼

編輯:ASP.NET基礎
背景
自動化(Automation)是一個過程,它允許編程語言譬如Visual Basic.NET或C#寫的應用程序可以編程控制其它應用程序。自動化到Word允許你執行像創建新文檔,向文檔中添加文本,郵件合並,還有控制文檔格式這樣的操作。使用Word和其它Microsoft Office應用程序,幾乎所有你能在用戶面板上手動實現的操作都可以通過自動化編程實現。Word通過一個對象模型來實現這個編程功能性(programmatically functionality)。對象模型是一系列類和方法,它們提供和Word的邏輯組成部分相似的服務。例如,一個應用程序對象,一個文檔對象,和一個段落對象,這些每個都包含著Word的相應組成部分的功能性。

項目
在.NET中操作Word的第一步,你需要在你的項目中添加一個COM引用,這通過右擊解決方案窗口中的引用->添加引用。單擊COM標簽尋找Microsoft Word 10.0 Object Library。單擊“選擇”添加,單擊“確定”返回。
這會自動在你的應用程序文件夾中放置一個程序集(assembly)將COM接口邦定到Word。
現在,你可以生成一個Word應用程序的實例了。

Word.ApplicationClass oWordApp = new Word.ApplicationClass();
你可以調用Microsoft Word提供給你的很有趣的方法和屬性來操作Word格式的文檔。學習怎樣操縱Word,Excel和PowerPoint對象模型最好的方法就是,在這些Office應用程序中使用宏錄制器:
1、在“工具”菜單中的“宏”選項中選擇“錄制新宏”,然後執行你感興趣的任務。
2、在“工具”菜單中的“宏”選項中選擇“停止錄制”。
3、一旦你完成了錄制,選擇“工具”菜單中的“宏”選項下的“宏”,選擇你錄制的宏,單擊“編輯”。
這將將你帶入生成的VBA代碼,這些代碼完成了你記錄的任務。注意記錄下的宏在多數情況下不是最好的代碼,但它提供了一個快速和可用的例子。
例如要打開一個存在的文件加入一些文本:
復制代碼 代碼如下:
object fileName = "c:\\database\\test.doc";
object;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
Word.ApplicationClass oWordApp = new Word.ApplicationClass();
Word.Document oWordDoc = oWordApp.Documents.Open(ref fileName,
ref missing,ref readOnly,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible,
ref missing,ref missing,ref missing);
oWordDoc.Activate();
oWordApp.Selection.TypeText("This is the text");
oWordApp.Selection.TypeParagraph();
oWordDoc.Save();
oWordApp.Application.Quit(ref missing, ref missing, ref missing);

或者要打開一個新的文檔然後保存它:
復制代碼 代碼如下:
Word.ApplicationClass oWordApp = new Word.ApplicationClass();
Word.Document oWordDoc = oWordApp.Documents.Add(ref missing,
ref missing,ref missing, ref missing);
oWordDoc.Activate();
oWordApp.Selection.TypeText("This is the text");
oWordApp.Selection.TypeParagraph();
oWordDoc.SaveAs("c:\\myfile.doc");
oWordApp.Application.Quit(ref missing, ref missing, ref missing);

在C#中,Word文檔類的open方法定義為:Open(ref object, ref object, ref object, ref object, ref object, ref object,ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object)。這說明C#的Open方法接受15個必要參數,每個參數都必須以ref關鍵字為前綴而且每個參數都必須是Object類型的。因為第一個參數是一個文件名,通常在Visual Basic.NET中的一個String值,我們必須聲明一個Object類型的變量來保存C#的string值,代碼如下:

object fileName = "c:\\database\\test.doc";
盡管我們在Open方法中只需要使用第一個參數,但是記住C#不允許可選參數,所以我們以Object類型的變量的形式提供余下的14個參數,它們保存System.Reflection.Missing.Value的值。

使用模板
如果你使用自動化來創建都是一致格式的文檔,使用預定義模板來處理新的文檔將會很方便。在你的Word自動化客戶程序中使用模板與不用模板相比,有兩個顯著的優點:
·對於你文檔的格式和對象位置上你可以擁有更多的控制權
·你可以使用更少的代碼來建立你的文檔
通過使用模板,你可以調整文檔中表格、段落還有其它對象的位置,還有也可以調整這些對象的格式。通過使用自動化,你可以創建一個基於你的模板的文檔,而只用如下的代碼:

Word.ApplicationClass oWordApp = new Word.ApplicationClass();
object oTemplate = "c:\\MyTemplate.dot";
oWordDoc = oWordApp.Documents.Add(ref oTemplate,
ref Missing,ref Missing, ref Missing);
在你的模版中,你可以定義書簽,這樣你的自動化客戶程序可以在文檔中的特定位置填入可變的文本,如下:

object oBookMark = "MyBookmark";
oWordDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";
使用模板的另一個優點是你可以創建你希望在運行時應用的存儲格式風格,如下:

object oStyleName = "MyStyle";
oWordDoc.Bookmarks.Item(ref oBookMark).Range.set_Style(ref oStyleName);
使用CCWordApp類
這個項目包含一個文件:CCWordAPP.cs。我不想每次都寫代碼來插入文本,打開一個文件,等等……所以我決定寫一個CCWordApp類來包括多數重要的方法。下面是對這個類和它的方法的簡要描述。
復制代碼 代碼如下:
public class CCWordApp
{
//it's a reference to the COM object of Microsoft Word Application
private Word.ApplicationClass oWordApplic;
// it's a reference to the document in use
private Word.Document oWordDoc;
// Activate the interface with the COM object of Microsoft Word
public CCWordApp();
// Open an existing file or open a new file based on a template
public void Open( string strFileName);
// Open a new document
public void Open( );
// Deactivate the interface with the COM object of Microsoft Word
public void Quit( );
// Save the document
public void Save( );
//Save the document with a new name as HTML document
public void SaveAs(string strFileName );
// Save the document in HTML format
public void SaveAsHtml(string strFileName );
// Insert Text
public void InsertText( string strText);
// Insert Line Break
public void InsertLineBreak( );
// Insert multiple Line Break
public void InsertLineBreak( int nline);
// Set the paragraph alignment
// Possible values of strType :"Centre", "Right", "Left", "Justify"
public void SetAlignment(string strType );
// Set the font style
// Possible values of strType :"Bold","Italic,"Underlined"
public void SetFont( string strType );
// Disable all the style
public void SetFont( );
// Set the font name
public void SetFontName( string strType );
// Set the font dimension
public void SetFontSize( int nSize );
// Insert a page break
public void InsertPagebreak();
// Go to a predefined bookmark
public void GotoBookMark( string strBookMarkName);
// Go to the end of document
public void GoToTheEnd( );
// Go to the beginning of document
public void GoToTheBeginning( );

這樣,打開一個已有的文件的操作就是:
CCWordApp test ;
test = new CCWordApp();
test.Open ("c:\\database\\test.doc");
test.InsertText("This is the text");
test.InsertLineBreak;
test.Save ();
test.Quit();
細節
示例項目包括:
CCWordApp.cs - the class
CreateDocModel.aspx: 創建一個基於模板的文檔和使用書簽的例子。
CreateNewDoc.aspx: 創建一個文檔和插入一些文本的例子。
ModifyDocument.aspx: 打開一個已有文檔然後在後面添加一些文本的例子。
template\template1.dot: 一個模板的例子(在CreateDocModel.aspx中使用)。
記住一點,你保存文件的目錄必須是可寫的。請查看Web.config文件來修改路徑。
引用
Microsoft Word Objects
Converting Microsoft Office VBA Macros to Visual Basic .NET and C#
HOWTO: Automate Microsoft Word to Perform a Mail Merge from Visual Basic .NET
A Primer to the Office XP Primary Interop Assemblies
HOWTO: Find and Use Office Object Model Documentation
Creating and Opening Microsoft Word Documents from .NET using C#
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved