国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

用.net操作word

2024-07-10 12:58:59
字體:
來源:轉載
供稿:網友


要操作word,我們就需要word的對象庫文件“msword.olb”(word 2000為msword9.olb),通常安裝了office word后,你就可以在office安裝目錄的office10文件夾下面找到這個文件,當我們將這個文件引入到項目后,我們就可以在源碼中使用各種操作函數來操作word。具體做法是打開菜單欄中的項目>添加引用>瀏覽,在打開的“選擇組件”對話框中找到msword.olb后按確定即可引入此對象庫文件,vs.net將會自動將 庫文件轉化為dll組件,這樣我們只要在源碼中創建該組件對象即可達到操作word的目的!

在cs代碼文件中對命名空間的應用,如:using word;范例如下:

using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using word;

namespace examsecure
{
///
/// itemtodoc 的摘要說明。
///
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 窗體設計器支持所必需的
//
initializecomponent();

//
// todo: 在 initializecomponent 調用后添加任何構造函數代碼
//
}
[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
///
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
///
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+"//試題庫【"+getrandomstring()+"】.doc";
object nothing=system.reflection.missing.value;
myworddoc=mywordapp.documents.add(ref nothing,ref nothing,ref nothing,ref nothing);

#region 將數據庫中讀取得數據寫入到word文件中

strcontent="試題庫/n/n/r";
writefile(strcontent);

strcontent="試題庫";
writefile(strcontent);


#endregion

//將worddoc文檔對象的內容保存為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);
//關閉worddoc文檔對象
myworddoc.close(ref nothing, ref nothing, ref nothing);
//關閉wordapp組件對象
mywordapp.quit(ref nothing, ref nothing, ref nothing);
}

///
/// 獲取一個隨即字符串
///
///
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();
//位數不足則補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;
}
}
}




發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松江区| 莱州市| 邯郸县| 张北县| 双鸭山市| 梧州市| 交城县| 榆社县| 泸溪县| 大石桥市| 丹巴县| 伊通| 苗栗市| 镇沅| 湟中县| 轮台县| 临江市| 高雄县| 涞水县| 定南县| 朝阳区| 临颍县| 浮山县| 雷波县| 温州市| 马山县| 平利县| 六安市| 元朗区| 五原县| 乌鲁木齐市| 五峰| 罗江县| 湟源县| 清丰县| 孝昌县| 留坝县| 开平市| 皋兰县| 嘉义市| 广元市|