生成静态页面的c#类

写了个生成静态页面的类,其实和前2篇一样是根据数据库的数据,然后替换模板页的例子,当然可以根据需要把类改的更加通用一些:

class RankingSet:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using HoWaveDbHelper;
  11. using System.Text;
  12. using System.IO;
  13. using System.Data.SqlClient;
  14. /// <summary>
  15. /// RankingSet 的摘要说明
  16. /// </summary>
  17. public class RankingSet
  18. {
  19.     string readUrl = "#";
  20.     string url = "#";
  21.     string htmStr = "失败";
  22.     public RankingSet()
  23.     {
  24.     }
  25.     public RankingSet(string readUrl, string url)
  26.     {
  27.         this.readUrl = readUrl;
  28.         this.url = url;
  29.     }
  30.     public string RankingSetStr()
  31.     {
  32.         htmStr = ReadHtml(readUrl);
  33.        return  RankingSetStr(htmStr);
  34.     }
  35.     public string RankingSetStr(string htmStr)
  36.     {
  37.         if (htmStr != "失败")
  38.         {
  39.             DataSet DsDay, DsWeek, DsMoon;
  40.             GetRanking(out  DsDay, out DsWeek, out DsMoon);
  41.             string strDiv = GetAllDiv(DsDay, DsWeek, DsMoon,htmStr);
  42.             return strDiv;
  43.         }
  44.         else
  45.         {
  46.             return "";
  47.         }
  48.     }
  49.     public string IntoHtm()
  50.     {
  51.         string strDiv = RankingSetStr();
  52.         if(strDiv=="")
  53.             return "写入失败!";
  54.         return InsertHtml(url,strDiv);
  55.     }
  56.     public string IntoHtm(string htmStr)
  57.     {
  58.         string strDiv = RankingSetStr(htmStr);
  59.         if (strDiv == "")
  60.             return "写入失败!";
  61.         return InsertHtml(url, strDiv);
  62.     }
  63.     /// <summary>
  64.     /// 分别获取三钟排行的DataSet
  65.     /// </summary>
  66.     /// <param name="DsDay">日排行</param>
  67.     /// <param name="DsWeek">周排行</param>
  68.     /// <param name="DsMoon">月排行</param>
  69.     private void GetRanking(out DataSet DsDay, out DataSet DsWeek, out DataSet DsMoon)
  70.     {
  71.         SqlParameter[] spDay ={ Product_Click.SqlParameters("@intTop", SqlDbType.Int, 0, 11), Product_Click.SqlParameters("@type", SqlDbType.Int, 0, 1) };
  72.         SqlParameter[] spWeek ={ Product_Click.SqlParameters("@intTop", SqlDbType.Int, 0, 11), Product_Click.SqlParameters("@type", SqlDbType.Int, 0, 2) };
  73.         SqlParameter[] spMoon ={ Product_Click.SqlParameters("@intTop", SqlDbType.Int, 0, 11), Product_Click.SqlParameters("@type", SqlDbType.Int, 0, 3) };
  74.         DsDay = HoWaveDbHelper.DbHelperSQL.RunProcedure("S_HLShop_Product_Ranking", spDay, "strDay");
  75.         DsWeek = HoWaveDbHelper.DbHelperSQL.RunProcedure("S_HLShop_Product_Ranking", spWeek, "strWeek");
  76.         DsMoon = HoWaveDbHelper.DbHelperSQL.RunProcedure("S_HLShop_Product_Ranking", spMoon, "strMoon");
  77.     }
  78.     /// <summary>
  79.     /// 生成HTML
  80.     /// </summary>
  81.     /// <param name="strDiv">要写入文本的字符串</param>
  82.     /// <param name="url">文本路径</param>
  83.     public string InsertHtml(string url,string strDiv)
  84.     {
  85.         if (strDiv == "")
  86.             return "写入失败!";
  87.         using (StreamWriter sw = new StreamWriter(url, false, System.Text.Encoding.GetEncoding("utf-8")))
  88.         {
  89.             try
  90.             {
  91.                 sw.Write(strDiv);
  92.                 return "写入成功!";
  93.             }
  94.             catch
  95.             {
  96.                 return "写入失败!";
  97.             }
  98.             finally
  99.             {
  100.                 sw.Flush();
  101.                 sw.Close();
  102.             }
  103.         }
  104.     }
  105.     /// <summary>
  106.     /// 读取HTML
  107.     /// </summary>
  108.     /// <param name="url">文本路径</param>
  109.     public string ReadHtml(string readUrl)
  110.     {
  111.         //Encoding code = Encoding.GetEncoding("gb2312");
  112.         Encoding code = System.Text.Encoding.Default;
  113.         StreamReader sr = null;
  114.        // StreamWriter sw = null;
  115.         //读取
  116.         try
  117.         {
  118.             sr = new StreamReader(readUrl, code);
  119.             htmStr = sr.ReadToEnd();
  120.         }
  121.         catch
  122.         {
  123.         }
  124.         finally
  125.         {
  126.             sr.Close();
  127.         }
  128.         return htmStr;
  129.     }
  130.   /// <summary>
  131.   /// 所有类排行数据填充
  132.  /// </summary>
  133.     /// <param name="DsDay">日排行</param>
  134.     /// <param name="DsWeek">周排行</param>
  135.     /// <param name="DsMoon">月排行</param>
  136.   /// <param name="htmStr">网页内容</param>
  137.   /// <returns></returns>
  138.     private string GetAllDiv(DataSet DsDay, DataSet DsWeek, DataSet DsMoon, string htmStr)
  139.     {
  140.             string dataStr = htmStr;
  141.             dataStr = GetDiv(1, DsDay, dataStr);
  142.             dataStr = GetDiv(2, DsWeek, dataStr);
  143.             dataStr = GetDiv(3, DsMoon, dataStr);
  144.             return dataStr;
  145.     }
  146.     /// <summary>
  147.     /// 具体类排行数据填充
  148.     /// </summary>
  149.     /// <param name="tit">类别</param>
  150.     /// <param name="Ds">数据</param>
  151.     /// <returns></returns>
  152.     private string GetDiv(int tit, DataSet Ds, string htmStr)
  153.     {
  154.         int strLen = 8;
  155.         if (Ds.Tables.Count == 0)
  156.             return htmStr;
  157.         string ProductID, ProductName, SubProductName;
  158.         decimal pric = 0.0m;
  159.         string dataStr = htmStr;
  160.         dataStr = dataStr.Replace("{Date" + tit + "}", DateTime.Now.ToString());   //对缓存时间进行赋值
  161.         int leftStr = dataStr.IndexOf("<!--li" + tit + "b-->");
  162.         int rightStr = dataStr.IndexOf("<!--li" + tit + "e-->");
  163.         if (leftStr < 1 || rightStr >= dataStr.Length || dataStr.Length < 1 || rightStr < 1 || leftStr > rightStr)
  164.             return "数据有错";
  165.         int sumStr = rightStr - leftStr;
  166.         if (sumStr >= dataStr.Length || sumStr < 1)
  167.             return "数据有错";
  168.         string listStr = dataStr.Substring(leftStr, sumStr);    //获取要替换的列表
  169.         if(listStr.Length<12)
  170.             return "数据有错";
  171.         listStr = listStr.Substring(11);
  172.         StringBuilder sbDiv = new StringBuilder(5000);
  173.         foreach (DataRow Dr in Ds.Tables[0].Rows)
  174.         {
  175.             ProductID = Dr["ProductID"].ToString();
  176.             ProductName = Dr["ProductName"].ToString();
  177.             SubProductName = ProductName;
  178.             if (ProductName.Length > strLen)
  179.              SubProductName = ProductName.Substring(0, strLen);
  180.             pric = Convert.ToDecimal(Dr["Prices"]);
  181.             string newList = listStr;
  182.             newList = newList.Replace("{ProductID" + tit + "}", ProductID);
  183.             newList = newList.Replace("{ProductName" + tit + "}", ProductName);
  184.             newList = newList.Replace("{SubProductName" + tit + "}", SubProductName);
  185.             newList = newList.Replace("{pric" + tit + "}", pric.ToString("C"));
  186.             sbDiv.Append(newList);
  187.             sbDiv.Append("/n");
  188.         }
  189.         dataStr = dataStr.Replace(listStr, sbDiv.ToString());
  190.         return dataStr;
  191.     }
  192. }

