新聞中心
.NET操作Word可以用using Word來(lái)實(shí)現(xiàn)。基本上,vs.net將會(huì)自動(dòng)將 庫(kù)文件轉(zhuǎn)化為DLL組件,這樣我們只要在源碼中創(chuàng)建該組件對(duì)象即可達(dá)到操作Word的目的。

公司主營(yíng)業(yè)務(wù):做網(wǎng)站、網(wǎng)站制作、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)建站是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。創(chuàng)新互聯(lián)建站推出豐寧免費(fèi)做網(wǎng)站回饋大家。
要實(shí)現(xiàn),我們就需要Word的對(duì)象庫(kù)文件“MSWORD.OLB”(word 2000為MSWORD9.OLB),通常安裝了Office Word后,你就可以在office安裝目錄的Office10文件夾下面找到這個(gè)文件,當(dāng)我們將這個(gè)文件引入到項(xiàng)目后,我們就可以在源碼中使用各種操作函數(shù)來(lái)操作Word。具體做法是打開(kāi)菜單欄中的項(xiàng)目>添加引用>瀏覽,在打開(kāi)的“選擇組件”對(duì)話框中找到MSWORD.OLB后按確定即可引入此對(duì)象庫(kù)文件。
在CS代碼文件中對(duì)命名空間的應(yīng)用,如:using Word;.NET操作Word范例如下:
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using Word;
- namespace ExamSecure
- {
- ///
- /// ItemToDoc 的摘要說(shuō)明。
- ///
- public class ItemToDoc : System.Windows.Forms.Form
- {
- object strFileName;
- Object Nothing;
- Word.ApplicationClass myWordApp=new Word.ApplicationClass();
- Word.Document myWordDoc;
- string strContent="";
- private System.ComponentModel.Container components = null;
- public ItemToDoc()
- {
- //
- // Windows 窗體設(shè)計(jì)器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
- //
- }
- [STAThread]
- static void Main()
- {
- System.Windows.Forms.Application.Run(new ItemToDoc());
- }
- ///
- /// 清理所有正在使用的資源。
- ///
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if(components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows Form Designer generated code
- ///
- /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
- /// 此方法的內(nèi)容。
- ///
- private void InitializeComponent()
- {
- //
- // ItemToDoc
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(292, 273);
- this.Name = "ItemToDoc";
- this.Text = "ItemToDoc";
- this.Load += new System.EventHandler(this.ItemToDoc_Load);
- }
- #endregion
- private void ItemToDoc_Load(object sender, System.EventArgs e)
- {
- WriteFile();
- }
- private void WriteFile()
- {
- strFileName=System.Windows.Forms.Application.StartupPath+"\\試題庫(kù)【"+GetRandomString()+"】.doc";
- Object Nothing=System.Reflection.Missing.Value;
- myWordDoc=myWordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
- #region 將數(shù)據(jù)庫(kù)中讀取得數(shù)據(jù)寫入到word文件中
- strContent="試題庫(kù)\n\n\r";
- WriteFile(strContent);
- strContent="試題庫(kù)";
- WriteFile(strContent);
- #endregion
- //將WordDoc文檔對(duì)象的內(nèi)容保存為DOC文檔
- myWordDoc.SaveAs(ref strFileName,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
- //關(guān)閉WordDoc文檔對(duì)象
- myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
- //關(guān)閉WordApp組件對(duì)象
- myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
- }
- ///
- /// 獲取一個(gè)隨即字符串
- ///
- ///
- private string GetRandomString()
- {
- DateTime iNow=DateTime.Now;
- string strDate=iNow.ToString("yyyyMMddHHmmffff");
- Random ran=new Random();
- int iRan=Convert.ToInt32(10000*ran.NextDouble());
- string strRan=iRan.ToString();
- //位數(shù)不足則補(bǔ)0
- int iRanlen=strRan.Length;
- for(int i=0;i<4-iRanlen;i++)
- {
- strRan="0"+strRan;
- }
- return strDate+strRan;
- }
- ///
- /// 將字符串寫入到Word文件中
- ///
- /// 要寫入的字符串
- private void WriteFile(string str)
- {
- myWordDoc.Paragraphs.Last.Range.Text=str;
- }
- }
- }
以上就是.NET操作Word的實(shí)現(xiàn)代碼。
【編輯推薦】
- ASP.NET新手問(wèn)題總結(jié)
- 深入研究Repeater控件:最大的靈活性
- DataList控件入門介紹
- DataGrid Web控件運(yùn)作機(jī)制探秘
- 小議ASP.NET數(shù)據(jù)Web控件之間的相似性
網(wǎng)頁(yè)題目:.NET操作Word的實(shí)現(xiàn):usingWord
文章URL:http://www.5511xx.com/article/djjjpee.html


咨詢
建站咨詢
