public partial class _Default : System.Web.UI.Page
    {
        int dateNum = 0;
        int sumNum = 0;
protected void Button1_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            string sqlCon = "Data Source=.;Initial Catalog=CSR;Integrated Security=True;";
            SqlConnection sql = new SqlConnection(sqlCon);
            sql.Open();
            DataTable dt = new DataTable();
            SqlDataAdapter sqlAdapter1 = new SqlDataAdapter("select shuju.no,pingxiang.name,pingxiang.type,shuju.nun,shuju.date from shuju left join pingxiang on shuju.no=pingxiang.no order by name", sqlCon);
            sqlAdapter1.Fill(dt);
            DataColumn column = new DataColumn("numAndDate");
            dt.Columns.Add(column);
            DataTable dtDate = new DataTable();
            SqlDataAdapter sqlAdapter2 = new SqlDataAdapter("select distinct shuju.date from shuju", sqlCon);
            sqlAdapter2.Fill(dtDate);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dataRow = dt.Rows[i];
                dataRow["numAndDate"] = dataRow["nun"] + "#" + dataRow["date"];
            }            Table CSRTable = new Table();
            CSRTable.CellPadding = 2;
            CSRTable.CellSpacing = 0;
            CSRTable.BorderWidth = 1;
            CSRTable.Style["width"] = "715";
            CSRTable.Style["table-layout"] = "fixed";
            CSRTable.ID = "Table1";
            Panel1.Controls.Add(CSRTable);
            TableRow row = new TableRow();
            row.BorderWidth = 1;
            row.Style["color"] = "#E7DCEF";
            row.Style["font-weight"] = "bold";
            TableCell cell = new TableCell();
            DataTable dtTitle = new DataTable();
            dateNum = dtDate.Rows.Count;
            #region 第一行标题
            for (int i = 0; i < dtDate.Rows.Count + 2; i++)
            {
                cell = new TableCell();
                cell.BackColor = Color.Plum;
                if (i == 0)
                {
                    cell.Text = "品项";
                    cell.ColumnSpan = 3;
                    cell.HorizontalAlign = HorizontalAlign.Center;                }
                    //存放不重复的天数
                else if (i < dtDate.Rows.Count +1)
                {
                    Label lb = new Label();
                    lb.ID = Convert.ToDateTime(dtDate.Rows[i-1 ]["date"].ToString()).ToString("yyyyMMdd");
                    lb.Text = Convert.ToDateTime(dtDate.Rows[i-1 ]["date"].ToString()).ToShortDateString();
                    lb.SkinID = i.ToString();
                    lb.Width = cell.Width;
                    cell.Controls.Add(lb);
                }
                else
                {
                    cell.Text = "合计";
                }
                cell.BorderWidth = 1;
                row.Cells.Add(cell);
            }
            CSRTable.Rows.Add(row);
            row.Style["backGround-color"] = "#E7DCEF";
            row.Style["color"] = "#E7DCEF";
            row.BorderWidth = 1;
            row.Style["color"] = "#E7DCEF";
            row.Style["font-weight"] = "bold";
            #endregion
            #region 显示数据
            //相同品项合并成一行
            for (int i = 0; i <dt.Rows.Count; i++)
            {
                DataRow dataRow =dt.Rows[i];
                string no = dataRow["no"].ToString();
                for (int j = i + 1; j < dt.Rows.Count; j++)
                {
                    DataRow dr =dt.Rows[j];
                    if (no == dr["no"].ToString())
                    {
                        dataRow["numAndDate"] = dataRow["numAndDate"] + "@" + dr["numAndDate"];
                        dr.Delete();
                    }
                }
                dt.AcceptChanges();
            }
            for (int j = 0; j < dt.Rows.Count + 2; j++)
            {                row = new TableRow();
                for (int col = 0; col < dt.Columns.Count - 2; col++)
                {
                    if (j < dt.Rows.Count)
                    {
                        if (col == 0)
                        {
                            cell = new TableCell();
                            cell.BorderWidth = 1;
                            cell.Text = dt.Rows[j]["no"].ToString();
                            row.Cells.Add(cell);                        }
                        else if (col == 1)
                        {
                            cell = new TableCell();
                            cell.BorderWidth = 1;
                            cell.Text = dt.Rows[j]["name"].ToString();
                            row.Cells.Add(cell);
                        }
                        else if (col == 2)
                        {
                            cell = new TableCell();
                            cell.BorderWidth = 1;
                            cell.Wrap = false;
                            cell.Text = dt.Rows[j]["type"].ToString();
                            row.Cells.Add(cell);
                        }
                        else if (col > 2 && dt.Rows[j]["nun"].ToString() != string.Empty)
                        {
                            Hashtable colIdList = new Hashtable();
                            string xx = dt.Rows[j]["numAndDate"].ToString();
                            string[] numAndDate = dt.Rows[j]["numAndDate"].ToString().Split('@');
                            for (int p = 0; p < numAndDate.Length; p++)
                            {
                                string num = numAndDate[p].ToString().Split('#')[0];
                                string date = string.Empty;
                                string id=string.Empty;
                                if (numAndDate[p].ToString().IndexOf("#") != -1)
                                {
                                    date = numAndDate[p].ToString().Split('#')[1];
                                    id = Convert.ToDateTime(date).ToString("yyyyMMdd");
                                    Label lb = (Label)(Panel1.FindControl(id));
                                    int ss = Convert.ToInt32(lb.SkinID);
                                    colIdList.Add(ss, num);
                                }                               
                            }
                            for (int dno = 1; dno <= dateNum + 1; dno++)
                            {
                                if (colIdList.Contains(dno))
                                {
                                    cell = new TableCell();
                                    cell.BorderWidth = 1;
                                    cell.Text = colIdList[dno].ToString();
                                    cell.BorderWidth = 1;
                                    row.Cells.Add(cell);
                                    sumNum += int.Parse(colIdList[dno].ToString());
                                }                                else if (dno == dateNum + 1)
                                {
                                    cell = new TableCell();
                                    cell.Text = sumNum.ToString();
                                    cell.BorderWidth = 1;
                                    row.Cells.Add(cell);
                                    sumNum = 0;
                                }
                                else
                                {
                                    cell = new TableCell();
                                    cell.Text = "0";
                                    cell.BorderWidth = 1;
                                    row.Cells.Add(cell);
                                }
                            }
                        }                    }                }
                CSRTable.Rows.Add(row);
            }
            #endregion