成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

當前位置:首頁>>開發編程>>VS.NET>>新聞內容
在ASP.NET中使用Microsoft Word文檔
作者:Michela 發布時間:2003-10-11 14:13:32 文章來源:codeproject

[來源]http://www.codeproject.com/useritems/wordapplication.asp

[作者]Michela

[下載本文中使用的演示工程] 工程文件 大小:309 Kb

[介紹]

    本文是應在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 里修改這個路徑。

[參考文獻]


最新更新
·C#中使用Split分隔字符串的技
·VS2008開發中Windows Mobile
·PC機和移動設備上絕對路徑的
·C#程序加殼的方法(使用Sixx
·當前上下文中不存在名稱Conf
·請插入磁盤:Visual Studio 2
·用VS.NET讀取Flash格式文件信
·在ASP.NET中使用AJAX的簡單方
·VS.NET 2005中常用的一些代碼
·安裝VS.NET 2005 SP1補丁全攻
相關信息
畫心
愚愛
偏愛
火苗
白狐
畫沙
犯錯
歌曲
傳奇
稻香
小酒窩
獅子座
小情歌
全是愛
棉花糖
海豚音
我相信
甩蔥歌
這叫愛
shero
走天涯
琉璃月
Nobody
我愛他
套馬桿
愛是你我
最后一次
少女時代
灰色頭像
斷橋殘雪
美了美了
狼的誘惑
我很快樂
星月神話
心痛2009
愛丫愛丫
半城煙沙
旗開得勝
郎的誘惑
愛情買賣
2010等你來
我叫小沈陽
i miss you
姑娘我愛你
我們都一樣
其實很寂寞
我愛雨夜花
變心的玫瑰
犀利哥之歌
你是我的眼
你是我的OK繃
貝多芬的悲傷
哥只是個傳說
丟了幸福的豬
找個人來愛我
要嫁就嫁灰太狼
如果這就是愛情
我們沒有在一起
寂寞在唱什么歌
斯琴高麗的傷心
別在我離開之前離開
不是因為寂寞才想你
愛上你等于愛上了錯
在心里從此永遠有個你
一個人的寂寞兩個人的錯
主站蜘蛛池模板: 台中市| 孙吴县| 济阳县| 维西| 越西县| 凌源市| 芦山县| 兴宁市| 尼玛县| 华容县| 建湖县| 武安市| 阿坝县| 磐石市| 资中县| 朝阳县| 宜君县| 宁国市| 黑龙江省| 呼和浩特市| 遂平县| 盘山县| 冕宁县| 伊宁县| 衢州市| 治县。| 镇康县| 铁力市| 闻喜县| 翼城县| 温州市| 汝州市| 平阴县| 冀州市| 芦溪县| 顺平县| 焦作市| 辛集市| 菏泽市| 黄大仙区| 阿拉尔市|