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