根据DataSet动态创建页面table表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void CreateTable()
{
try
{
PAP_EvaluatioinScoreBLL bll = new PAP_EvaluatioinScoreBLL();
DataTable dt = new DataTable();
dt = bll.GetEvaluationYearavgScore(userId, year, month, Type);
TabStat.Rows.Clear();
TableRow TR = new TableRow();
TableCell TC = new TableCell();
#region 创建表头
TR = new TableRow();
TR.CssClass = "RowTitle";
TC = new TableCell();
TC.CssClass = "CellTitle";
TC.BorderWidth = 1;
TC.RowSpan = 2;
TC.Width = Unit.Percentage(8);
TC.Text = "指标名称";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Text = "评价的主要内容";
TC.CssClass = "CellTitle";
TC.BorderWidth = 1;
TC.RowSpan = 2;
TC.Width = Unit.Percentage(32);
TR.Cells.Add(TC);
TC = new TableCell();
TC.Text = "权重";
TC.RowSpan = 2;
TC.CssClass = "CellTitle";
TC.BorderWidth = 1;
TC.Width = Unit.Percentage(8);
TR.Cells.Add(TC);
TC = new TableCell();
TC.Text = "评分";
TC.CssClass = "CellTitle";
TC.BorderWidth = 1;
TC.ColumnSpan = 6;
TR.Cells.Add(TC);
TC = new TableCell();
TC.Text = "备注";
TC.CssClass = "CellTitle";
TC.BorderWidth = 1;
TC.RowSpan = 2;
TC.Width = Unit.Percentage(10);
TR.Cells.Add(TC);
TabStat.Rows.Add(TR);
TR = new TableRow();
TR.CssClass = "RowTitle";
TC = new TableCell();
TC.Text = "很差(<60)";
TC.CssClass = "CellTitle";
TC.BorderWidth = 1;
TC.Width = Unit.Percentage(6);
TR.Cells.Add(TC);
TC = new TableCell();
TC.Text = "差(60-69)";
TC.CssClass = "CellTitle";
TC.BorderWidth = 1;
TC.Width = Unit.Percentage(6);
TR.Cells.Add(TC);
TC = new TableCell();
TC.Text = "一般(70-79)";
TC.CssClass = "CellTitle";
TC.BorderWidth = 1;
TC.Width = Unit.Percentage(6);
TR.Cells.Add(TC);
TC = new TableCell();
TC.Text = "好(80-89)";
TC.CssClass = "CellTitle";
TC.BorderWidth = 1;
TC.Width = Unit.Percentage(6);
TR.Cells.Add(TC);
TC = new TableCell();
TC.Text = "很好(90-100)";
TC.CssClass = "CellTitle";
TC.BorderWidth = 1;
TC.Width = Unit.Percentage(6);
TR.Cells.Add(TC);
TC = new TableCell();
TC.Text = "得分";
TC.CssClass = "CellTitle";
TC.BorderWidth = 1;
TC.Width = Unit.Percentage(6);
TR.Cells.Add(TC);
TabStat.Rows.Add(TR);
#endregion
#region 创建表体
int iRowSpan = 1;
int iRowSpan2 = 1;
int iRowSpan3 = 1;
int iRowCount = 2;
int iiRowCount = 2;
int iiiRowCount = 2;
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TR = new TableRow();
TR.CssClass = "RowContent";
if (i == 0)
{
TC = new TableCell();
TC.Width = Unit.Percentage(8);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = dt.Rows[i]["TargetName"].ToString() + "<br/>" + dt.Rows[i]["ETWeight"].ToString() + "%";
TC.RowSpan = 1;
iRowSpan = 1;
TR.Cells.Add(TC);
iRowCount = 2;
}
else
{
string s = dt.Rows[i]["TargetName"].ToString();
string ss = TabStat.Rows[iRowCount].Cells[0].Text.ToString().Split('<')[0].ToString();
if (dt.Rows[i]["TargetName"].ToString() == TabStat.Rows[iRowCount].Cells[0].Text.ToString().Split('<')[0].ToString())
{
iRowSpan++;
TabStat.Rows[iRowCount].Cells[0].RowSpan = iRowSpan;
}
else
{
TC = new TableCell();
TC.Width = Unit.Percentage(8);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.RowSpan = 1;
iRowSpan = 1;
TC.Text = dt.Rows[i]["TargetName"].ToString() + "<br/>" + dt.Rows[i]["ETWeight"].ToString() + "%";
TR.Cells.Add(TC);
iRowCount = i + 2;
}
}
TC = new TableCell();
TC.Width = Unit.Percentage(32);
TC.CssClass = "CellContent";
TC.BorderWidth = 1;
TC.Text = dt.Rows[i]["EvaluationTargetDescription"].ToString();
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(8);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = dt.Rows[i]["AppraiserWeight"].ToString() + "%";
TR.Cells.Add(TC);
#region 评分
int sScore = Convert.ToInt32(dt.Rows[i]["avgscore"].ToString());
if (sScore < 60)
{
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = dt.Rows[i]["avgscore"].ToString();
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
}
else if (sScore >= 60 && sScore <= 69)
{
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = dt.Rows[i]["avgscore"].ToString();
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
}
else if (sScore >= 70 && sScore <= 79)
{
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = dt.Rows[i]["avgscore"].ToString();
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
}
else if (sScore >= 80 && sScore <= 89)
{
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = dt.Rows[i]["avgscore"].ToString();
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
}
else if (sScore >= 90 && sScore <= 100)
{
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(6);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = dt.Rows[i]["avgscore"].ToString();
TR.Cells.Add(TC);
}
#endregion
float quanzhong = float.Parse("0." + dt.Rows[i]["AppraiserWeight"].ToString());
float zongfen = 0;
if (i == 0)
{
zongfen = sScore * quanzhong;
TC = new TableCell();
TC.Width = Unit.Percentage(8);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.Text = "";
TC.RowSpan = 1;
iRowSpan2 = 1;
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(10);
TC.CssClass = "CellContent";
TC.BorderWidth = 1;
TC.Text = "";
TC.RowSpan = 1;
iRowSpan3 = 1;
TR.Cells.Add(TC);
iiRowCount = 2;
iiiRowCount = 2;
}
else
{
if (dt.Rows[i]["TargetName"].ToString() == TabStat.Rows[iiRowCount].Cells[0].Text.ToString().Split('<')[0].ToString())
{
iRowSpan2++;
iRowSpan3++;
zongfen += sScore * quanzhong;
TabStat.Rows[iiRowCount].Cells[8].RowSpan = iRowSpan2;
TabStat.Rows[iiiRowCount].Cells[9].RowSpan = iRowSpan3;
TabStat.Rows[iiRowCount].Cells[8].Text = zongfen.ToString();
}
else
{
TC = new TableCell();
TC.Width = Unit.Percentage(8);
TC.CssClass = "CellContent2";
TC.BorderWidth = 1;
TC.RowSpan = 1;
iRowSpan2 = 1;
TC.Text = "";
TR.Cells.Add(TC);
TC = new TableCell();
TC.Width = Unit.Percentage(10);
TC.CssClass = "CellContent";
TC.BorderWidth = 1;
TC.RowSpan = 1;
iRowSpan3 = 1;
TC.Text = "";
TR.Cells.Add(TC);
iiRowCount = i + 2;
iiiRowCount = i + 2;
}
}
TabStat.Rows.Add(TR);
}
}
#endregion
}
catch (Exception)
{
this.lblError.Text = "查询数据错误!";
}
}
需求:用户给了一张员工绩效考核Excel表格,要求根据表格做一个页面,数据从后台取,通过计算显示在页面上,然后打印。
excel表格:
取得的dataset:
代码: