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
}
}
新闻热点
疑难解答
图片精选