在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+"http://試題庫【"+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;
}
}
}
新聞熱點
疑難解答
圖片精選