1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5using System.Web;
6/**//// <summary>
7/// ====================
8/// 作者:skyblue 带模板的HTML生成类 http://skyblue.cnblogs.com
9/// 日期:2008-09-17
10/// 版本:1.0.0
11/// ====================
12/// </summary>
13namespace SkyBlue_HtmlMade
14{
15 /**//// <summary>
16 /// HTML生成类
17 /// </summary>
18 public class MadeHtml
19 {
20 Members 成员#region Members 成员
21
22 /**//// <summary>
23 /// 生成的文件名称
24 /// </summary>
25 private string m_HtmlFileName = string.Empty;
26
27 /**//// <summary>
28 /// 生成的文件名称
29 /// </summary>
30 public string HtmlFileName
31 {
32 get { return m_HtmlFileName; }
33 set { m_HtmlFileName = value; }
34 }
35
36 /**//// <summary>
37 /// 生成编码
38 /// </summary>
39 private Encoding m_code = Encoding.GetEncoding("utf-8");
40
41 /**//// <summary>
42 /// 生成编码
43 /// </summary>
44 public Encoding Code
45 {
46 get { return m_code; }
47 set { m_code = value; }
48 }
49
50 /**//// <summary>
51 /// html 模版全路径
52 /// </summary>
53 private string m_TemplateFilePath = string.Empty;
54
55 /**//// <summary>
56 /// html 模版全路径
57 /// </summary>
58 public string TemplateFilePath
59 {
60 get { return m_TemplateFilePath; }
61 set { m_TemplateFilePath = value; }
62 }
63
64 /**//// <summary>
65 /// 参数值数组
66 /// </summary>
67 private string[] m_values;
68
69 /**//// <summary>
70 /// 替换参数数组
71 /// </summary>
72 private string[] m_params;
73
74 /**//// <summary>
75 /// 生成文件文件夹
76 /// </summary>
77 private string m_HtmlFilePath = string.Empty;
78
79 /**//// <summary>
80 /// 生成Html文件文件夹路径
81 /// </summary>
82 public string HtmlFilePath
83 {
84 get { return m_HtmlFilePath; }
85 set { m_HtmlFilePath = value; }
86 }
87 #endregion
88
89 Contructors 构造#region Contructors 构造
90 public MadeHtml()
91 {
92
93 }
94 /**//// <summary>
95 /// templateFilePath html模版全路径
96 /// htmlFilePath 生成Html文件文件夹路径
97 /// htmlFileName 生成的文件名称
98 /// code 生成文件编码
99 /// </summary>
100 /// <param name="templateFilePath"></param>
101 /// <param name="htmlFilePath"></param>
102 /// <param name="htmlFileName"></param>
103 /// <param name="code"></param>
104 public MadeHtml(string templateFilePath,string htmlFilePath, string htmlFileName, Encoding code)
105 {
106 this.m_TemplateFilePath = templateFilePath;
107 this.m_HtmlFilePath = htmlFilePath;
108 this.m_HtmlFileName = htmlFileName;
109 this.m_code = code;
110 }
111 /**//// <summary>
112 /// templateFilePath html模版全路径
113 /// htmlFilePath 生成Html文件文件夹路径
114 /// htmlFileName 生成的文件名称
115 /// </summary>
116 /// <param name="templateFilePath"></param>
117 /// <param name="htmlFilePath"></param>
118 /// <param name="htmlFileName"></param>
119 public MadeHtml(string templateFilePath, string htmlFilePath, string htmlFileName)
120 {
121 this.m_TemplateFilePath = templateFilePath;
122 this.m_HtmlFilePath = htmlFilePath;
123 this.m_HtmlFileName = htmlFileName;
124 }
125 #endregion
126
127 Methods 方法#region Methods 方法
128
129 /**//// <summary>
130 /// html生成
131 /// </summary>
132 /// <returns></returns>
133 public string HtmlWriting(string[] param, string[] value)
134 {
135
136 if (param.Length > 0 && value.Length > 0 && param.Length == value.Length)
137 {
138 this.m_params = param;
139 this.m_values = value;
140
141 return this.HtmlFileWirte();
142 }
143 else
144 {
145 return "";
146 }
147 }
148
149 /**//// <summary>
150 /// 生成Html
151 /// </summary>
152 /// <returns></returns>
153 private string HtmlFileWirte()
154 {
155
156 // 读取模板文件
157 string tempHtml = HttpContext.Current.Server.MapPath(this.TemplateFilePath);
158 StreamReader srd = null;
159 string str = "";
160 try
161 {
162 srd = new StreamReader(tempHtml, this.m_code);
163 str = srd.ReadToEnd(); // 读取文件
164 }
165 catch (Exception ex)
166 {
167 throw ex;
168
169 }
170 finally
171 {
172 srd.Close();
173 }
174 // 替换内容
175 // 这时,模板文件已经读入到名称为str的变量中了
176 for (int i = 0; i < this.m_params.Length; i++)
177 {
178 str = str.Replace(this.m_params[i], this.m_values[i]);
179 }
180
181 return SaveHtmlFile(str);
182 }
183
184 /**//// <summary>
185 /// 保存文件
186 /// 返回生成的文件名称
187 /// </summary>
188 /// <param name="str">string</param>
189 /// <returns>string</returns>
190 private string SaveHtmlFile(string str)
191 {
192 if (str.Length > 0)
193 {
194 // 写文件
195 StreamWriter sw = null;
196 try
197 {
198 //独要
199 string HtmlPath = HttpContext.Current.Server.MapPath(this.m_HtmlFilePath);
200
201 //获得文要生成Html文件名称
202 string HtmlFileName = GetHtmlFileName();
203
204 //判断文件是否在,存在删除
205 if (File.Exists(HtmlPath + "\\" + HtmlFileName))
206 {
207 File.Delete(HtmlPath + "\\" + HtmlFileName);
208 }
209
210 //写文件
211 sw = new StreamWriter(HtmlPath + "\\" + HtmlFileName, false, this.Code);
212 sw.Write(str);
213 sw.Flush();
214
215
216 return HtmlFileName;
217 }
218 catch (Exception ex)
219 {
220 throw ex;
221 }
222 finally
223 {
224 sw.Close();
225 }
226 return "";
227 }
228 else
229 {
230 return "";
231 }
232 }
233
234 /**//// <summary>
235 /// 获得文件
236 /// </summary>
237 /// <returns></returns>
238 private string GetHtmlFileName()
239 {
240 if (m_HtmlFileName.Length == 0)
241 {
242 return DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
243 }
244 else
245 {
246 return m_HtmlFileName;
247 }
248 }
249 #endregion
250 }
251}
252
模版
<!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>
<title>生成试测</title>
<style type="text/css">
body
{
margin:0;
padding:0;
}
.ContentDiv
{
float:left;
border-top:solid 1px #000000;text-align:center;
border-right:solid 1px #000000; width:98px;
}
</style>
</head>
<body>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="400">
<tr>
<td style="border-left:solid 1px #000000; "><div class="ContentDiv">一</div><div class="ContentDiv">一</div><div class="ContentDiv">一</div><div class="ContentDiv">一</div><div class="ContentDiv">一</div><div class="ContentDiv">一</div>
<div class="ContentDiv">一</div>
<div class="ContentDiv">一</div>
<div class="ContentDiv">一</div>
<div class="ContentDiv">一</div>
<div class="ContentDiv">一</div>
<div class="ContentDiv">一</div>
<div class="ContentDiv">一</div>
<div class="ContentDiv">一</div>
<div style=" background-color:#000000;margin:0; padding:0; height:1px; width:99%"></div>
</td>
</tr>
</table>
</body>
</html>
测式调用
//<div class="ContentDiv">一</div>
SkyBlue_HtmlMade.MadeHtml dal = new MadeHtml("Html/Template.htm", "html", "test.htm");
string[] param ={ "$HtmlStr$" };
string[] value ={ "<div class=""ContentDiv"">一</div><div class=""ContentDiv"">一</div><div class=""ContentDiv"">一</div><div class=""ContentDiv"">一</div><div class=""ContentDiv"">一</div><div class=""ContentDiv"">一</div>" };
string flag= dal.HtmlWriting(param, value);
Response.Write(flag);