模板HTML
模板
<!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> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title></title> <link href="http://www.cnblogs.com/2011_08_24/lucklist/template.css" rel="stylesheet" type="text/css" /> </head> <body class="template_body" style="margin: 0 auto 0 auto;background-image: url(../images/bg_03.jpg);width:406px;"> <table border="0" cellpadding="0" cellspacing="0" style="text-align: left;"> <!-- <tr style="background-image: URL(../images/bg_02.jpg);">--> <tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr> <tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr> <tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr> <tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr> <tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr> <tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr> <tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr> <tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr> <tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr> <tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr> <tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr><tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr> <tr style="background-image: URL(../images/bg_04.jpg);height: 24px"> <td class="tdName"> #Name </td> <td class="tdTime"> #Time </td> <td class="tdLuckName"> #LuckName </td> </tr>
</table> <div class="Page">#Page</div> </body> </html>
首先设置类的全局变量,设置文件路径和编码
public Encoding code = Encoding.GetEncoding("gb2312");//定义文字编码 UTF-8 public string path;//定义html文件存放路径 // 读取模板文件 public string temp;
///<summary> /// ///</summary> ///<param name="FilePath">"./lucklist/"</param> ///<param name="TemplatePath">"./lucklist/Main.html"</param> public CreateHtml(string FilePath, string TemplatePath) { path = HttpContext.Current.Server.MapPath(FilePath);//定义html文件存放路径 temp = HttpContext.Current.Server.MapPath(TemplatePath);//定义模板路径 }
然后就是类的主要代码,再使用的时候就调用这个类
///<summary> /// 创建HTML网页 ///</summary> ///<param name="ds">dataset</param> ///<param name="PageSize">一页的记录数量</param> ///<param name="FileName">文件名,如0824-</param> ///<param name="FileNameExt">如rd</param> ///<param name="FileType">html</param> ///<param name="ReplaceData">string[] 类型的文件存放"XX,XX"</param> ///<param name="IndexCount">分页导航栏的数字长度1,2,3,4,5</param> ///<param name="anchor">锚点#xxxx</param> ///<param name="ReplaceString">需要替换的其他字符串,注意:全部替换</param> ///<returns></returns> public bool CreateHtmlFile(DataSet ds, int PageSize, string FileName, string FileNameExt, string FileType, string[] ReplaceData, int IndexCount, string anchor, string[] ReplaceString) { string str = ReadHtmlFile(temp); string tempStr = str; //备份改写前的模板HTML文件
int count = ds.Tables[0].Rows.Count; //数据条数 int PageCount = count / PageSize + (count % PageSize == 0 ? 0 : 1); //页面数量
//string FileName = "0824"; //0824+1+rd.html //string FileNameExt = "rd"; //string FileType = "html"
//循环页面的数量 for (int SelectPage = 1; SelectPage <= PageCount; SelectPage++) { //整合出来的文件名 string htmlfilename = path + FileName + SelectPage.ToString() + FileNameExt + "." + FileType;
//从Dataset中读取指定数量的条目替换HTML文件 for (int i = (SelectPage - 1) * PageSize; i < SelectPage * PageSize; i++) { if (i < count) //判断不会超过索引 { //获得参数 foreach (string RepS in ReplaceData) { string[] data = RepS.Split(','); //data[0]为#Name //data[1]为数据库对应字段,如UserName str = new Regex(data[0]).Replace(str, ds.Tables[0].Rows[i][data[1]].ToString(), 1); } //Regex Regex1 = new Regex("#Name"); //str = Regex1.Replace(str, ds.Tables[0].Rows[i]["UserName"].ToString(), 1); //Regex Regex2 = new Regex("#Time"); //str = Regex2.Replace(str, ds.Tables[0].Rows[i]["Time"].ToString().Substring(11, 5), 1); //Regex Regex3 = new Regex("#LuckName"); //str = Regex3.Replace(str, ds.Tables[0].Rows[i]["Score"].ToString(), 1); } else //超出索引的全部删掉 foreach (string RepS in ReplaceData) { string[] data = RepS.Split(','); //data[0]为#Name //data[1]为数据库对应字段,如UserName str = new Regex(data[0]).Replace(str, "", 1); } }
foreach (string RepString in ReplaceString) { string[] data = RepString.Split(','); //data[0]为#Select3 //data[1]为替换字段 str = str.Replace(data[0], data[1]); }
Regex Regex4 = new Regex("#Page"); str = Regex4.Replace(str, GetNavigate(count, PageSize, IndexCount, SelectPage, FileType + anchor, FileName, FileNameExt));
WriteHtmlFile(str, htmlfilename); str = tempStr; } return true; }
需要用到的子方法
读取HTML
///<summary> /// 读取HTML文件 ///</summary> ///<param name="temp">HttpContext.Current.Server.MapPath("./lucklist/Main.html");</param> ///<returns></returns> public string ReadHtmlFile(string temp) { StreamReader sr = 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(); } finally { sr.Dispose(); sr.Close();
} return str; }
写入HTML
///<summary> /// 写入HTML文件 ///</summary> ///<param name="str">HTML代码</param> ///<param name="htmlfilename">完整带路径的文件名</param> ///<returns></returns> public bool WriteHtmlFile(string str, string htmlfilename) { StreamWriter sw = null; // 写文件 try { sw = new StreamWriter(htmlfilename, false, code); sw.Write(str); sw.Flush(); } catch (Exception ex) { HttpContext.Current.Response.Write(ex.Message); HttpContext.Current.Response.End(); } finally { sw.Dispose(); sw.Close(); } return true; }
生成导航栏
///<summary> /// 生成分页导航条 ///</summary> ///<param name="totalRecord">总记录数</param> ///<param name="pageSize">页面记录数</param> ///<param name="DistLen">数字长度</param> ///<param name="page">当前页数</param>
|
请发表评论