模板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>
///<param name="QueryParametet">URL参数</param>
///<param name="scriptName">页名前缀</param>
///<returns></returns>
public static string GetNavigate(int totalRecord, int pageSize, int DistLen, int page, string QueryParameter, string scriptName, string extName)
{
int pageNum; //页面数量
string HTML = "";
if ((totalRecord % pageSize) != 0)
pageNum = (totalRecord / pageSize) + 1;
else
pageNum = totalRecord / pageSize;
if (pageNum < 1)
return HTML;
//统计信息
HTML += "显示" + ((page - 1) * pageSize + 1) + "━";
if (page >= pageNum)
HTML += totalRecord + "条";
else
HTML += page * pageSize + "条";
HTML += "共<B>" + totalRecord + "</B>条 " + pageSize + "条/页";
//首页,上一页
if (page > 1)
{
HTML += "<a href=\"" + scriptName + "1" + extName + "." + QueryParameter + "\" > 首 页 </a>";
HTML += "<a href=\"" + scriptName + (page - 1) + extName + "." + QueryParameter + "\" > 上一页 </a>";
}
//数字序列
int page_t;
if (page < DistLen * 2)
{
if (pageNum > DistLen * 2)
page_t = DistLen * 2;
else
page_t = pageNum;
for (int i = 1; i <= page_t; i++)
{
if (i == page)
HTML += "<B> " + page + "</B>";
else
HTML += "<a href=\"" + scriptName + i + extName + "." + QueryParameter + "\"> " + i + "</a>";
}
}
else
{
if (pageNum > (page + DistLen))
page_t = page + DistLen;
else
page_t = pageNum;
for (int i = (page - DistLen); i <= page_t; i++)
{
if (i == page)
HTML += "<B> " + page + "</B>";
else
HTML += "<a href=\"" + scriptName + i + extName + "." + QueryParameter + "\" > " + i + "</a>";
}
}
//下一页,尾页
if (page < pageNum)
{
HTML += "<a href=\"" + scriptName + (page + 1) + extName + "." + QueryParameter + "\" > 下一页 </a>";
HTML += "<a href=\"" + scriptName + pageNum + extName + "." + QueryParameter + "\" > 尾页 </a>";
}
return HTML;
}
然后再在调用类生成HTML
//读取数据库,放入ds
DataSet ds = new DataSet();
LuckSql lsql = new LuckSql();
ds = lsql.temp1();
//实例化设置路径
CreateHtml ch = new CreateHtml("./", "./Main.html");
//设置模板中需要替换的字段,#Name是模板中的字段,Name是ds中的字段
string[] ReplaceData = new string[3];
ReplaceData[0] = "#Name,Name";
ReplaceData[1] = "#Time,Time";
ReplaceData[2] = "#LuckName,luckname";
//替换非数据库中的字段 ,模板中的任何位置都可以,这个部分模板里忘记添加了,可以略过
string[] ReplaceString = new string[1];
ReplaceString[0] = "#Select3Index,替换字段";
//生成,锚点不需要的话就填""
ch.CreateHtmlFile(ds, 32, "0824-", "-rd", "html", ReplaceData, 6, "#mid", ReplaceString);
注意的是:
模板最好和css样式和调用类的页面放在同一个文件,模板的样式放在css里面最好,不然生成十多页改起来麻烦.
编码默认是gb2312,当然随便你.中文还是gb2312比较好吧.
模板中数据的条目一定要和"32"的位置对应 ch.CreateHtmlFile(ds, 32, "0824-", "-rd", "html", ReplaceData, 6, "#mid", ReplaceString);
不然数据条目对不上号的.