Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Export to Excel sheet from Datagrid

Export to Excel sheet from Datagrid

Scheduled Pinned Locked Moved ASP.NET
htmlhelptutorialquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Harini N K
    wrote on last edited by
    #1

    Need help on exporting to excel from datagrid. (I dont want third party tool). I tried to render the data to HTML as given below:

    //first let's clean up the response.object
    response.Clear();
    response.ClearContent();
    response.ClearHeaders();
    response.Buffer = false;

    //set the response mime type for excel
    response.ContentType = "application/ms-excel";

    response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.xls", StrFileName));

    //create a string writer
    StringWriter stringWrite = new StringWriter();

    //create an htmltextwriter which uses the stringwriter
    HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

    //instantiate a datagrid
    DataGrid dg = new DataGrid();

    //set the datagrid datasource to the dataset passed in
    dg.DataSource = dr;

    //bind the datagrid
    dg.DataBind();

    //tell the datagrid to render itself to our htmltextwriter
    dg.RenderControl(htmlWrite);

    //all that's left is to output the html
    response.Write(stringWrite.ToString());
    response.End();

    It works fine if i edit the file and save. I tried to create new worksheet in the same excel file and saved this changes, new temp folder has been created. (filename_files as like HTML files if you save any web page) Please let me know how to avoid this?

    Harini

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups