Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear sir,
I fetch data from database, currently i need how to export data gridview to excel or MSoffice and i need to create the salary slip of particular employee based on the standard format salaryslip.
Posted

 
Share this answer
 
Comments
Espen Harlinn 6-Feb-12 8:47am    
5'ed!
Abhinav S 6-Feb-12 10:35am    
Right. Thank you.
C#
Response.Clear();

            Response.Buffer = true;

            Response.AddHeader("content-disposition",

             "attachment;filename=GridViewExport.csv");

            Response.Charset = "";

            Response.ContentType = "application/text";



            GridView1.AllowPaging = false;

            GridView1.DataBind();



            StringBuilder sb = new StringBuilder();

            for (int k = 0; k < GridView1.Columns.Count; k++)
            {

                //add separator

                sb.Append(GridView1.Columns[k].HeaderText + ',');

            }

            //append new line

            sb.Append("\r\n");

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

                for (int k = 0; k < GridView1.Columns.Count; k++)
                {

                    //add separator

                    sb.Append(GridView1.Rows[i].Cells[k].Text + ',');

                }

                //append new line

                sb.Append("\r\n");

            }

            Response.Output.Write(sb.ToString());

            Response.Flush();

            Response.End();



and dont forget to add these namespace--
C#
using System.IO;
using System.Text;
 
Share this answer
 
v2
Comments
uspatel 6-Feb-12 2:19am    
[EDIT] Code tag added.....
http://vb.net-informations.com/datagridview/vb.net_datagridview_export.htm[^]

Use Above link for export the data to excel.....

http://www.developerfusion.com/tools/convert/vb-to-csharp/[^]

Use above link for VB to C# conversion...:)
 
Share this answer
 
Use Datatable to excell as

C#
private void ExportDataTable(DataTable dt)
       {
           string attachment = "attachment; filename=a.xls";
           HttpContext.Current.Response.Clear();
           HttpContext.Current.Response.AddHeader("content-disposition", attachment);
           HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
           string sTab = "";
           foreach (DataColumn dc in dt.Columns)
           {
               HttpContext.Current.Response.Write(sTab + dc.ColumnName);
               sTab = "\t";
           }
           HttpContext.Current.Response.Write("\n");

           int i;
           foreach (DataRow dr in dt.Rows)
           {
               sTab = "";
               for (i = 0; i < dt.Columns.Count; i++)
               {
                   HttpContext.Current.Response.Write(sTab + dr[i].ToString());
                   sTab = "\t";
               }
               HttpContext.Current.Response.Write("\n");
           }
           HttpContext.Current.Response.End();
       }
 
Share this answer
 
Hi,

Look my answer here.
 
Share this answer
 
You can try this tool
Export to Excel
 
Share this answer
 
Hi , Use below article, it is written by me and i have used this. It will provide formatted excel as user defined format.


Export to Excel from GridView in C#[^]

For any help, you can write to me. Don't Hesitate :-D
 
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