Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I have some data in gridview controls now I want to download as a Excel file and .pdf formats how can I convert and download it?
Posted
Updated 1-Mar-13 0:39am
v2
Comments
FspFriends 1-Mar-13 6:39am    
foreach (GridViewRow gvrow in gvMISReport.Rows)
{
gvrow.BackColor = Color.White;
if (j <= gvMISReport.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
gvMISReport.RenderControl(htw);


In this line it ll terminate

Pleas have a look at this article: Export Data to Excel, Word, PDF without Automation from DataBase.

It introduces how to create a wizard to export data to Excel, PDF and other document formats. And the data will be exported to DataGridView firstly and then export to Excel, PDF from this DataGridView.
 
Share this answer
 
For PDF:
HTML convert to PDF using itextsharp[^]

For Excel:
protected void btnExpExcel_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Report.xls"));
            Response.ContentType = "application/ms-excel";
 
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
 
            DataTable dt = new DataTable();
            dt = (DataTable)Session["dtReport"];
 
            gvMISReport.AllowPaging = false;
 
            gvMISReport.DataSource = dt;
            gvMISReport.DataBind();
 
            //Change the Header Row back to white color
            gvMISReport.HeaderRow.Style.Add("background-color", "#FFFFFF");
            
            //Applying stlye to gridview header cells
            for (int i = 0; i < gvMISReport.HeaderRow.Cells.Count; i++)
            {
                gvMISReport.HeaderRow.Cells[i].Style.Add("background-color", "#157CB0");
            }
            int j = 1;
            
            //This loop is used to apply stlye to cells based on particular row
            foreach (GridViewRow gvrow in gvMISReport.Rows)
            {
                gvrow.BackColor = Color.White;
                if (j <= gvMISReport.Rows.Count)
                {
                    if (j % 2 != 0)
                    {
                        for (int k = 0; k < gvrow.Cells.Count; k++)
                        {
                            gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                        }
                    }
                }
                j++;
            }
            gvMISReport.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
 
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