Exporting datagrid to Excel

Discussion in 'ASP .Net' started by Alex A., Nov 30, 2005.

  1. Alex A.

    Alex A. Guest

    I got some code off of dotnetjunkies.com to export a datagrid to excel.


    Everything works fine when I test it on my localhost, but when I move
    it to a server it doesn't do anything (no errors, just a plain
    postback).

    What am I missing?
    Is it a security issue?

    Any help appreciated and here the code:

    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.ContentType =
    "application/vnd.ms-excel"

    HttpContext.Current.Response.AddHeader("content-disposition",
    "attachment;filename=" & saveAsFile & ".xls")
    ' Remove the charset from the Content-Type header.
    HttpContext.Current.Response.Charset = ""
    'HttpContext.Current.Response.WriteFile("style.txt")
    ' Turn off the view state.
    grid.EnableViewState = False
    Dim tw As New System.IO.StringWriter
    Dim hw As New System.Web.UI.HtmlTextWriter(tw)

    grid.RenderControl(hw)
    ' Write the HTML back to the browser.
    HttpContext.Current.Response.Write(tw.ToString())
    ' End the response.
    HttpContext.Current.Response.End()
     
    Alex A., Nov 30, 2005
    #1
    1. Advertisements

  2. Also do a clearheaders() and ClearContents() if you have authentication.
    Debug/step?
     
    Edwin Knoppert, Nov 30, 2005
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.