被调用的html:

  1. <link type="text/css" rel="stylesheet" href="IndexStyle.css" />
  2. <div class="r0"><h2><span class="title">热点3C排行(本日)</span></h2>
  3.  <div class="dian pro2"  >
  4.  <!--缓存时间:{Date1}-->
  5.  <ul class="paihang">
  6.  <!--li1b--><li><span class="span_title">
  7.  <a href="/Shop/Auction/Auction.aspx?ProductID={ProductID1}" target="_blank"  title="{ProductName1}">{SubProductName1}</a>
  8.  </span>
  9.  <span class="span_ProductPrice">{pric1}</span>
  10.  </li><!--li1e-->
  11.  </ul></div>
  12.  </div>
  13.  <div class="r1"><h2><span class="title">热点3C排行(本周)</span></h2>
  14.  <div class="dian pro2"  >
  15.   <!--缓存时间:{Date2}-->
  16.   <ul class="paihang">
  17.   <!--li2b--><li><span class="span_title"> <a href="/Shop/Auction/Auction.aspx?ProductID={ProductID2}" target="_blank"  title="{ProductName2}">{SubProductName2}</a></span>
  18.   <span class="span_ProductPrice">{pric2}</span></li><!--li2e-->
  19.   </ul>
  20.   </div></div>
  21.    <div class="r1"><h2><span class="title">热点3C排行(本月)</span></h2>
  22.  <div class="dian pro2"  >
  23.   <!--缓存时间:{Date3}-->
  24.   <ul class="paihang">
  25.   <!--li3b--><li><span class="span_title"> <a href="/Shop/Auction/Auction.aspx?ProductID={ProductID3}" target="_blank"  title="{ProductName3}">{SubProductName3}</a></span>
  26.   <span class="span_ProductPrice">{pric3}</span></li><!--li3e-->
  27.   </ul></div></div>

调用代码:

  1.         string readUrl = Server.MapPath("../Label/LabelOutPut/网站访问排行.htm");
  2.         string url = Server.MapPath("../top/index_Ranking.htm");
  3.         RankingSet Rks = new RankingSet(readUrl, url);
  4.         string strReturn = Rks.IntoHtm(); 
  5.         Response.Write( Rks.RankingSetStr());   //获取要写入网页的字符串
  6.        Response.Write("<script>alert('"+strReturn+"')</script");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值