zoukankan      html  css  js  c++  java
  • 根据模板生成网页

    using System.Text;
    using System.IO;

                string mbPath =Server.MapPath("template.htm");
                Encoding code = Encoding.GetEncoding("gb2312");
                StreamReader sr = null;
                StreamWriter sw = null;
                string str = null;
     
                //读取
                try
                {
                    sr = new StreamReader(mbPath, code);
                    str = sr.ReadToEnd();
     
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    sr.Close();
                }
     
                //根据时间自动重命名,扩展名也可以自行修改
                string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
                str = str.Replace("$title$", txtTitle.Text);//替换Title
                str = str.Replace("$content$", txtContent.Text);//替换content
     
                //生成文件
                try
                {
                    sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
     
                    sw.Write(str);
                    sw.Flush();
     
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    sw.Close();
                    Response.Write("<a href=htm/"+fileName+" target=_blank>"+fileName+"</a>已经生成!");
                }

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    RequireJS 模块化加载框架使用
    MySQL数据类型(最大值 和 最小值)
    utf-8的中文是一个字符占几个字节
    spring的事务是什么?与数据库的事务是否一样
    事务和锁机制是什么关系? 开启事务就自动加锁了吗?
    关于数据库主键和外键
    浅谈Mysql共享锁、排他锁、悲观锁、乐观锁及其使用场景
    TCP/IP协议
    分库分表分区需要考虑的问题及方案
    Redis(十一):Redis的事务功能详解
  • 原文地址:https://www.cnblogs.com/starcrm/p/1311014.html
Copyright © 2011-2022 走看看