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 dataset to Excel

Export dataset to Excel

Scheduled Pinned Locked Moved ASP.NET
helpquestion
3 Posts 2 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.
  • C Offline
    C Offline
    countmein
    wrote on last edited by
    #1

    Hi, When I tried to implement the code from this forum to display data in the excel from a webpage, I am getting weird results. I am wondering if you could direct me to correct the problem? I thank you for your help. The code is : HttpResponse response = HttpContext.Current.Response; response.Clear(); response.Charset = ""; //MAKE THE CONTENT TYPE TO EXCEL FORMAT. response.ContentType = "application/vnd.ms-excel"; response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + ".xls\""); // CREATE A STRING WRITER. using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { // INSTANTIATE A DATAGRID. GridView dg = new GridView(); dg.DataSource = ds.Tables[0]; dg.DataBind(); dg.RenderControl(htw); //STYLE TO FORMAT NUMBERS TO STRING. string style1 = @" .textmode { mso-number-format:\@; } "; response.Write(sw.ToString()); response.Write(style1); //response.Flush(); HttpContext.Current.ApplicationInstance.CompleteRequest(); } } } catch (Exception ex) { throw ex; } } ds:is the dataset and filename is the name of the filename. However, the file contains data as below: " " MonthYearUSERIDApplicationNumOfReturns" " 08/2011userid12010" " 08/2011userid22010" " 08/2011userid32009" " 08/2011userid42010" " " .textmode { mso-number-format:\@; } " Untitled Page"

    I am wondering what wrong with my code. Any help is greatly appreciated. Thank you.

    U 1 Reply Last reply
    0
    • C countmein

      Hi, When I tried to implement the code from this forum to display data in the excel from a webpage, I am getting weird results. I am wondering if you could direct me to correct the problem? I thank you for your help. The code is : HttpResponse response = HttpContext.Current.Response; response.Clear(); response.Charset = ""; //MAKE THE CONTENT TYPE TO EXCEL FORMAT. response.ContentType = "application/vnd.ms-excel"; response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + ".xls\""); // CREATE A STRING WRITER. using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { // INSTANTIATE A DATAGRID. GridView dg = new GridView(); dg.DataSource = ds.Tables[0]; dg.DataBind(); dg.RenderControl(htw); //STYLE TO FORMAT NUMBERS TO STRING. string style1 = @" .textmode { mso-number-format:\@; } "; response.Write(sw.ToString()); response.Write(style1); //response.Flush(); HttpContext.Current.ApplicationInstance.CompleteRequest(); } } } catch (Exception ex) { throw ex; } } ds:is the dataset and filename is the name of the filename. However, the file contains data as below: " " MonthYearUSERIDApplicationNumOfReturns" " 08/2011userid12010" " 08/2011userid22010" " 08/2011userid32009" " 08/2011userid42010" " " .textmode { mso-number-format:\@; } " Untitled Page"

      I am wondering what wrong with my code. Any help is greatly appreciated. Thank you.

      U Offline
      U Offline
      uspatel
      wrote on last edited by
      #2

      This dll can help you with good designing Export to Excel

      C 1 Reply Last reply
      0
      • U uspatel

        This dll can help you with good designing Export to Excel

        C Offline
        C Offline
        countmein
        wrote on last edited by
        #3

        Thank you.

        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