Microsoft Word、Microsoft
Excel或者使用者定義的ActiveX文檔等構成的複合文檔,每一種文件類型都可以直
接進行編輯修改。 ActiveX文檔可以作為對象嵌入到WEB網頁中,在WEB上發布
用 <iframe width= height= border=yes/no src="c:/網頁路徑/yourword.doc或xls檔案"></iframe> 即可
http://www.codeproject.com/useritems/wordapplication.asp
本文是應在ASP.NET裡建立Microsoft Word文檔之需而寫的。這篇文章示範了在ASP.NET裡怎麼建立和修改Microsoft Word文檔。
[背景]
自動化是一種能讓各種語言編寫的(如:Visual Basic.NET或C#)應用程式在程式層級上控制其他應用程式。
對於Word的自動化允許你執行諸如建立新的文檔,向文檔裡添加文本,合併列印和格式化文檔這些操作。在Word和其他的Microsoft Office程式裡,那些通過使用者介面進行的可視化操作也可以通過程式層級的自動化來實現。
Word通過物件模型把這個程式可操作的功能向外提供了使用介面。
物件模型是一組類和方法的集合,這些類和方法與Word的邏輯組件構成對應。例如,他可能是應用程式物件,文檔對象,段落對象,每一個對象都包含了Word組件的功能。
[建立工程]
在.NET裡操作Word的第一步就是添加COM引用到你的工程裡,通過右鍵點擊Solution Explorer的Reference,Add Reference。選擇COM選項卡,尋找Microsoft Word 10.0 Object Library。點擊選擇,OK。
這將把封裝有Word的COM的程式集自動的添加到應用程式目錄裡。
現在,你可以建立一個Word的執行個體了:
Word.ApplicationClass oWordApp = new Word.ApplicationClass();
你可以調用Word提供給你的方法和屬性來操縱Word文檔。
學習如何使用Word,Excel,Powerpoint的物件模型最好的途徑就是使用在這些Office應用裡使用Macro Recorder:
1.在Tools菜單的Macro選項裡選擇 Record New Macro ,並且執行你有興趣的任務。
2.在Tools菜單的Macro選項裡選擇 Stop Recording。
3.如果你進行了紀錄,選擇Tools菜單的Macro選項裡的Macros,找到你記錄的宏,你可以編輯它。
上面的操作產生了VBA代碼來完成你記錄的任務。需要注意的是,宏在大多數情況下不是最好的代碼,但是它提供了一種便捷和可用的方法。
下面例子開啟並添加一寫文字:
object fileName = "c://database//test.doc";
object readOnly = false;
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(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#裡的開啟方法需要15個參數,並且每個參數必須被ref關鍵字所描述,而且是object類型。
第一個參數是檔案,名,在Visual Basic.NET裡通常是一個String,但是在在C#裡,它必須是一個包含有String的object,代碼是這樣的:
object fileName = "c://database//test.doc";
雖然我們僅需要使用Open方法的第一個參數,但是C#不允許使用預設參數,所以我們賦予它14個object類型的值:System.Reflection.Missing.Value
[使用摸版]
如果你需要自動的建立有通用格式的文檔,那你可以使用基於預格式化的摸版來建立新文檔,這樣可以方便很多。
在Word裡使用摸版而不是建立空文檔有兩個明顯的優點:
1.你可以更大程度的格式化文檔和控制文檔裡的對象。
2.可以用較少的代碼建立文檔。
通過使用摸版,你可以調整表格、段落和其他一些在文檔裡的對象的位置,同時包括格式化這些對象。通過使用自動化處理,你可以建立一個基於摸版的文檔,代碼如下:
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 - 上面使用的類
CreateDocModel.aspx - 建立基於使用書籤的摸版的新文檔的例子。
CreateNewDoc.aspx - 建立新文檔,並向其中添加一寫文本。
ModifyDocument.aspx - 開啟一個存在的文檔,並在末尾追加一些文本。
template/template1.dot - 摸版的例子(在CreateDocModel.aspx中使用到)
注意你用來儲存文檔的目錄,應該是可重寫的。
可以在 Web.config 裡修改這個路徑。