本文是應在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 里修改這個路徑。
|
新聞熱點
疑難解答
圖片精選