Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a gridview whick shows the data from the database.now i want that to be export on execl file
how will i do it??
Posted

C#
protected void btnExport_Click(object sender, EventArgs e)
   {
           string strReport ="Report";
           GetGridDetails(true); // Function to Bind GridView Data
           ExportDataGrid(dgSRDetails, strReport);
   }

 public void ExportDataGrid(DataGrid oGrid, string exportFile)
    {
        
            //Clear the response, and set the content type and mark as attachment
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=\"" + exportFile + "\"");
            Response.ContentType = "application/excel";

            //Clear the character set
            Response.Charset = "";

            //Create a string and Html writer needed for output
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            //Clear the controls from the pased grid
            ClearControls(oGrid);

            //Show grid lines
            oGrid.GridLines = GridLines.Both;

            //Color header
            oGrid.HeaderStyle.BackColor = System.Drawing.Color.LightGray;

            //Render the grid to the writer
            oGrid.RenderControl(oHtmlTextWriter);

            Response.Write(oStringWriter.ToString());

            Response.Flush();
            Response.End();
    }

 private void ClearControls(Control control)
    {
        //Recursively loop through the controls, calling this method
        for (int i = control.Controls.Count - 1; i >= 0; i--)
        {
            ClearControls(control.Controls[i]);
        }

        //If we have a control that is anything other than a table cell
        if (!(control is TableCell))
        {
            if (control.GetType().GetProperty("SelectedItem") != null)
            {
                LiteralControl literal = new LiteralControl();
                control.Parent.Controls.Add(literal);
                try
                {
                    literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);
                }
                catch
                {
                }
                control.Parent.Controls.Remove(control);
            }
            else if (control.GetType().GetProperty("Text") != null)
            {
                LiteralControl literal = new LiteralControl();
                control.Parent.Controls.Add(literal);
                literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control, null);
                control.Parent.Controls.Remove(control);
            }
        }
        return;
    }
 
Share this answer
 
v2
Comments
fak_farrukh 26-Apr-13 2:08am    
GetGridDetails(true);
cant find this??
I have used ClosedXML in multiple projects and it works great and it's easy to use.

http://closedxml.codeplex.com/[^]

Create excel worksheet from datatable:
C#
var wb = new XLWorkbook();
var dataTable = GetTable("Information");
wb.Worksheets.Add(dataTable);
wb.SaveAs("AddingDataTableAsWorksheet.xlsx");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900