1

export data grid view to excel code is run and all data export into excel sheet but i want to only selected column in excel sheet how to solve this problem

    protected void btnexcel_Click1(object sender, EventArgs e)
   {

    Response.Clear();
    Response.Buffer = true;

    Response.AddHeader("content-disposition",
    "attachment;filename=ActualsAndBudgets.xls");
    Response.Charset = "";
    Response.ContentType = "application/ms-excel";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    gvdetails.AllowPaging = false;
    fillgrid();
    gvdetails.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
    gvdetails.AllowPaging = true;
    fillgrid();

  }
   public override void VerifyRenderingInServerForm(Control control)
  {

  }
2
  • Remember to pass a datatable containing your data to excel_Click() Commented Apr 11, 2013 at 10:46
  • What are you trying to do exactly? Commented Apr 11, 2013 at 10:51

1 Answer 1

1
    using System;
    using System.Data;
     using System.IO;
     using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;

       namespace Whatever
       {
        /// 

        /// This class provides a method to write a dataset to the HttpResponse as
          /// an excel file. 
         /// 

            public class ExcelExport
       {
          public static void ExportDataSetToExcel(DataSet ds, string filename)
       {
        HttpResponse response = HttpContext.Current.Response;

   // first let's clean up the response.object
      response.Clear();
       response.Charset = "";

        // set the response mime type for excel
          response.ContentType = "application/vnd.ms-excel";
         response.AddHeader("Content-Disposition", "attachment;
           filename=\"" + filename       + "\"");

         // create a string writer
       using (StringWriter sw = new StringWriter())
      {
       using (HtmlTextWriter htw = new HtmlTextWriter(sw))
        {
       // instantiate a datagrid
        DataGrid dg = new DataGrid();
      dg.DataSource = ds.Tables[0];
        dg.DataBind();
       dg.RenderControl(htw);
        response.Write(sw.ToString());
      response.End(); 
    }
  }
  }
   }
     }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.