创建 HTML 文件(表格)






2.74/5 (12投票s)
2004年4月19日
2分钟阅读

68690

2364
一篇关于使用 VC++ 创建 HTML 文件的文章。
引言
这个简单的类可以写入包含表格的 HTML 文件。由于使用了可变参数函数(...),因此行数和列数没有限制。
背景
我需要将列表控件的内容写入 HTML 文件,所以我编写了这个类,它可以写入没有列数或行数限制的表格。这个类是一个快速的实现,几乎没有错误处理。但是你可以将其用作你自己的类的起点。如果你觉得有用,那就好!如果不是,请不要攻击我。;-)
使用代码
这很简单!就像这样
- 使用所有应该显示在文件主体中的字符串构造一个本地的
CHtmlEdit
类对象。CHtmlEdit oHtmlEdt ("Title", // Title of the HTML-File "John Doe", // Author of the HTML-File "Test Table", // The caption of the Table "Marquee Text", // The Scrolling Text "http://codeproject.com", // A Link which will be placed in the Footer "Link to a Cool Site", // Text for the link above "#C0C0C0", // Backgroundcolor of the HTML-File (HTML-Notation) "#000000", // Textcolor of the HTML-File (HTML-Notation) "#ff6666", // Backgroundcolor of the Header (HTML-Notation) "#ffffcc"); // Backgroundcolor of the Table (HTML-Notation)
如果你添加一些内容(例如表格的描述或某些内容)作为滚动文本,则将显示滚动文本(MARQUEE)和两个按钮,一个用于暂停滚动,另一个用于恢复滚动。如果你不想使用 MARQUEE,则不提供任何内容(默认)。
- 将可变数量的列(标题)写入表格
int InsertTableHeader(int iItemNo, // No. of columns CString sFirstItem, // The CString which is to // add to the first column ...); // all CStrings for the columns
这个例子向表格添加 3 列
oHtmlEdt.InsertTableHeader(3, // add 3 columns to the table "Header 1", // string for the headline of column 1 "Header 2", // string for the headline of column 2 "Header 3"); // string for the headline of column 3
你可以添加任意数量的列到表格中,但你必须使用相同数量的字符串(
CString
s)来调用此函数。 - 将可变数量的行写入表格
int InsertTableRow(CString sFirstItem, // The CString which is to // add to the first column ...); // all other CStrings(!)
3 列的示例
oHtmlEdt.InsertTableRow("cell 1", // string for the 1st cell in column 1 "cell 2", // string for the 2nd cell in column 1 "cell 3"); // string for the 3rd cell in column 1
你必须使用与定义的行数相同的字符串(
CString
s)来调用此函数。字符串可以为空,但必须存在。你可以将此函数调用放入一个循环中,在该循环中请求列表控件的单元格。如果字符串包含括号(<
或>
),则该字符串将被转换为电子邮件地址,并且括号将被删除。 - 将 HTML 代码写入文件
void WriteHTMLFile(CString sFullPath); // sFullPath is the path AND the Filename
示例
oHtmlEdt.WriteHTMLFile("TestTable.HTML");
关注点
我学会了如何使用函数的可变数量的参数。
历史
目前还没有计划更新。