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
}
}