DataSet转成HTML的Table输出
2015-12-07 14:56:46 访问(1979) 赞(0) 踩(0)
/// <summary>
/// 转换HTML的<和>
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string ConvertHtmlTagPrefix(string str)
{
return str.Replace("<", "<").Replace(">", ">");
}
/// <summary>
/// DataSet转成HTML的Table输出
/// </summary>
/// <param name="ds"></param>
/// <param name="needConvertHTML"></param>
/// <returns></returns>
public string DataSetToHtmlTable(DataSet ds, bool needConvertHTML)
{
return DataSetToHtmlTable(ds, -98, needConvertHTML);
}
/// <summary>
/// DataSet转成HTML的Table输出
/// </summary>
/// <param name="ds"></param>
/// <param name="needConvertHTML"></param>
/// <returns></returns>
public string DataSetToHtmlTable(DataSet ds, int widthValue, bool needConvertHTML)
{
if (ds == null || ds.Tables.Count == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:DataSet ds为null。"
);
}
string widthStyle = "";
if (widthValue > 0)
{
widthStyle = " width:" + widthValue.ToString() + "px;";
}
else if (widthValue < 0)
{
widthStyle = " width:" + (-widthValue).ToString() + "%;";
}
DataTable dt = ds.Tables[0];
StringBuilder theResult = new StringBuilder();
theResult.Append(@"<table style=""background-color: #CDCDCD; font-family: 仿宋;" + widthStyle + @""" align=""center"" border=""0"" cellpadding=""1"" cellspacing=""1"">");
if (needConvertHTML)
{
theResult.Append(@"<tr style=""background-color: #FFFFFF;"">");
foreach (DataColumn dc in dt.Columns)
{
theResult.Append(@"<td style=""background-color: #FFFFFF; font-size: 18px; font-weight: bold; font-family: 宋体; line-height: 20pt; vertical-align: middle; line-height: 20pt; text-align: center;"" align=""center"" height=""30px"">");
theResult.Append(ConvertHtmlTagPrefix(dc.ColumnName));
}
theResult.Append("</tr>");
foreach (DataRow dr in dt.Rows)
{
theResult.Append(@"<tr style=""background-color: #FFFFFF;"">");
foreach (DataColumn dc in dt.Columns)
{
theResult.Append(@"<td style=""background-color: #FFFFFF; font-size: 14px; font-family: 宋体; line-height: 20pt; vertical-align: middle; line-height: 20pt; padding-left: 5px;"" align=""center"" height=""30px"">");
theResult.Append(ConvertHtmlTagPrefix(dr[dc.ColumnName].ToString()));
theResult.Append("</td>");
}
theResult.Append("</tr>");
}
}
else
{
theResult.Append(@"<tr style=""background-color: #FFFFFF;"">");
foreach (DataColumn dc in dt.Columns)
{
theResult.Append(@"<td style=""background-color: #FFFFFF; font-size: 18px; font-weight: bold; font-family: 宋体; line-height: 20pt; vertical-align: middle; line-height: 20pt; text-align: center;"" align=""center"" height=""30px"">");
theResult.Append(dc.ColumnName);
theResult.Append("</td>");
}
theResult.Append("</tr>");
foreach (DataRow dr in dt.Rows)
{
theResult.Append(@"<tr style=""background-color: #FFFFFF;"">");
foreach (DataColumn dc in dt.Columns)
{
theResult.Append(@"<td style=""background-color: #FFFFFF; font-size: 14px; font-family: 宋体; line-height: 20pt; vertical-align: middle; line-height: 20pt; padding-left: 5px;"" align=""center"" height=""30px"">");
theResult.Append(dr[dc.ColumnName].ToString());
theResult.Append("</td>");
}
theResult.Append("</tr>");
}
}
theResult.Append("</table>");
return theResult.ToString();
}
标签:
DataSet转成HTML的Table输出 


上一条:
下一条:
相关评论
发表评论