...
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net; using System.Text.RegularExpressions; namespace Zxq.Common { //鄭希強 //www.cnblogs.com/zhengxiqiang //生成靜態頁操作 //2009.2 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 } }