生成静态页核心代码
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
namespace Zxq.Common
{
//生成静态页操作
public class ToHtml
{
#region 读取模板
/// <summary>
/// 读取模板
/// </summary>
/// <param name="templateUrl">模板地址</param>
/// <param name="coding">编码</param>
/// <returns>模板内容</returns>
public string ReadTemplate(string templateUrl, Encoding code)
{
string tlPath = System.Web.HttpContext.Current.Server.MapPath(templateUrl);
StreamReader sr = null;
string str = null;
//读取模板内容
try
{
sr = new StreamReader(tlPath, code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
return str;
}
#endregion
#region 生成文件
/// <summary>
/// 生成文件
/// </summary>
/// <param name="str">文件内容</param>
/// <param name="htmlFile">文件存放地址</param>
/// <param name="fileName">文件名</param>
/// <param name="coding">编码</param>
/// <returns>文件名</returns>
public bool CreateHtml(string str, string htmlFile, string fileName, Encoding code)
{
StreamWriter sw = null;
bool a = false;
//写入生成
try
{
htmlFile = System.Web.HttpContext.Current.Server.MapPath(htmlFile);
this.FolderCreate(htmlFile);
sw = new StreamWriter(htmlFile + fileName, false, code);
sw.Write(str);
sw.Flush();
a = FileExists(htmlFile + fileName);
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
}
return a;
}
#endregion
#region 判断文件是否存在
/// <summary>
/// 判断文件是否存在
/// </summary>
/// <param name="FilePath">文件路径</param>
/// <returns></returns>
public bool FileExists(string FilePath)
{
if (System.IO.File.Exists(FilePath))
return true;
else
return false;
}
#endregion
#region 创建目录
/// <summary>
/// 创建目录
/// </summary>
/// <param name="Path"></param>
/// <returns></returns>
public bool FolderCreate(string Path)
{
// 判断目标目录是否存在如果不存在则新建之
if (!FolderExists(Path))
Directory.CreateDirectory(Path);
return FolderExists(Path);
}
#endregion
#region 判断目录是否存在
/// <summary>
/// 判断目录是否存在
/// </summary>
/// <param name="Path">路径</param>
/// <returns></returns>
public bool FolderExists(string Path)
{
if (Directory.Exists(Path))
return true;
else
return false;
}
#endregion
#region 静态列表页分页
/// <summary>
/// 静态列表页分页
/// </summary>
/// <param name="pageCount">总页数</param>
/// <param name="currentPage">当前页</param>
/// <param name="prefix">如:list</param>
/// <param name="suffix">如:.shtml</param>
/// <returns></returns>
public string GetHtmlPager(int pageCount, int currentPage, string prefix, string suffix)
{
int stepNum = 4;
int pageRoot = 1;
pageCount = pageCount == 0 ? 1 : pageCount;
currentPage = currentPage == 0 ? 1 : currentPage;
StringBuilder sb = new StringBuilder();
sb.Append("<ul>");
sb.Append("<li class=pagerTitle> 分页 " + currentPage.ToString() + "/" + pageCount.ToString() + " </li>\r");
if (currentPage - stepNum < 2)
pageRoot = 1;
else
pageRoot = currentPage - stepNum;
int pageFoot = pageCount;
if (currentPage + stepNum >= pageCount)
pageFoot = pageCount;
else
pageFoot = currentPage + stepNum;
if (pageRoot == 1)
{
if (currentPage > 1)
{
sb.Append("<li> <a href='" + prefix + "1" + suffix + "' title='首页'>首页</a> </li>\r");
sb.Append("<li> <a href='" + prefix + Convert.ToString(currentPage - 1) + suffix + "' title='上页'>上页</a> </li>\r");
}
}
else
{
sb.Append("<li> <a href='" + prefix + "1" + suffix + "' title='首页'>首页</a> </li>");
sb.Append("<li> <a href='" + prefix + Convert.ToString(currentPage - 1) + suffix + "' title='上页'>上页</a> </li>\r");
}
for (int i = pageRoot; i <= pageFoot; i++)
{
if (i == currentPage)
{
sb.Append("<li class='current'> " + i.ToString() + " </li>\r");
}
else
{
sb.Append("<li> <a href='" + prefix + i.ToString() + suffix + "' title='第" + i.ToString() + "页'>" + i.ToString() + "</a> </li>\r");
}
if (i == pageCount)
break;
}
if (pageFoot == pageCount)
{
if (pageCount > currentPage)
{
sb.Append("<li> <a href='" + prefix + Convert.ToString(currentPage + 1) + suffix + "' title='下页'>下页</a> </li>\r");
sb.Append("<li> <a href='" + prefix + pageCount.ToString() + suffix + "' title='尾页'>尾页</a> </li>\r");
}
}
else
{
sb.Append("<li> <a href='" + prefix + Convert.ToString(currentPage + 1) + suffix + "' title='下页'>下页</a> </li>\r");
sb.Append("<li> <a href='" + prefix + pageCount.ToString() + suffix + "' title='尾页'>尾页</a> </li>\r");
}
sb.Append("</ul>");
return sb.ToString();
}
#endregion
#region 压缩Html文件
/// <summary>
/// 压缩Html文件
/// </summary>
/// <param name="html">Html文件</param>
/// <returns></returns>
public string ZipHtml(string Html)
{
Html = Regex.Replace(Html, @">\s+?<", "><");//去除Html中的空白字符.
Html = Regex.Replace(Html, @"\r\n\s*", "");
Html = Regex.Replace(Html, @"<body([\s|\S]*?)>([\s|\S]*?)</body>", @"<body$1>$2</body>", RegexOptions.IgnoreCase);
return Html;
}
#endregion
#region 从HTML中获取文本,保留br,p,img
/// <summary>
/// 从HTML中获取文本,保留br,p,img
/// </summary>
/// <param name="Html">文本内容</param>
/// <returns></returns>
public string GetTextFromHtml(string Html)
{
Regex regEx = new Regex(@"</?(?!br|/?p|img)[^>]*>", RegexOptions.IgnoreCase);
return regEx.Replace(Html, "");
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
namespace Zxq.Common
{
//生成静态页操作
public class ToHtml
{
#region 读取模板
/// <summary>
/// 读取模板
/// </summary>
/// <param name="templateUrl">模板地址</param>
/// <param name="coding">编码</param>
/// <returns>模板内容</returns>
public string ReadTemplate(string templateUrl, Encoding code)
{
string tlPath = System.Web.HttpContext.Current.Server.MapPath(templateUrl);
StreamReader sr = null;
string str = null;
//读取模板内容
try
{
sr = new StreamReader(tlPath, code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
return str;
}
#endregion
#region 生成文件
/// <summary>
/// 生成文件
/// </summary>
/// <param name="str">文件内容</param>
/// <param name="htmlFile">文件存放地址</param>
/// <param name="fileName">文件名</param>
/// <param name="coding">编码</param>
/// <returns>文件名</returns>
public bool CreateHtml(string str, string htmlFile, string fileName, Encoding code)
{
StreamWriter sw = null;
bool a = false;
//写入生成
try
{
htmlFile = System.Web.HttpContext.Current.Server.MapPath(htmlFile);
this.FolderCreate(htmlFile);
sw = new StreamWriter(htmlFile + fileName, false, code);
sw.Write(str);
sw.Flush();
a = FileExists(htmlFile + fileName);
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
}
return a;
}
#endregion
#region 判断文件是否存在
/// <summary>
/// 判断文件是否存在
/// </summary>
/// <param name="FilePath">文件路径</param>
/// <returns></returns>
public bool FileExists(string FilePath)
{
if (System.IO.File.Exists(FilePath))
return true;
else
return false;
}
#endregion
#region 创建目录
/// <summary>
/// 创建目录
/// </summary>
/// <param name="Path"></param>
/// <returns></returns>
public bool FolderCreate(string Path)
{
// 判断目标目录是否存在如果不存在则新建之
if (!FolderExists(Path))
Directory.CreateDirectory(Path);
return FolderExists(Path);
}
#endregion
#region 判断目录是否存在
/// <summary>
/// 判断目录是否存在
/// </summary>
/// <param name="Path">路径</param>
/// <returns></returns>
public bool FolderExists(string Path)
{
if (Directory.Exists(Path))
return true;
else
return false;
}
#endregion
#region 静态列表页分页
/// <summary>
/// 静态列表页分页
/// </summary>
/// <param name="pageCount">总页数</param>
/// <param name="currentPage">当前页</param>
/// <param name="prefix">如:list</param>
/// <param name="suffix">如:.shtml</param>
/// <returns></returns>
public string GetHtmlPager(int pageCount, int currentPage, string prefix, string suffix)
{
int stepNum = 4;
int pageRoot = 1;
pageCount = pageCount == 0 ? 1 : pageCount;
currentPage = currentPage == 0 ? 1 : currentPage;
StringBuilder sb = new StringBuilder();
sb.Append("<ul>");
sb.Append("<li class=pagerTitle> 分页 " + currentPage.ToString() + "/" + pageCount.ToString() + " </li>\r");
if (currentPage - stepNum < 2)
pageRoot = 1;
else
pageRoot = currentPage - stepNum;
int pageFoot = pageCount;
if (currentPage + stepNum >= pageCount)
pageFoot = pageCount;
else
pageFoot = currentPage + stepNum;
if (pageRoot == 1)
{
if (currentPage > 1)
{
sb.Append("<li> <a href='" + prefix + "1" + suffix + "' title='首页'>首页</a> </li>\r");
sb.Append("<li> <a href='" + prefix + Convert.ToString(currentPage - 1) + suffix + "' title='上页'>上页</a> </li>\r");
}
}
else
{
sb.Append("<li> <a href='" + prefix + "1" + suffix + "' title='首页'>首页</a> </li>");
sb.Append("<li> <a href='" + prefix + Convert.ToString(currentPage - 1) + suffix + "' title='上页'>上页</a> </li>\r");
}
for (int i = pageRoot; i <= pageFoot; i++)
{
if (i == currentPage)
{
sb.Append("<li class='current'> " + i.ToString() + " </li>\r");
}
else
{
sb.Append("<li> <a href='" + prefix + i.ToString() + suffix + "' title='第" + i.ToString() + "页'>" + i.ToString() + "</a> </li>\r");
}
if (i == pageCount)
break;
}
if (pageFoot == pageCount)
{
if (pageCount > currentPage)
{
sb.Append("<li> <a href='" + prefix + Convert.ToString(currentPage + 1) + suffix + "' title='下页'>下页</a> </li>\r");
sb.Append("<li> <a href='" + prefix + pageCount.ToString() + suffix + "' title='尾页'>尾页</a> </li>\r");
}
}
else
{
sb.Append("<li> <a href='" + prefix + Convert.ToString(currentPage + 1) + suffix + "' title='下页'>下页</a> </li>\r");
sb.Append("<li> <a href='" + prefix + pageCount.ToString() + suffix + "' title='尾页'>尾页</a> </li>\r");
}
sb.Append("</ul>");
return sb.ToString();
}
#endregion
#region 压缩Html文件
/// <summary>
/// 压缩Html文件
/// </summary>
/// <param name="html">Html文件</param>
/// <returns></returns>
public string ZipHtml(string Html)
{
Html = Regex.Replace(Html, @">\s+?<", "><");//去除Html中的空白字符.
Html = Regex.Replace(Html, @"\r\n\s*", "");
Html = Regex.Replace(Html, @"<body([\s|\S]*?)>([\s|\S]*?)</body>", @"<body$1>$2</body>", RegexOptions.IgnoreCase);
return Html;
}
#endregion
#region 从HTML中获取文本,保留br,p,img
/// <summary>
/// 从HTML中获取文本,保留br,p,img
/// </summary>
/// <param name="Html">文本内容</param>
/// <returns></returns>
public string GetTextFromHtml(string Html)
{
Regex regEx = new Regex(@"</?(?!br|/?p|img)[^>]*>", RegexOptions.IgnoreCase);
return regEx.Replace(Html, "");
}
#endregion
}
}