相信你也遇到过这样的一个合并table的情况,如截图:
我的基本思路是
我的生成代码段如下:
代码
/// <summary>
/// 传入组织好的DataSet, 根据内容进行表格HTML代码输出
/// </summary>
/// <param name="dsJobCount">数据源</param>
/// <param name="orderFiledNams">排序字段名</param>
/// <returns></returns>
private string ShowHTMLTable(ref DataSet dsJobCount, string[] orderFiledNams)
{
StringBuilder tablehtml = new StringBuilder();
if (dsJobCount.Tables.Count == 0)
{
return tablehtml.ToString();
}
if (dsJobCount.Tables[0].Rows.Count == 0)
{
return tablehtml.ToString();
}
// 初始化表格合并累计变量
int[] rowspanListCount = new int[orderFiledNams.Length];
InitRowspanListCount(ref rowspanListCount);
// 是否显示带有rowspan的行
bool[] rowspanListShow = new bool[orderFiledNams.Length];
InitRowspanListShow(ref rowspanListShow);
// 将数据源装入DataTable
DataTable dtJobCount = new DataTable();
dtJobCount = dsJobCount.Tables[0];
DataView dvJobCountTemp;
// 开始遍历数据源
/*
<tr>
<td style="width:121px" rowspan="2" align="center">
职能单元</td>
<td style="width:156px" rowspan="2" align="center">
功能单元</td>
<td style="width:116px" rowspan="2" align="center">
次功能单元</td>
<td style="width:117px" rowspan="2" align="center">
职位名称</td>
<td style="width:92px" align="center">
定编人数</td>
<td style="width:109px" align="center">
在编人数</td>
<td style="width:102px" align="center">
超编人数</td>
</tr>
* */
// 职能单元
string templetTd1 = @"<td rowspan=""{0}"" align=""center""><p>{1}</p><p>(在编{2}人)</p></td>";
// 职能单元
string templetTd2 = @"<td rowspan=""{0}"" align=""center""><p>{1}</p><p>(在编{2}人)</p></td>";
// 次功能单元
string templetTd3 = @"<td rowspan=""{0}"" align=""center""><p>{1}</p><p>(在编{2}人)</p></td>";
// 职位名称
string templetTd4 = @"<td align=""center"">{0}</td>";
// 定编人数
string templetTd5 = @"<td align=""center"">{0}</td>";
// 在编人数
string templetTd6 = @"<td align=""center"">{0}</td>";
// 超编人数
string templetTd7 = @"<td align=""center"">{0}</td>";
string rowFilter = string.Empty;
for (int dti = 0; dti < dtJobCount.Rows.Count; dti++)
{
// 初始化
rowFilter = "";
// 开始拼HTML
tablehtml.Append("<tr>");
// 判断并置Rowspan
for (int i = 0; i < orderFiledNams.Length; i++)
{
dvJobCountTemp = dtJobCount.DefaultView;
rowFilter += string.Format(" and {0}=\'{1}\'"
, orderFiledNams[i]
, dtJobCount.Rows[dti][orderFiledNams[i]].ToString());
rowFilter = Regex.Replace(rowFilter, @"^\sand\s", "");
// 统计rowspan值
if (rowspanListShow[i])
{
dvJobCountTemp.RowFilter = rowFilter;
rowspanListCount[i] = dvJobCountTemp.Count;
}
}
#region <td rowspan="3" align="center"><p>直接销售(在编104人)</p></td>
// 职能单元
if (rowspanListShow[0])
{
tablehtml.Append(string.Format(templetTd1
, rowspanListCount[0].ToString()
, dtJobCount.Rows[dti]["fName"].ToString()
, dtJobCount.Rows[dti]["fincount"].ToString()
)
);
}
// 功能单元
if (rowspanListShow[1])
{
tablehtml.Append(string.Format(templetTd2
, rowspanListCount[1].ToString()
, dtJobCount.Rows[dti]["wfName"].ToString()
, dtJobCount.Rows[dti]["inwfcount"].ToString()
)
);
}
// 次功能单元
if (rowspanListShow[2])
{
tablehtml.Append(string.Format(templetTd3
, rowspanListCount[2].ToString()
, dtJobCount.Rows[dti]["swfName"].ToString()
, dtJobCount.Rows[dti]["swfincount"].ToString()
)
);
}
#endregion
#region 装入底层循环数据
// 职位名称
tablehtml.Append(string.Format(templetTd4,
Helper.Decrypt(dtJobCount.Rows[dti]["jobName"].ToString()))
);
// 定编人数
tablehtml.Append(string.Format(templetTd5
, GetJobCountForLabel(Convert.ToInt32(dtJobCount.Rows[dti]["isCount"].ToString())
, dtJobCount.Rows[dti]["settingjobcount"].ToString()))
);
// 在编人数
tablehtml.Append(string.Format(templetTd6,
dtJobCount.Rows[dti]["injobcount"].ToString())
);
tablehtml.Append(string.Format(templetTd7
, GetSuperjobcount(dtJobCount.Rows[dti]["injobcount"].ToString()
, dtJobCount.Rows[dti]["settingjobcount"].ToString()
, Convert.ToInt32(dtJobCount.Rows[dti]["isCount"].ToString()))
)
);// 超编人数
#endregion
tablehtml.Append("</tr>");
// 置累计数据 rowspan - 1
SetRowspanListCountSubtraction(ref rowspanListCount, ref rowspanListShow);
}
return tablehtml.ToString();
}
代码
/// <summary>
/// 在输入框的情况下,判断定编是否初始化
/// 已初始化 - 显示定编
/// 未初始化 - 显示"--"
/// </summary>
/// <param name="inCount"></param>
/// <param name="settingJobCount"></param>
/// <returns></returns>
public string GetJobCountForLabel(int inCount, string settingJobCount)
{
string _result = string.Empty;
if (inCount == 0)
{
_result = "--";
}else if (inCount == 1)
{
_result = settingJobCount;
}
return _result;
}
#region 编制报表合并表格公共方法
/// <summary>
/// 统计rowspan值
/// 如果减1后为0,则如果下一行还有数据就显示带有rowspan的行
/// </summary>
/// <param name="rowspanListCount"></param>
/// <returns></returns>
public void SetRowspanListCountSubtraction(ref int[] rowspanListCount, ref bool[] rowspanListShow)
{
for (int i = 0; i < rowspanListCount.Length; i++)
{
// 统计rowspan值
rowspanListCount[i] = rowspanListCount[i] - 1;
// 如果减1后为0,则如果下一行还有数据就显示带有rowspan的行
if (rowspanListCount[i] == 0)
{
// 下行显示
rowspanListShow[i] = true;
}
else
{
// 下行不显示
rowspanListShow[i] = false;
}
}
}
/// <summary>
/// 置rowspan赋值为0
/// </summary>
/// <param name="rowspanListCount"></param>
/// <returns></returns>
public void InitRowspanListCount(ref int[] rowspanListCount)
{
for (int i = 0; i < rowspanListCount.Length; i++)
{
// 统计rowspan值
rowspanListCount[i] = 0;
}
}
/// <summary>
/// 置控制是否显示带rowspan的行为true
/// </summary>
/// <returns></returns>
public void InitRowspanListShow(ref bool[] rowspanListShow)
{
for (int i = 0; i < rowspanListShow.Length; i++)
{
// 统计rowspan值
rowspanListShow[i] = true;
}
}
#endregion