Friday, 11 November 2011

" GRID VIEW in asp.net "How are manage


using System.Data.OleDb;



protected void Page_Load(object sender, EventArgs e)

{

        GetExcelSheetNames("Path");

}



private void GetExcelSheetNames(string excelFile)

{

        OleDbConnection objConn = null;

        System.Data.DataTable dt = null;

        try

        {

               DataSet ds = new DataSet();

               // Connection String. 

               String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";Extended Properties=Excel 8.0;";

               // Create connection. 

               objConn = new OleDbConnection(connString);

               // Opens connection with the database. 

               objConn.Open();

               // Get the data table containing the schema guid, and also sheet names. 

               dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

               if (dt == null)

               {

                       return;

               }

               String[] excelSheets = new String[dt.Rows.Count];

               int i = 0;

               // Add the sheet name to the string array. 

               // And respective data will be put into dataset table 

               foreach (DataRow row in dt.Rows)

               {

                       excelSheets[i] = row["TABLE_NAME"].ToString();

                       OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + excelSheets[i] + "]", objConn);

                       OleDbDataAdapter oleda = new OleDbDataAdapter();

                       oleda.SelectCommand = cmd;

                       oleda.Fill(ds, "TABLE");

                       i++;

               }

               // Bind the data to the GridView 

               GridView1.DataSource = ds.Tables[0].DefaultView;

               GridView1.DataBind();

               Session["Table"] = ds.Tables[0];

        }

        catch (Exception ex)

        {

               Response.Write(ex.Message);

               

        }

        finally

        {

               // Clean up. 

               if (objConn != null)

               {

                       objConn.Close();

                       objConn.Dispose();

               }

               if (dt != null)

               {

                       dt.Dispose();

               }

        }

}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

        GridView1.PageIndex = e.NewPageIndex;

        GridView1.DataSource = ((DataTable)Session["Table"]).DefaultView;

        GridView1.DataBind();

}



protected void BtnExport_Click(object sender, EventArgs e)

{

    DataTable dtOriginal = new DataTable();

    dtOriginal = (DataTable)gvRapporten.DataSource; //Return Table consisting data



    //Create Tempory Table

    DataTable dtTemp = new DataTable();



    //Creating Header Row

    dtTemp.Columns.Add("<b>Melder</b>");

    dtTemp.Columns.Add("<b>Onderwerp</b>");

    dtTemp.Columns.Add("<b>Oplosser</b>");

    dtTemp.Columns.Add("<b>Niveau 2</b>");

    DataRow drAddItem;

    for (int i = 0; i < dtOriginal.Rows.Count; i++)

    {

        drAddItem = dtTemp.NewRow();

        drAddItem[0] = dtOriginal.Rows[i][0].ToString();//Melder

        drAddItem[1] = dtOriginal.Rows[i][1].ToString();//Onderwerp

        drAddItem[2] = dtOriginal.Rows[i][2].ToString();//Oplosser

        drAddItem[3] = dtOriginal.Rows[i][3].ToString();//Niveau 2



        dtTemp.Rows.Add(drAddItem);

    }



    //Temp Grid

    DataGrid dg = new DataGrid();

    dg.DataSource = dtTemp;

    dg.DataBind();

    ExportToExcel("Report.xls", dg);

    dg = null;

    dg.Dispose();

}



private void ExportToExcel(string strFileName, DataGrid dg)

{

    Response.ClearContent();

    Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);

    Response.ContentType = "application/excel";

    System.IO.StringWriter sw = new System.IO.StringWriter();

    HtmlTextWriter htw = new HtmlTextWriter(sw);

    dg.RenderControl(htw);

    Response.Write(sw.ToString());

    Response.End();

}

No comments:

Post a Comment