当前文件路径:AcomStore/Components/toHtml.cs 
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Net;
11
using System.Text;
12
using System.IO;
13
14
using AcomLb.Enumerations;
15
16
namespace AcomLb.Components
17
...{
18
/**//// <summary>
19
/// toHtml 的摘要说明
20
/// </summary>
21
public class toHtml
22
...{
23
public toHtml()
24
...{
25
//
26
// TODO: 在此处添加构造函数逻辑
27
//
28
}
29
30
读取模板#region 读取模板
31
/**//// <summary>
32
/// 读取模板
33
/// </summary>
34
/// <param name="templateUrl">模板地址</param>
35
/// <param name="coding">编码.如:(utf-8)</param>
36
/// <returns>模板内容</returns>
37
public static string readTemplate(string templateUrl, EncodeKind encode)
38
...{
39
string tlPath = System.Web.HttpContext.Current.Server.MapPath(templateUrl);
40
string coding = GetCode(encode);
41
Encoding code = Encoding.GetEncoding(coding);
42
StreamReader sr = null;
43
string str = null;
44
//读取模板内容
45
try
46
...{
47
sr = new StreamReader(tlPath, code);
48
str = sr.ReadToEnd();
49
}
50
catch (Exception ex)
51
...{
52
throw ex;
53
}
54
finally
55
...{
56
sr.Close();
57
}
58
return str;
59
}
60
#endregion
61
62
生成页面#region 生成页面
63
/**//// <summary>
64
/// 生成页面
65
/// </summary>
66
/// <param name="str">文件内容</param>
67
/// <param name="htmlFile">文件存放地址</param>
68
/// <param name="fileName">文件名</param>
69
/// <param name="coding">编码.如:(utf-8)</param>
70
/// <returns>文件名</returns>
71
public static string createHtml(string str, string htmlFile, string fileName, EncodeKind encode)
72
...{
73
string coding = GetCode(encode);
74
string HtmlDirectory = System.Web.HttpContext.Current.Server.MapPath(htmlFile);
75
if (!Directory.Exists(HtmlDirectory))
76
Directory.CreateDirectory(HtmlDirectory);
77
78
Encoding code = Encoding.GetEncoding(coding);
79
StreamWriter sw = null;
80
//写入生成
81
try
82
...{
83
sw = new StreamWriter(HtmlDirectory+ fileName, false, code);
84
sw.Write(str);
85
sw.Flush();
86
}
87
catch (Exception ex)
88
...{
89
throw ex;
90
}
91
finally
92
...{
93
sw.Close();
94
}
95
return fileName;
96
}
97
#endregion
98
99
Url生成页面#region Url生成页面
100
/**//// <summary>
101
/// url生成页面
102
/// </summary>
103
/// <param name="url">url地址</param>
104
/// <param name="coding">编码.如:(utf-8)</param>
105
/// <param name="htmlFile">文件存放地址</param>
106
/// <param name="fileName">文件名</param>
107
/// <returns></returns>
108
public static string urlToHtml(string url, EncodeKind encode, string htmlFile, string fileName)
109
...{
110
string coding = GetCode(encode);
111
Encoding code = Encoding.GetEncoding(coding);
112
StreamReader sr = null;
113
StreamWriter sw = null;
114
string str = null;
115
116
//读取远程地址
117
WebRequest temp = WebRequest.Create(url);
118
WebResponse myTemp = temp.GetResponse();
119
sr = new StreamReader(myTemp.GetResponseStream(), code);
120
121
//读取页面内容
122
try
123
...{
124
sr = new StreamReader(myTemp.GetResponseStream(), code);
125
str = sr.ReadToEnd();
126
}
127
catch (Exception ex)
128
...{
129
throw ex;
130
}
131
finally
132
...{
133
sr.Close();
134
}
135
136
//写入生成
137
try
138
...{
139
sw = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath(htmlFile) + fileName, false, code);
140
sw.Write(str);
141
sw.Flush();
142
}
143
catch (Exception ex)
144
...{
145
throw ex;
146
}
147
finally
148
...{
149
sw.Close();
150
}
151
return fileName;
152
}
153
#endregion
154
155
private static string GetCode(EncodeKind code)
156
...{
157
switch (code)
158
...{
159
case EncodeKind.GB2312:
160
return "gb2312";
161
case EncodeKind.Utf8:
162
return "utf-8";
163
default:
164
return "utf-8";
165
}
166
}
167
}
168
}