国产黄色免费网站,人人干人人干人人干,免费大黄网站在线看,国产情侣一区二区三区,99精品国产福利免费一区二区,国产亚洲成归v人片在线观看,伊人88

2023信創(chuàng)獨(dú)角獸企業(yè)100強(qiáng)
全世界各行各業(yè)聯(lián)合起來,internet一定要實(shí)現(xiàn)!

在ASP.NET中使用Microsoft Word文檔

2004-02-11 eNet&Ciweek

  本文是應(yīng)在ASP.NET里創(chuàng)建Microsoft Word文檔之需而寫的。這篇文章演示了在ASP.NET里怎么創(chuàng)建和修改Microsoft Word文檔。

  [背景]

  自動(dòng)化是一種能讓各種語言編寫的(如:Visual Basic.NET或C#)應(yīng)用程序在程序級(jí)別上控制其他應(yīng)用程序。

  對(duì)于Word的自動(dòng)化允許你執(zhí)行諸如創(chuàng)建新的文檔,向文檔里添加文本,郵件合并和格式化文檔這些操作。在Word和其他的Microsoft Office程序里,那些通過用戶接口進(jìn)行的可視化操作也可以通過程序級(jí)別的自動(dòng)化來實(shí)現(xiàn)。

  Word通過對(duì)象模型把這個(gè)程序可操作的功能向外提供了使用接口。

  對(duì)象模型是一組類和方法的集合,這些類和方法與Word的邏輯組件構(gòu)成對(duì)應(yīng)。例如,他可能是應(yīng)用程序?qū)ο螅臋n對(duì)象,段落對(duì)象,每一個(gè)對(duì)象都包含了Word組件的功能。

  [建立工程]

  在.NET里操作Word的第一步就是添加COM引用到你的工程里,通過右鍵點(diǎn)擊Solution Explorer的Reference,Add Reference。選擇COM選項(xiàng)卡,查找Microsoft Word 10.0 Object Library。點(diǎn)擊選擇,OK。

  這將把封裝有Word的COM的程序集自動(dòng)的添加到應(yīng)用程序目錄里。

  現(xiàn)在,你可以建立一個(gè)Word的實(shí)例了:

  Word.ApplicationClass oWordApp = new Word.ApplicationClass();

  你可以調(diào)用Word提供給你的方法和屬性來操縱Word文檔。

  學(xué)習(xí)如何使用Word,Excel,Powerpoint的對(duì)象模型最好的途徑就是使用在這些Office應(yīng)用里使用Macro Recorder:

  1.在Tools菜單的Macro選項(xiàng)里選擇 Record New Macro ,并且執(zhí)行你有興趣的任務(wù)。

  2.在Tools菜單的Macro選項(xiàng)里選擇 Stop Recording。

  3.如果你進(jìn)行了紀(jì)錄,選擇Tools菜單的Macro選項(xiàng)里的Macros,找到你記錄的宏,你可以編輯它。

  上面的操作產(chǎn)生了VBA代碼來完成你記錄的任務(wù)。需要注意的是,宏在大多數(shù)情況下不是最好的代碼,但是它提供了一種便捷和可用的方法。

  下面例子打開并添加一寫文字:

  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);   

  如果創(chuàng)建一個(gè)新文檔并保存是這樣寫的: 

  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個(gè)參數(shù),并且每個(gè)參數(shù)必須被ref關(guān)鍵字所描述,而且是object類型。

  第一個(gè)參數(shù)是文件,名,在Visual Basic.NET里通常是一個(gè)String,但是在在C#里,它必須是一個(gè)包含有String的object,代碼是這樣的:

  object fileName = "c:\\database\\test.doc";   

  雖然我們僅需要使用Open方法的第一個(gè)參數(shù),但是C#不允許使用默認(rèn)參數(shù),所以我們賦予它14個(gè)object類型的值:System.Reflection.Missing.Value  

  [使用模版]  

  如果你需要自動(dòng)的建立有通用格式的文檔,那你可以使用基于預(yù)格式化的摸版來建立新文檔,這樣可以方便很多。

  在Word里使用摸版而不是建立空文檔有兩個(gè)明顯的優(yōu)點(diǎn):  

  1.你可以更大程度的格式化文檔和控制文檔里的對(duì)象。

  2.可以用較少的代碼建立文檔。
  
  通過使用摸版,你可以調(diào)整表格、段落和其他一些在文檔里的對(duì)象的位置,同時(shí)包括格式化這些對(duì)象。通過使用自動(dòng)化處理,你可以建立一個(gè)基于摸版的文檔,代碼如下:  

  Word.ApplicationClass oWordApp = new Word.ApplicationClass();

  object oTemplate = "c:\\MyTemplate.dot";

  oWordDoc = oWordApp.Documents.Add(ref oTemplate, ref Missing,ref Missing, ref Missing);
  在你使用的摸版里,你可以定義一些記號(hào),自動(dòng)化處理將向這些位置填充文本,如下: 

  object oBookMark = "MyBookmark";

  oWordDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";
  

  使用摸版的另一個(gè)優(yōu)點(diǎn)是你可以創(chuàng)建和保存那些在運(yùn)行過程中你想要的格式化樣式,如下:  

  object oStyleName = "MyStyle";

  oWordDoc.Bookmarks.Item(ref oBookMark).Range.set_Style(ref oStyleName);   

  [使用CCWordApp類]

  在工程中包含了CCWordApp.cs這個(gè)文件,我不想總是在寫象插入文本,打開文檔這樣的代碼。

  所以,我決定把一些最重要的功能封裝到CCWordApp類里去。

  下面代碼簡(jiǎn)要描述了這個(gè)類和他的功能:

  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( );

  打開一個(gè)存在的文件的代碼將是這樣的:

  CCWordApp test ;

  test = new CCWordApp();

  test.Open ("c:\\database\\test.doc");

  test.InsertText("This is the text");

  test.InsertLineBreak;

  test.Save ();

  test.Quit();

  [細(xì)節(jié)]  

  演示工程包含:

  CCWordApp.cs - 上面使用的類

  CreateDocModel.aspx - 建立基于使用書簽的摸版的新文檔的例子。

  CreateNewDoc.aspx - 建立新文檔,并向其中添加一寫文本。

  ModifyDocument.aspx - 打開一個(gè)存在的文檔,并在末尾追加一些文本。

  template\template1.dot - 摸版的例子(在CreateDocModel.aspx中使用到)

  注意你用來保存文檔的目錄,應(yīng)該是可重寫的。

  可以在 Web.config 里修改這個(gè)路徑。

相關(guān)頻道: eNews

您對(duì)本文或本站有任何意見,請(qǐng)?jiān)谙路教峤唬x謝!

投稿信箱:tougao@enet16.com