原文发布时间为:2009-09-30 —— 来源于本人的百度文章 [由搬家工具导入]

采用模板法:【例子中的两个页面以及生成的页面均在同一个目录,自己可以去改】

模板Template.htm:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>标题:$htmlformat[0]</title>
    <meta http-equiv="Content-Type" content="text/html;   charset=gb2312" />
</head>
<body>

<div style="text-align: center;">
        内容
    </div>
    <div style="text-align: center; font-size: 16px; font-weight: bold;$htmlformat[1]">$htmlformat[2]
    </div>
</body>
</html>

后台程序:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;

public partial class ChangeToHtml : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ToHTML();
    }

    protected void ToHTML()
    {
        string[] format = new string[3];//定义和htmlformat标记数目一致的数组    
        StringBuilder htmltext = new StringBuilder();
        try
        {
             using (StreamReader sr = new StreamReader(Server.MapPath("Template.htm"), Encoding.GetEncoding("GB2312")))
            {
                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    htmltext.Append(line);
                }
                sr.Close();
            }
        }
        catch
        {
            Response.Write("<script>alert('读取文件错误')</script>");
        }

        //--------给标记数组赋值-----------//   

        format[0] = "静态页面生成";
        format[1] = "color:red";   
        format[2] = "生成了静态HTML";

        //-------替换html里的标记为你想加的内容-----------//    

        for (int i = 0; i < 3; i++)
        {
            htmltext.Replace("$htmlformat[" + i + "]", format[i]);
        }

        //----------生成html文件------------//

        try
        {
            using (StreamWriter sw = new StreamWriter(Server.MapPath(DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm"), false, Encoding.GetEncoding("GB2312")))
            {
                sw.WriteLine(htmltext);
                sw.Flush();
                sw.Close();
            }
        }
        catch
        {
            Response.Write("<script>alert('文件生成失败')</script>");
        }
    }
}

最新文章

  1. Spring学习(一)
  2. Servlet生命周期
  3. 读《编写可维护的JavaScript》第六章总结
  4. Docker实践(6)—CentOS7上部署Kubernetes
  5. Java Hour 15 以写小说的心态
  6. github中国版本coding.net 的部署和使用
  7. sql server中的decimal或者numeric的精度问题
  8. POI使用cell.getCellStyle()设置指定单元格颜色,但是其它没有指定的单元格也会变色
  9. jquery实现当前页面编辑
  10. git的入门使用操作
  11. FFmpeg的Android平台移植—编译篇
  12. OC类方法的调用
  13. (译文)开始学习Webpack-应用TypeScript,配置热加载和Source Map
  14. Tomcat内核之Tomcat的类加载器
  15. HDU - 1050
  16. NGINX 502错误排查(转)
  17. [转]PHP时区/MySql时区/Linux时区
  18. Linux搭建好apache后,只有本地能访问,局域或外网不能访问
  19. mysql异常
  20. springboot-8- 日志配置

热门文章

  1. 【图论】[USACO]控制公司 Controlling Companies
  2. http请求中客户端真实的ip
  3. PHP获取接下来一周的日期
  4. vue.js 图片预览
  5. 25.VUE学习之-单击和双击事件
  6. stm32的systick原理与应用
  7. 二分答案:Poweroj2461-入门基础之二分答案(二分法的应用)
  8. 不同项目同一浏览器访问 导致Session覆盖 登录后点击就退出登录问题
  9. C#入门篇-3:数据类型及转换
  10. windows服务安装卸载