c# aspx转为html,asp.net中aspx页面生成Html页面的方法

[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("");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值