private void CreateHtmlFile()
5

{
6
/**////定义和html标记数目一致的数组
7
string[] newContent = new string[5];
8
StringBuilder strhtml = new StringBuilder();
9
try
10
{
11
/**////创建StreamReader对象
12
using (StreamReader sr = new StreamReader(Server.MapPath("createHTML") + "//template.html"))
13
{
14
String oneline;
15
16
/**////读取指定的HTML文件模板
17
while ((oneline = sr.ReadLine()) != null)
18
{
19
strhtml.Append(oneline);
20
}
21
sr.Close();
22
}
23
}
24
catch(Exception err)
25
{
26
/**////输出异常信息
27
Response.Write(err.ToString());
28
}
29
/**////为标记数组赋值
30
newContent[0] = txtTitle.Text;//标题
31
newContent[1] = "BackColor='#cccfff'";//背景色
32
newContent[2] = "#ff0000";//字体颜色
33
newContent[3] = "100px";//字体大小
34
newContent[4] = txtContent.Text;//主要内容
35
36
/**////根据上面新的内容生成html文件
37
try
38
{
39
/**////指定要生成的HTML文件
40
string fname = Server.MapPath("createHTML") +"//" + DateTime.Now.ToString("yyyymmddhhmmss") + ".html";
41
42
/**////替换html模版文件里的标记为新的内容
43
for(int i=0;i < 5;i++)
44
{
45
strhtml.Replace("$htmlkey["+i+"]",newContent[i]);
46
}
47
/**////创建文件信息对象
48
FileInfo finfo = new FileInfo(fname);
49
50
/**////以打开或者写入的形式创建文件流
51
using(FileStream fs = finfo.OpenWrite())
52
{
53
/**////根据上面创建的文件流创建写数据流
54
StreamWriter sw = new StreamWriter(fs,System.Text.Encoding.GetEncoding("GB2312"));
55
56
/**////把新的内容写到创建的HTML页面中
57
sw.WriteLine(strhtml);
58
sw.Flush();
59
sw.Close();
60
}
61
62
/**////设置超级链接的属性
63
hyCreateFile.Text = DateTime.Now.ToString("yyyymmddhhmmss")+".html";
64
hyCreateFile.NavigateUrl = "createHTML/"+DateTime.Now.ToString("yyyymmddhhmmss")+".html";
65
}
66
catch(Exception err)
67
{
68
Response.Write (err.ToString());
69
}
70
}
5


{ 6

/**////定义和html标记数目一致的数组7
string[] newContent = new string[5];8
StringBuilder strhtml = new StringBuilder();9
try 10

{11

/**////创建StreamReader对象12
using (StreamReader sr = new StreamReader(Server.MapPath("createHTML") + "//template.html")) 13

{14
String oneline;15
16

/**////读取指定的HTML文件模板17
while ((oneline = sr.ReadLine()) != null) 18

{19
strhtml.Append(oneline);20
}21
sr.Close();22
}23
}24
catch(Exception err)25

{26

/**////输出异常信息27
Response.Write(err.ToString());28
}29

/**////为标记数组赋值30
newContent[0] = txtTitle.Text;//标题31
newContent[1] = "BackColor='#cccfff'";//背景色32
newContent[2] = "#ff0000";//字体颜色33
newContent[3] = "100px";//字体大小34
newContent[4] = txtContent.Text;//主要内容35

36

/**////根据上面新的内容生成html文件37
try38

{39

/**////指定要生成的HTML文件40
string fname = Server.MapPath("createHTML") +"//" + DateTime.Now.ToString("yyyymmddhhmmss") + ".html";41
42

/**////替换html模版文件里的标记为新的内容43
for(int i=0;i < 5;i++)44

{45
strhtml.Replace("$htmlkey["+i+"]",newContent[i]);46
}47

/**////创建文件信息对象48
FileInfo finfo = new FileInfo(fname);49
50

/**////以打开或者写入的形式创建文件流51
using(FileStream fs = finfo.OpenWrite())52

{53

/**////根据上面创建的文件流创建写数据流54
StreamWriter sw = new StreamWriter(fs,System.Text.Encoding.GetEncoding("GB2312"));55
56

/**////把新的内容写到创建的HTML页面中57
sw.WriteLine(strhtml);58
sw.Flush();59
sw.Close();60
}61
62

/**////设置超级链接的属性63
hyCreateFile.Text = DateTime.Now.ToString("yyyymmddhhmmss")+".html";64
hyCreateFile.NavigateUrl = "createHTML/"+DateTime.Now.ToString("yyyymmddhhmmss")+".html";65
}66
catch(Exception err)67

{ 68
Response.Write (err.ToString());69
}70
}
此博客展示了使用C#创建并生成HTML文件的代码。通过StreamReader读取HTML模板文件,将新内容存储在数组中,再用StringBuilder替换模板里的标记,最后使用FileStream和StreamWriter将新内容写入新的HTML文件。

2560

被折叠的 条评论
为什么被折叠?



