上班无聊时写的,可能还有很多方面考虑到,希望各位园友多多指教!
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
14
/// Class1 的摘要说明
15
/// </summary>
16
public class Tohtml
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
18
private string FileName;
19
public string SavePath;
20
public string TemplateFilePath;
21
public string ExecSite;
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
public Tohtml()
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
//
26
// TODO: 在此处添加构造函数逻辑
27
//
28
}
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
public Tohtml(string SavePath,string TemplateFilePath)
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
this.SavePath = SavePath;
33
this.TemplateFilePath = TemplateFilePath;
34
}
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
文件名类型#region 文件名类型
37
public int FileNameType
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
get
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41
FileNameType = 0;
42
return FileNameType;
43
}
44
set
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
//文件存储类型为1时,文件名为日期
47
if (value == 1)
48![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
49
this.FileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".html";
50
}
51
//文件存储类型为2时,文件名为时间
52
else if (value == 2)
53![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54
this.FileName = DateTime.Now.ToString("hhmmss") + ".html";
55
}
56
//文件存储类型为3时,文件名为随机数
57
else if (value == 3)
58![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
59
Random Rand = new Random((int)DateTime.Now.Ticks);
60
this.FileName = Rand.Next().ToString() + ".html";
61
}
62
else
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
this.FileName = "";
65
}
66
}
67
}
68
#endregion
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
利用模板创建静态页面#region 利用模板创建静态页面
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
79
//判断参数列表是否一致
80
if (Arg.Length != ArgValue.Length)
81![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
82
HttpContext.Current.Response.Write("参数列表不一致");
83
return false;
84
}
85
else
86![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
87![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
string dir = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + this.SavePath;
89
if (!System.IO.Directory.Exists(dir))
90![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
91
System.IO.Directory.CreateDirectory(dir);
92
}
93
StringBuilder strHtml = new StringBuilder();
94![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
StreamReader sr = new StreamReader(TemplateFilePath,System.Text.Encoding.GetEncoding("gb2312"));
96
string oneline;
97![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
while ((oneline = sr.ReadLine()) != null)
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
100
strHtml.Append(oneline);
101
}
102
sr.Close();
103
for (int i = 0; i < Arg.Length; i++)
104![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
105
strHtml.Replace(Arg[i], ArgValue[i]);
106
}
107
try
108![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
120
HttpContext.Current.Response.Write("参数列表不一致");
121
}
122
return true;
123
}
124
}
125
#endregion
126![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
127![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
执行某个aspx页面后再产生静态页面#region 执行某个aspx页面后再产生静态页面
128![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
129
/// 执行某个aspx页面后再产生静态页面
130
/// </summary>
131
/// <returns></returns>
132
public bool CreateHtmlByExec()
133![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
134
//调用Main_Execute,并且获取其输出
135
StringWriter sw = new StringWriter();
136
HttpContext.Current.Server.Execute(ExecSite, sw);
137![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
138
string content = sw.ToString();
139
string filename = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + this.SavePath + "\\" + FileName;
140
//写进文件
141
try
142![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
143
using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write))
144![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
145
using (StreamWriter streamwriter = new StreamWriter(fs, HttpContext.Current.Response.ContentEncoding))
146![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
147
streamwriter.Write(content);
148
}
149
}
150
return true;
151
}
152
catch (Exception ex)
153![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
154
HttpContext.Current.Response.Write(ex.Message);
155
return false;
156
}
157![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
158
}
159
#endregion
160![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
161
}
162![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)