zoukankan      html  css  js  c++  java
  • ASP.NET C#生成HTML页类

        /// <summary>
        /// 生成HTML页类
        /// </summary>
        public class ProduceHtmlPage
        {
            public static bool WriteFile(string strTopic,string strArticleClass,string strArticleClassChild,string strContent,string strSource,string strAuthor,string strEditor,string strPubDate,string strPageUrl)
            {
                string path = HttpContext.Current.Server.MapPath("/bluedn/html/");
                Encoding code = Encoding.GetEncoding("gb2312");
                //读取模板文件
                string temp = HttpContext.Current.Server.MapPath("/bluedn/template/NewsInfo.htm");
                StreamReader sr=null;
                StreamWriter sw=null;
                string str=""; 
                try
                {
                    sr = new StreamReader(temp, code);
                    str = sr.ReadToEnd(); //读取文件
                }
                catch(Exception exp)
                {
                    HttpContext.Current.Response.Write(exp.Message);
                    HttpContext.Current.Response.End();
                    sr.Close();
                }

                string htmlfilename= strPageUrl+".htm";
                //DateTime.Now.ToString("yyyyMMddHHmmss")+".htm";
                //替换内容
                //这时,模板文件已经读入到名称为str的变量中了

                //模板页中的新闻标题
                str = str.Replace("$t_Topic$",strTopic);
                //一级栏目
                str = str.Replace("$t_ArticleClass$",strArticleClass);
                //二级栏目
                str = str.Replace("$t_ArticleClassChild$",strArticleClassChild);
                //新闻来源
                str = str.Replace("$t_Source$",strSource);
                //作者
                str = str.Replace("$t_Author$",strAuthor);
                //编辑
                str = str.Replace("$t_Editor$",strEditor);
                //发布日期
                str = str.Replace("$t_PubDate$",strPubDate);
                //新闻内容
                str = str.Replace("$t_Content$",strContent);

                //写文件
                try
                {

     

                    sw = new StreamWriter(path + htmlfilename , false, code);
                    sw.Write(str);
                    sw.Flush();
                }
                catch(Exception ex)
                {
                    HttpContext.Current.Response.Write(ex.Message);
                    HttpContext.Current.Response.End();
                }
                finally
                {
                    sw.Close();
                }
                return true;
            }
        }
  • 相关阅读:
    @Configuration和@Component区别
    ElasticSearch中text和keyword类型的区别
    Elasticsearch实战篇——Spring Boot整合ElasticSearch
    Elasticsearch入门篇——基础知识2
    学习Elasticsea从零学Elasticsearch系列——基础概念rch系列——(1)基础概念
    开发工具:IDEA类和方法注释模板设置(非常详细)
    项目中时间的处理,到最后的数据库
    详细刨析---电商系统之订单系统设计
    记录一下自己用springboot整合阿里巴巴开源的EasyExcel实现导入导出功能
    Springboot整合easyExcel导入导出Excel
  • 原文地址:https://www.cnblogs.com/cosiray/p/1558515.html
Copyright © 2011-2022 走看看