2010年1月22日 星期五

[ASP.Net]將Table資料分別寫入Excel中不同工作表(Sheet)中

將Table資料分別寫入Excel中不同工作表(Sheet)中
      
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = Class1.ConnStr;
        conn.Open();
        SqlCommand CMD = new SqlCommand(SqlString, conn);
        ExcelUtil eu = new ExcelUtil("MIS", "GS-ZYI");
        try
        {
            SqlDataReader dr = CMD.ExecuteReader();
           
            while (dr.Read())
            {
                string CID = dr.GetValue(0).ToString();
                string Alias = dr.GetValue(1).ToString();
                SqlCommand QryString = new SqlCommand();
                QryString.CommandText = "Exec ZRealSalesTotal3 @CID = '" + CID + "', @dY='" + dy + "', @dM = '" + dm + "'";
//-[1]-查找資料並放入DataSet
                DataSet ResultSet = RunQuery(QryString);
//-[2]-動態產生DataGrid
                DataGrid DG = new DataGrid();
                DG.ID = CID.ToString();
                this.Controls.Add(DG);
                DG.DataSource = ResultSet;
                DG.AutoGenerateColumns = false;
                BoundColumn c07 = new BoundColumn();
                BoundColumn c08 = new BoundColumn();
                BoundColumn c09 = new BoundColumn();
                BoundColumn c10 = new BoundColumn();
                //BoundColumn c11 = new BoundColumn();
                c07.HeaderText = "規格//項目";c07.DataField = ResultSet.Tables[0].Columns[7].ColumnName;
                c08.HeaderText = "進貨"; c08.DataField = ResultSet.Tables[0].Columns[8].ColumnName;
                c09.HeaderText = "實販"; c09.DataField = ResultSet.Tables[0].Columns[9].ColumnName;
                c10.HeaderText = "庫存"; c10.DataField = ResultSet.Tables[0].Columns[10].ColumnName;
                //c11.HeaderText = "前庫"; c11.DataField = ResultSet.Tables[0].Columns[11].ColumnName;
                DG.Columns.Add(c07);
                DG.Columns.Add(c08);
                DG.Columns.Add(c09);
                DG.Columns.Add(c10);
                //DG.Columns.Add(c11);
                DG.DataBind();
//[-3]-將DataGrid放入各Sheet中
                eu.AddGrid(DG, Alias);
            }
            dr.Close();
        }
        catch (Exception e2)
        {
            //
        }
        conn.Close();
//-[4]-輸出Excel檔       
        eu.Export(this, "GY"+xlsName);
    }
--
看到、想到、說到、做到 
能夠填平大海的誓言,也比不上邁出一步的價值 
是以吾輩此生,再無任何誓言

沒有留言: