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

來源:互聯網
上載者:User

背景
自動化(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#

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.