经常碰到客户要的表格需要合并行:
代码如下:
#region 查询事件
/// <summary>
/// 查询事件
/// </summary>
protected void btnStat_Click(object sender, EventArgs e)
{
try
{
//得到显示字段
string strShowFields = getSelectFields();
StringBuilder sbSql = new StringBuilder();
if (strShowFields == "")
{
sbSql.Append("SELECT COUNT(StudNo) AS 学生人数 FROM (");
//得到数据表语句
sbSql.Append(getSelectTable());
sbSql.Append(") A");
}
else
{
sbSql.Append("SELECT ");
//得到显示字段
sbSql.Append(strShowFields);
sbSql.Append(",COUNT(StudNo) AS 学生人数 FROM (");
//得到数据表语句
sbSql.Append(getSelectTable());
sbSql.Append(") A GROUP BY ");
//得到分组字段
sbSql.Append(getGroupByFields());
sbSql.Append(" ORDER BY ");
sbSql.Append(getGroupByFields());
}
//获得数据集信息
DataTable dtInfo = MSSQLHelper.adGetDataTable(adConn, sbSql.ToString());
processTable(dtInfo);
drawTable(dtInfo);
tbExcel.Visible = true;
}
catch
{ }
}
/// <summary>
/// 获得显示字段
/// </summary>
/// <returns></returns>
private string getSelectFields()
{
StringBuilder sbFields = new StringBuilder();
sbFields.Append("");
for (int i = 0; i < chkGroup.Items.Count; i++)
{
if (chkGroup.Items[i].Selected)
{
sbFields.Append(chkGroup.Items[i].Value + " AS " + chkGroup.Items[i].Text + ",");
}
}
string strFields = sbFields.ToString();
if (strFields.EndsWith(","))
{
strFields = strFields.Substring(0, strFields.Length - 1);
}
return strFields;
}
/// <summary>
/// 获得分组字段
/// </summary>
/// <returns></returns>
private string getGroupByFields()
{
StringBuilder sbFields = new StringBuilder();
sbFields.Append("");
for (int i = 0; i < chkGroup.Items.Count; i++)
{
if (chkGroup.Items[i].Selected)
{
sbFields.Append(chkGroup.Items[i].Value + ",");
}
}
string strFields = sbFields.ToString();
if (strFields.EndsWith(","))
{
strFields = strFields.Substring(0, strFields.Length - 1);
}
return strFields;
}
/// <summary>
/// 获得查询表
/// </summary>
/// <returns></returns>
private string getSelectTable()
{
StringBuilder sbSelectSql = new StringBuilder();
sbSelectSql.Append("SELECT StudNo,StudName,DeptName,Grade,SpecName,Folk FROM vstud_student WHERE 1=1 ");
//院系
if (ddlDept.SelectedValue.ToString() != "-1")
{
sbSelectSql.Append(" AND DeptID='" + ddlDept.SelectedValue.ToString() + "'");
}
//年级
if (ddlGrade.SelectedValue.ToString() != "-1")
{
sbSelectSql.Append(" AND Grade='" + ddlGrade.SelectedValue.ToString() + "'");
}
//专业
if (ddlSpecName.SelectedValue.ToString() != "-1")
{
sbSelectSql.Append(" AND SpecName='" + ddlSpecName.SelectedValue.ToString() + "'");
}
//民族
if (ddlFolk.SelectedValue.ToString() != "-1")
{
sbSelectSql.Append(" AND FolkCode='" + ddlFolk.SelectedValue.ToString() + "'");
}
return sbSelectSql.ToString();
}
//存储需待合并的行
private void processTable(System.Data.DataTable tb)
{
for (int i = 1; i < tb.Rows.Count; i++)
{
for (int j = 0; j < tb.Columns.Count; j++)
{
if (tb.Columns[j].ColumnName != "学生人数")
{
if (tb.Rows[i][j].ToString() != lastColunmValue(tb, i, j))
break;
else
{
tb.Rows[i][j] = "";
}
}
}
}
}
// 判断合并的最后一行
private string lastColunmValue(System.Data.DataTable tb, int r, int c)
{
for (int i = r - 1; i >= 0; i--)
{
if (tb.Rows[i][c].ToString() == "")
continue;
else
return tb.Rows[i][c].ToString();
}
return "";
}
//绘制表格
private void drawTable(System.Data.DataTable tb)
{
this.PlaceHolder1.Controls.Clear();
Table Table1 = new Table();
Table1.CssClass = "displaytable";
Table1.CellPadding = 0;
Table1.CellSpacing = 0;
Table1.Style.Add("width", "100%");
TableRow r = null;
TableCell c = null;
r = new TableRow();
for (int i = 0; i < tb.Columns.Count; i++)
{
c = new TableCell();
c.CssClass = "secondlytdcenter";
c.Text = tb.Columns[i].ColumnName;
r.Cells.Add(c);
}
Table1.Rows.Add(r);
TableCell[] oldColums = new TableCell[tb.Columns.Count];
for (int i = 0; i < oldColums.Length; i++)
oldColums[i] = null;
for (int i = 0; i < tb.Rows.Count; i++)
{
r = new TableRow();
for (int j = 0; j < tb.Columns.Count; j++)
{
string strValue = tb.Rows[i][j].ToString();
if (tb.Columns[j].ColumnName != "学生人数")
{
if (strValue == "")
oldColums[j].RowSpan++;
else
{
c = new TableCell();
oldColums[j] = c;
c.RowSpan = 1;
c.CssClass = "firsttdcenter";
c.Text = strValue;
r.Cells.Add(c);
}
}
else
{
c = new TableCell();
c.RowSpan = 1;
oldColums[j] = c;
c.CssClass = "firsttdcenter";
c.Text = strValue;
r.Cells.Add(c);
}
}
Table1.Rows.Add(r);
}
this.PlaceHolder1.Controls.Add(Table1);
}
#endregion