namespace bankaccount.test
{
 /// <summary>
 /// htmlwriter 的摘要說明。
 /// </summary>
 public class htmlwriter : system.web.ui.page
 {
  private system.data.sqlclient.sqldataadapter myadapter;
  private system.data.sqlclient.sqlconnection myconn;
  private system.data.datatable mydt;
  private string strconn = configurationsettings.appsettings["cns"];
  private void page_load(object sender, system.eventargs e)
  {
   try
   {
    string sqlstr = "select cmp_description from company where cmp_number = '123567'";
    string cription="";
    mydt = new datatable();
    myconn = new sqlconnection(strconn);
    myconn.open();
    myadapter = new sqldataadapter(sqlstr,myconn);
    myadapter.fill(mydt);
    if(mydt.rows.count>0)
    {
     cription = mydt.rows[0]["cmp_description"].tostring();
    }
    string title = "這是動態生成的靜態頁面";
    string dhtml = "<!doctype html public /"-//w3c//dtd html 4.0 transitional//cn/"><html><head>"+
     "<meta http-equiv=/"content-type/" content=/"text/html; charset=gb2312/">"+
     "<title>"+ title +"</title></head><body topmargin=0>";
    dhtml += "<table width=760 valign=top border=1 cellspacing=1 cellpadding=0 align=center>"+
     "<tr><td height=5>"+ title  +"</td></tr><tr><td height=200 align=center>"+ cription +"</td></tr></table>"+
     "</body></html>";
    string filename = datetime.now.tostring("yyyymmddhhmmss")+".html";//動態的文件名
    string filepath = server.mappath(@"/bankaccount/test/");
    string cname = datetime.now.year.tostring() + datetime.now.month.tostring();
    string filecreatepath = filepath + cname + @"/";//頁面要保存的路徑
    //動態生成的靜態頁面按年月保存本年本月的文件夾中
    if (directory.exists(filecreatepath)) //判斷當月的文件夾是否存在,
    {
     //調用創建html靜態頁面方法
     create_html(filecreatepath+filename,dhtml);
    }
    else
    {
     //創建頁面保存到的文件夾
     directoryinfo di = directory.createdirectory(filecreatepath);
     create_html(filecreatepath+filename,dhtml);
    }
   }
   catch(ioexception ex)
   {
    throw ex;
   }
  }
  private void create_html(string allfilename,string htmlcode)
  {
   filestream createfile = new filestream(allfilename,filemode.createnew);
   streamwriter sw = new streamwriter(createfile);
   sw.writeline(htmlcode);//將拼好的html代碼寫入頁面中
   sw.close();
   page.registerstartupscript("","<script>window.alert('the file created successfully.!')</script>");
  }
  #region web 窗體設計器生成的代碼
  override protected void oninit(eventargs e)
  {
   //
   // codegen: 該調用是 asp.net web 窗體設計器所必需的。
   //
   initializecomponent();
   base.oninit(e);
  }
  
  /// <summary>
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void initializecomponent()
  {    
   this.load += new system.eventhandler(this.page_load);
  }
  #endregion
 }
}
新聞熱點
疑難解答
圖片精選