htmlparserWord格式文档下载.docx
《htmlparserWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《htmlparserWord格式文档下载.docx(16页珍藏版)》请在冰豆网上搜索。
IEnumerable&
lt;
Tuple&
string,string&
gt;
&
info;
17:
18:
publicHtmlMaker(stringtitle,IEnumerable&
info,DataViewview)
19:
20:
this.title=title;
21:
this.info=info;
22:
this.view=view;
23:
this.TitleVisible=true;
24:
25:
26:
publicvoidSave(stringhtmlFileName)
27:
28:
using(varwriter=newStreamWriter(htmlFileName))
29:
30:
WriteHead(writer);
31:
WriteTitle(writer);
32:
WriteInfo(writer);
33:
newHtmlTable(view).Write(writer);
34:
WriteTail(writer);
35:
36:
37:
38:
voidWriteTitle(TextWriterwriter)
39:
40:
if(!
TitleVisible)return;
41:
writer.Write("
&
h1&
"
);
42:
WebUtility.HtmlEncode(title,writer);
43:
writer.WriteLine("
/h1&
44:
45:
46:
voidWriteInfo(TextWriterwriter)
47:
48:
if(info==null)return;
49:
vardt=newDataTable();
50:
dt.Columns.Add("
Key"
typeof(string));
51:
Value"
52:
foreach(varrowininfo)
53:
54:
vardr=dt.NewRow();
55:
dr[0]=row.Item1;
56:
dr[1]=row.Item2;
57:
dt.Rows.Add(dr);
58:
59:
varhtmlTable=newHtmlTable(dt.DefaultView);
60:
htmlTable.ColumnHeadersVisible=false;
61:
htmlTable.Write(writer);
62:
63:
64:
voidWriteHead(TextWriterwriter)
65:
66:
!
DOCTYPEhtmlPUBLIC\"
-//W3C//DTDXHTML1.0Strict//EN\"
"
67:
\"
http:
//www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"
68:
htmlxmlns=\"
//www.w3.org/1999/xhtml\"
69:
head&
70:
metahttp-equiv=\"
Content-Type\"
content=\"
text/html;
charset=utf-8\"
/&
71:
title&
72:
73:
/title&
74:
/head&
75:
body&
76:
77:
78:
voidWriteTail(TextWriterwriter)
79:
80:
/body&
81:
/html&
82:
83:
84:
注意上述程序第70行表明我们生成的Html文件是使用UTF-8编码,这应该成为一个共识。
而在第28行使用的StreamWriter类默认情况也是使用UTF8Encoding。
还有要注意第42行和第72行,在写入Html文件时要使用WebUtility类的静态方法HtmlEncode对数据进行编码。
在第33行和第59行使用的HtmlTable类的源程序HtmlTable.cs如下所示:
sealedclassHtmlTable
publicintLevel{get;
publicboolColumnHeadersVisible{get;
publicColorColumnHeadersBackgroundColor{get;
publicColorBackgroundColor{get;
publicColorAlternatingRowsBackgroundColor{get;
publicHtmlTable(DataViewview)
Level=1;
ColumnHeadersVisible=true;
ColumnHeadersBackgroundColor=Color.Cyan;
BackgroundColor=Color.Azure;
AlternatingRowsBackgroundColor=Color.LightYellow;
publicvoidWrite(TextWriterwriter)
if(writer==null)return;
if(view==null)return;
varblank="
.PadLeft(Level*2);
writer.Write(blank);
tableborder=\"
1\"
cellspacing=\"
2\"
cellpadding=\"
tbody"
WriteBackgroundColor(writer,BackgroundColor);
if(ColumnHeadersVisible)WriteTableHead(writer);
varrowIndex=0;
foreach(DataRowViewrowinview)WriteTableRow(writer,row,rowIndex++);
/tbody&
/table&
privatevoidWriteBackgroundColor(TextWriterwriter,Colorcolor)
if(color==Color.Empty)return;
style=\"
background-color:
writer.Write(color.ToHtmlCode());
voidWriteTableHead(TextWriterwriter)
varblank2="
.PadLeft((Level+1)*2);
varblank3="
.PadLeft((Level+2)*2);
writer.Write(blank2);
tr"
WriteBackgroundColor(writer,ColumnHeadersBackgroundColor);
foreach(DataColumncolumninview.Table.Columns)
writer.Write(blank3);
th&
WebUtility.HtmlEncode(column.Caption,writer);
/th&
/tr&
voidWriteTableRow(TextWriterwriter,DataRowViewview,introwIndex)
if(rowIndex%2!
=0)WriteBackgroundColor(writer,AlternatingRowsBackgroundColor);
foreach(varfieldinview.Row.ItemArray)
td&
85:
WebUtility.HtmlEncode(field.ToString(),writer);
86:
/td&
87:
88:
89:
90:
91:
92:
几点说明:
第11行的Level字段表示这个&
table&
元素在Html文件中缩进层次,每缩进一层增加两个空格。
第12行的ColumnHeadersVisible字段表示是否要显示该表格的标题行。
第13行的ColumnHeadersBackgroundColor字段表示该表格标题行的背景色,默认值是青色(Cyan)。
第14行的BackgroundColor字段表示该表格的背景色,默认值是天蓝色(Azure)。
第15行的AlternatingRowsBackgroundColor字段表示该表格偶数行的背景色,默认值是浅黄色(LightYellow)。
以上三个字段的值都可以设为Color.Empty,表示继承上一级的背景色。
第50行的ToHtmlCode方法是一个扩展方法,定义于Skyiv.ExtensionMethods静态类中。
下面就是ExtensionMethods.cs:
publicstaticclassExtensionMethods
publicstaticstringToHtmlCode(thisColorcolor)
returnstring.Format("
#{0:
X2}{1:
X2}{2:
X2}"
color.R,color.G,color.B);
使用示例
这让我们来看一个例子吧,下面就是EnumTester.cs:
namespaceSkyiv.Tester
sealedclassEnumTester
staticvoidMain()
try
vartype=typeof(System.IO.FileAttributes);
varhtml=newHtmlMaker("
枚举测试者"
GetInfo(type),GetTable(type).DefaultView);
html.TitleVisible=false;
html.Save("
enum.html"
catch(Exceptionex)
Console.Error.WriteLine(ex);
staticList&
GetInfo(Typetype)
varinfo=newList&
();
info.Add(Tuple.Create("
枚举类型名称"
type.ToString()));
枚举成员数量"
Enum.GetValues(type).Length.ToString()));
returninfo;
staticDataTableGetTable(Typetype)
vartable=newDataTable();
table.Columns.Add("
十进制"
十六进制"
名称"
foreach(varvinEnum.GetValues(type))
vardr=table.NewRow();
dr[0]=Enum.Format(type,v,"
D"
dr[1]=Enum.Format(type,v,"
X"
dr[2]=Enum.Format(type,v,"
G"
table.Rows.Add(dr);
returntable;
在WindowsVista操作系统的.NETFraemwork4环境中编译和运行:
E:
\work&
cscEnumTester.csHtmlMaker.csHtmlTable.csExtensionMethods.cs
Microsoft(R)VisualC#2010编译器4.0.30319.1版
版权所有(C)MicrosoftCorporation。
保留所有权利。
EnumTester
这个EnumTester.exe运行后生成enum.html文件,在谷歌浏览器中的显示效果如下图所示:
这个Html文件的源代码如下所示:
DOCTYPEhtmlPUBLIC"
-//W3C//DTDXHTML1.0Strict//EN"
//www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
htmlxmlns="
//www.w3.org/1999/xhtml"
metahttp-equiv="
Content-Type"
content="
charset=utf-8"
枚举测试者&
tableborder="
1"
cellspacing="
2"
cellpadding="
tbodystyle="
#F0FFFF"
tr&
枚举类型名称&
System.IO.FileAttributes&
trstyle="
#FFFFE0"
枚举成员数量&
14&
#00FFFF"
十进制&
十六进制&
名称&
1&
00000001&
ReadOnly&
2&
00000002&
Hidden&
4&
00000004&
System&
16&
00000010&
Directory&
32&
00000020&
Archive&
64&
00000040&
Device&
128&
00000080&
Normal&
t