Click here to Skip to main content
14,699,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi...i have table contains 10 columns...but i bound with grid view with only 4 columns...now i need to export data from sqlserver to excel sheet when clicking import linkbutton in gridview...can anybody help me???
Posted
Comments
Sandeep Mewara 30-May-11 5:30am
   
So what is the issue. Sounds straight forward. Where are you stuck? You want to export grid data into excel, do it.

1 solution

Hi,

on Button Click Event try this code it will work

sql = "SELECT EmpCode as [Code], FullName as [Employee], Designation where emp_id="+ EMP_ID +"";
    dt = new DataTable();
    dt = cCommon.FillDataTable(sql);
if (dt.Rows.Count > 0)
{
    Response.Clear();
    Response.ContentType = "application/vnd.ms-excel";
    string sep = "";
    foreach (DataColumn dc in dt.Columns)
    {
        Response.Write(sep + dc.ColumnName);
        sep = "\t";
    }
    Response.Write("\n");
    int i;
    foreach (DataRow dr in dt.Rows)
    {
        sep = "";
        for (i = 0; i < dt.Columns.Count; i++)
        {
            if (i == 3)
            {
                string date = cDateFunction.ConvertToDate(Convert.ToInt32(dr[i].ToString()).ToString()).ToString();
                Response.Write(sep + date);
            }
            else
            {
                Response.Write(sep + dr[i].ToString());
            }
            sep = "\t";
        }
        Response.Write("\n");
    }
   

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