上班无聊时写的,可能还有很多方面考虑到,希望各位园友多多指教!
1
using System;
2
using System.Data;
3
using System.Text;
4
using System.Configuration;
5
using System.IO;
6
using System.Web;
7
using System.Web.Security;
8
using System.Web.UI;
9
using System.Web.UI.WebControls;
10
using System.Web.UI.WebControls.WebParts;
11
using System.Web.UI.HtmlControls;
12
13
/**//// <summary>
14
/// Class1 的摘要说明
15
/// </summary>
16
public class Tohtml
17

{
18
private string FileName;
19
public string SavePath;
20
public string TemplateFilePath;
21
public string ExecSite;
22
23
public Tohtml()
24
{
25
//
26
// TODO: 在此处添加构造函数逻辑
27
//
28
}
29
30
public Tohtml(string SavePath,string TemplateFilePath)
31
{
32
this.SavePath = SavePath;
33
this.TemplateFilePath = TemplateFilePath;
34
}
35
36
文件名类型#region 文件名类型
37
public int FileNameType
38
{
39
get
40
{
41
FileNameType = 0;
42
return FileNameType;
43
}
44
set
45
{
46
//文件存储类型为1时,文件名为日期
47
if (value == 1)
48
{
49
this.FileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".html";
50
}
51
//文件存储类型为2时,文件名为时间
52
else if (value == 2)
53
{
54
this.FileName = DateTime.Now.ToString("hhmmss") + ".html";
55
}
56
//文件存储类型为3时,文件名为随机数
57
else if (value == 3)
58
{
59
Random Rand = new Random((int)DateTime.Now.Ticks);
60
this.FileName = Rand.Next().ToString() + ".html";
61
}
62
else
63
{
64
this.FileName = "";
65
}
66
}
67
}
68
#endregion
69
70
利用模板创建静态页面#region 利用模板创建静态页面
71
/**//// <summary>
72
/// 利用模板创建静态页面
73
/// </summary>
74
/// <param name="Arg">被替换变量</param>
75
/// <param name="ArgValue">替换值</param>
76
/// <returns></returns>
77
public bool CreateHtmlByTemplate(string []Arg , string []ArgValue)
78
{
79
//判断参数列表是否一致
80
if (Arg.Length != ArgValue.Length)
81
{
82
HttpContext.Current.Response.Write("参数列表不一致");
83
return false;
84
}
85
else
86
{
87
88
string dir = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + this.SavePath;
89
if (!System.IO.Directory.Exists(dir))
90
{
91
System.IO.Directory.CreateDirectory(dir);
92
}
93
StringBuilder strHtml = new StringBuilder();
94
95
StreamReader sr = new StreamReader(TemplateFilePath,System.Text.Encoding.GetEncoding("gb2312"));
96
string oneline;
97
98
while ((oneline = sr.ReadLine()) != null)
99
{
100
strHtml.Append(oneline);
101
}
102
sr.Close();
103
for (int i = 0; i < Arg.Length; i++)
104
{
105
strHtml.Replace(Arg[i], ArgValue[i]);
106
}
107
try
108
{
109
//写文件
110
FileInfo fileInfo = new FileInfo(dir + "\\" + this.FileName);
111
FileStream fs = fileInfo.OpenWrite();
112
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
113
//把新的内容写到创建的HTML页面中
114
sw.WriteLine(strHtml);
115
sw.Flush();
116
sw.Close();
117
}
118
catch (Exception ex)
119
{
120
HttpContext.Current.Response.Write("参数列表不一致");
121
}
122
return true;
123
}
124
}
125
#endregion
126
127
执行某个aspx页面后再产生静态页面#region 执行某个aspx页面后再产生静态页面
128
/**//// <summary>
129
/// 执行某个aspx页面后再产生静态页面
130
/// </summary>
131
/// <returns></returns>
132
public bool CreateHtmlByExec()
133
{
134
//调用Main_Execute,并且获取其输出
135
StringWriter sw = new StringWriter();
136
HttpContext.Current.Server.Execute(ExecSite, sw);
137
138
string content = sw.ToString();
139
string filename = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + this.SavePath + "\\" + FileName;
140
//写进文件
141
try
142
{
143
using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write))
144
{
145
using (StreamWriter streamwriter = new StreamWriter(fs, HttpContext.Current.Response.ContentEncoding))
146
{
147
streamwriter.Write(content);
148
}
149
}
150
return true;
151
}
152
catch (Exception ex)
153
{
154
HttpContext.Current.Response.Write(ex.Message);
155
return false;
156
}
157
158
}
159
#endregion
160
161
}
162