[C#] 纯文本查看 复制代码//将动态的ASP.NET页面转化成静态的页面

private void CreateHtml()

{

try

{

//设置文件名

string filename = lblApplyTime.Text.Replace("/", "-") + "-" + lblName.Text + ".html";

//获取当前文件路径(服务器端)

string path = HttpContext.Current.Server.MapPath(@"~/WebManage/Template.htm");

//通过路径获取模板文件内容

using (StreamReader r = new StreamReader(path))

{

String line = null;

//大量字符串拼接或频繁对某一字符串进行操作时最好使用 StringBuilder

StringBuilder str = new StringBuilder();

//开始读取模板文件内容

while ((line = r.ReadLine()) != null)

{

str.AppendLine(line);//这里就是一行一行的拼接字符串

}

r.Close();

str.Replace("$Department$", lblDepartment.Text);//开始替换文本

str.Replace("$ApplyTime$", lblApplyTime.Text);

str.Replace("$Name$", lblName.Text);

str.Replace("$WorkDate$", lblWorkTime.Text);

str.Replace("$WorkTime$", lblWorkTime.Text);

str.Replace("$Reason$", lblReason.Text);

//设置文件路径

string htmlpath = HttpContext.Current.Server.MapPath("~/HtmlFile/");

string paths = htmlpath + filename;

//实例化,并制定文件名称规则和生成文件路径

StreamWriter w = new StreamWriter(paths, false, Encoding.GetEncoding("utf-8"));

w.Write(str);//这里才真正开始创建文件

w.Close();//关闭

w.Dispose();

}

}

catch (Exception ex)

{

Response.Write("");

}

}

更多推荐