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

I have a GirdView that contains values. Now i'm going to download those values as .pdf file. And also Excel formats
Posted

Dear friend,
Pass dataset, filename and Worksheet name ...........

Look into the following code

private void ExportToExcel(DataTable dt, string fileName, string worksheetName)
   {
       Response.Clear();
       Response.AddHeader("content-disposition", "attachment;filename=" + fileName + "");
       Response.ContentType = "application/vnd.ms-excel";

       StringWriter stringWriter = new StringWriter();
       HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
       DataGrid dataExportExcel = new DataGrid();
       dataExportExcel.ItemDataBound += new DataGridItemEventHandler(dataExportExcel_ItemDataBound);
       dataExportExcel.DataSource = dt;
       dataExportExcel.DataBind();
       dataExportExcel.RenderControl(htmlWrite);
       StringBuilder sbResponseString = new StringBuilder();
       sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>" + worksheetName + "</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>");
       sbResponseString.Append(stringWriter + "</body></html>");
       Response.Write(sbResponseString.ToString());
       Response.End();
   }

   void dataExportExcel_ItemDataBound(object sender, DataGridItemEventArgs e)
   {
       if (e.Item.ItemType == ListItemType.Header)
       {
           //Header Text Format can be done as follows
           e.Item.Font.Bold = true;

           //Adding Filter/Sorting functionality for the Excel
           int cellIndex = 0;
           while (cellIndex < e.Item.Cells.Count)
           {
               e.Item.Cells[cellIndex].Attributes.Add("x:autofilter", "all");
               e.Item.Cells[cellIndex].Width = 200;
               e.Item.Cells[cellIndex].HorizontalAlign = HorizontalAlign.Center;
               cellIndex++;
           }
       }

       if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
       {
           int cellIndex = 0;
           while (cellIndex < e.Item.Cells.Count)
           {
               //Any Cell specific formatting should be done here
               e.Item.Cells[cellIndex].HorizontalAlign = HorizontalAlign.Left;
               cellIndex++;
           }
       }
   }
 
Share this answer
 
Hi
Use iTextSharp to conver Gridview to 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
 
Comments
FspFriends 27-Feb-13 23:32pm    
In this code I have a error on this line

gvMISReport.RenderControl(htw);

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