如何将通用列表的内容转换为 HTML?
How can I convert the contents of a generic List to HTML?
我需要将自定义 class 的通用列表的内容转换为 html。例如,如果我在通用列表中存储 class 的值,如下所示:
public class PlatypusSummary
{
public String PlatypusName { get; set; }
public int TotalOccurrencesOfSummaryItems { get; set; }
public int TotalSummaryExceptions { get; set; }
public double TotalPercentageOfSummaryExceptions { get; set; }
}
...这样我就得到了一个像这样的通用列表:
List<PlatypusSummary> _PlatypusSummaryList = null;
. . .
var dbp = new PlatypusSummary
{
PlatypusName = summary["duckbillname"].ToString(),
TotalOccurrencesOfSummaryItems = totalOccurrences,
TotalSummaryExceptions = totalExceptions,
TotalPercentageOfSummaryExceptions = totalPercentage
};
_PlatypusSummaryList.Add(dbp);
...如何将该通用列表的内容转换为 HTML?
这是从该列表中生成一些 "plain vanilla" HTML 的方法。这可以适用于其他 class 类型(将 "PlatypusSummary" 替换为您的 class)和 class 成员(将 "PlatypusName" 和 foreach 循环中的其他值替换为您 class 的成员):
internal static string ConvertDuckbillSummaryListToHtml(List<PlatypusSummary> _PlatypusSummaryList)
{
StringBuilder builder = new StringBuilder();
// Add the html opening tags and "preamble"
builder.Append("<html>");
builder.Append("<head>");
builder.Append("<title>");
builder.Append("bla"); // TODO: Replace "bla" with something less blah
builder.Append("</title>");
builder.Append("</head>");
builder.Append("<body>");
builder.Append("<table border='1px' cellpadding='5' cellspacing='0' ");
builder.Append("style='border: solid 1px Silver; font-size: x-small;'>");
// Add the column names row
builder.Append("<tr align='left' valign='top'>");
PropertyInfo[] properties = typeof(PlatypusSummary).GetProperties();
foreach (PropertyInfo property in properties)
{
builder.Append("<td align='left' valign='top'><b>");
builder.Append(property.Name);
builder.Append("</b></td>");
}
builder.Append("</tr>");
// Add the data rows
foreach (PlatypusSummary ps in _PlatypusSummaryList)
{
builder.Append("<tr align='left' valign='top'>");
builder.Append("<td align='left' valign='top'>");
builder.Append(ps.PlatypusName);
builder.Append("</td>");
builder.Append("<td align='left' valign='top'>");
builder.Append(ps.TotalOccurrencesOfSummaryItems);
builder.Append("</td>");
builder.Append("<td align='left' valign='top'>");
builder.Append(ps.TotalSummaryExceptions);
builder.Append("</td>");
builder.Append("<td align='left' valign='top'>");
builder.Append(ps.TotalPercentageOfSummaryExceptions);
builder.Append("</td>");
builder.Append("</tr>");
}
// Add the html closing tags
builder.Append("</table>");
builder.Append("</body>");
builder.Append("</html>");
return builder.ToString();
}
注意:此代码改编自 Sumon Banerjee 的 "Data Table to HTML" 代码 here。
我需要将自定义 class 的通用列表的内容转换为 html。例如,如果我在通用列表中存储 class 的值,如下所示:
public class PlatypusSummary
{
public String PlatypusName { get; set; }
public int TotalOccurrencesOfSummaryItems { get; set; }
public int TotalSummaryExceptions { get; set; }
public double TotalPercentageOfSummaryExceptions { get; set; }
}
...这样我就得到了一个像这样的通用列表:
List<PlatypusSummary> _PlatypusSummaryList = null;
. . .
var dbp = new PlatypusSummary
{
PlatypusName = summary["duckbillname"].ToString(),
TotalOccurrencesOfSummaryItems = totalOccurrences,
TotalSummaryExceptions = totalExceptions,
TotalPercentageOfSummaryExceptions = totalPercentage
};
_PlatypusSummaryList.Add(dbp);
...如何将该通用列表的内容转换为 HTML?
这是从该列表中生成一些 "plain vanilla" HTML 的方法。这可以适用于其他 class 类型(将 "PlatypusSummary" 替换为您的 class)和 class 成员(将 "PlatypusName" 和 foreach 循环中的其他值替换为您 class 的成员):
internal static string ConvertDuckbillSummaryListToHtml(List<PlatypusSummary> _PlatypusSummaryList)
{
StringBuilder builder = new StringBuilder();
// Add the html opening tags and "preamble"
builder.Append("<html>");
builder.Append("<head>");
builder.Append("<title>");
builder.Append("bla"); // TODO: Replace "bla" with something less blah
builder.Append("</title>");
builder.Append("</head>");
builder.Append("<body>");
builder.Append("<table border='1px' cellpadding='5' cellspacing='0' ");
builder.Append("style='border: solid 1px Silver; font-size: x-small;'>");
// Add the column names row
builder.Append("<tr align='left' valign='top'>");
PropertyInfo[] properties = typeof(PlatypusSummary).GetProperties();
foreach (PropertyInfo property in properties)
{
builder.Append("<td align='left' valign='top'><b>");
builder.Append(property.Name);
builder.Append("</b></td>");
}
builder.Append("</tr>");
// Add the data rows
foreach (PlatypusSummary ps in _PlatypusSummaryList)
{
builder.Append("<tr align='left' valign='top'>");
builder.Append("<td align='left' valign='top'>");
builder.Append(ps.PlatypusName);
builder.Append("</td>");
builder.Append("<td align='left' valign='top'>");
builder.Append(ps.TotalOccurrencesOfSummaryItems);
builder.Append("</td>");
builder.Append("<td align='left' valign='top'>");
builder.Append(ps.TotalSummaryExceptions);
builder.Append("</td>");
builder.Append("<td align='left' valign='top'>");
builder.Append(ps.TotalPercentageOfSummaryExceptions);
builder.Append("</td>");
builder.Append("</tr>");
}
// Add the html closing tags
builder.Append("</table>");
builder.Append("</body>");
builder.Append("</html>");
return builder.ToString();
}
注意:此代码改编自 Sumon Banerjee 的 "Data Table to HTML" 代码 here。