Export to Excel code

Discussion in 'ASP .Net Web Controls' started by Guest, Mar 3, 2004.

  1. Guest

    Guest Guest

    I have a web page with a populated datagrid on it, that has sort functions
    on several of the columns.

    I have a routine that runs immediately before the code below that clears the
    controls.

    The code below is then supposed to execute:

    Dim tw As New System.IO.StringWriter()

    Dim hw As New System.Web.UI.HtmlTextWriter(tw)

    DataGrid1.RenderControl(hw)

    Response.AppendHeader("content-disposition", "attachment; filename=" & sFile
    & ".xls")

    ' Remove the charset from the Content-Type header.

    'Response.Charset = ""

    ' Turn off the view state.

    'Me.EnableViewState = False

    ' Write the HTML back to the browser.

    Response.Write(tw.ToString())

    ' End the response.

    Response.End()



    What I get is a zero byte file that I am prompted to either open or save.
    Either action will result in an "Unable to read file" message.

    I've tweaked the other commands (response.charset, and viewstate) on or off,
    and it hasn't made any difference. This is giving me the same error message
    when I just have a response.contenttype command in the stream without doing
    the rendercontrol. I can't read that file either. I've shuffled these
    commands around too, having the rendercontrol immediately before the
    response.write command.

    Does anyone have any idea what's going on with this code? Or do you have an
    example of some code that DOES work?

    I am lost on this.

    SC
    Guest, Mar 3, 2004
    #1
    1. Advertising

  2. There are quite a few articles on doing this, so maybe one of them will get
    you going?

    http://www.c-sharpcorner.com/Code/2003/Sept/ExportASPNetDataGridToExcel.asp

    http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309so_l/asp200309so_l.asp

    http://weblogs.asp.net/dneimke/archive/2004/01/27/63348.aspx

    http://www.mikepope.com/blog/AddComment.aspx?blogid=344

    <> wrote in message
    news:%...
    >I have a web page with a populated datagrid on it, that has sort functions
    > on several of the columns.
    >
    > I have a routine that runs immediately before the code below that clears
    > the
    > controls.
    >
    > The code below is then supposed to execute:
    >
    > Dim tw As New System.IO.StringWriter()
    >
    > Dim hw As New System.Web.UI.HtmlTextWriter(tw)
    >
    > DataGrid1.RenderControl(hw)
    >
    > Response.AppendHeader("content-disposition", "attachment; filename=" &
    > sFile
    > & ".xls")
    >
    > ' Remove the charset from the Content-Type header.
    >
    > 'Response.Charset = ""
    >
    > ' Turn off the view state.
    >
    > 'Me.EnableViewState = False
    >
    > ' Write the HTML back to the browser.
    >
    > Response.Write(tw.ToString())
    >
    > ' End the response.
    >
    > Response.End()
    >
    >
    >
    > What I get is a zero byte file that I am prompted to either open or save.
    > Either action will result in an "Unable to read file" message.
    >
    > I've tweaked the other commands (response.charset, and viewstate) on or
    > off,
    > and it hasn't made any difference. This is giving me the same error
    > message
    > when I just have a response.contenttype command in the stream without
    > doing
    > the rendercontrol. I can't read that file either. I've shuffled these
    > commands around too, having the rendercontrol immediately before the
    > response.write command.
    >
    > Does anyone have any idea what's going on with this code? Or do you have
    > an
    > example of some code that DOES work?
    >
    > I am lost on this.
    >
    > SC
    >
    >
    >
    >
    >
    >
    Ken Cox [Microsoft MVP], Mar 4, 2004
    #2
    1. Advertising

  3. Guest

    Guest Guest

    <<
    http://www.c-sharpcorner.com/Code/2003/Sept/ExportASPNetDataGridToExcel.asp

    http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309so_l/asp200309so_l.asp
    http://weblogs.asp.net/dneimke/archive/2004/01/27/63348.aspx
    http://www.mikepope.com/blog/AddComment.aspx?blogid=344>>

    Ken,

    I tried the one at the
    weblogs.asp.net/dneimke/archive/2004/01/27/63348.aspx, which seems to be
    generally like what I've got.

    However, when I run it I still get a zero byte file that gives me a "Unable
    to read file" message in Excel when I attempt to open it.

    In stepping thru the code, which is EXACTLY like the one in the example,
    with the exception that mine is not a private sub rendergrid that gets
    called from a page load event, but rather the object of a
    button_selectedindexchanged (a radio button) I have on the screen to choose
    whether it's web (default) or Excel output.

    After I step through the code, when the datagrid1.rendercontrol(hw) command
    has executed, I do a quickwatch on it, and get this in the value field:

    RenderControl Argument not specified for parameter 'writer' of 'Public Sub
    RenderControl(writer As System.Web.UI.HtmlTextWriter)'.

    A quickwatch on the next command, the response.write (tw.tostring()) is:
    NewLine "" String

    Also, the way I am doing my routine is in a couple of subs within my page.
    I'm not calling either of the bindgrid or render controls in the pageload
    event.

    Any ideas?

    SC
    Guest, Mar 4, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGF2aWQgVmFsbGU=?=

    Invalid export DLL or export format

    =?Utf-8?B?RGF2aWQgVmFsbGU=?=, Oct 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    3,617
    =?Utf-8?B?RGF2aWQgVmFsbGU=?=
    Oct 29, 2003
  2. =?Utf-8?B?U3JpZGhhcg==?=

    using Microsoft Excel image for Export to Excel button

    =?Utf-8?B?U3JpZGhhcg==?=, Dec 9, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    3,103
    =?Utf-8?B?U3JpZGhhcg==?=
    Dec 9, 2005
  3. =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?=

    Export to Excel (Default File Type - Excel)

    =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?=, Apr 24, 2006, in forum: ASP .Net
    Replies:
    15
    Views:
    17,518
    syed hassan
    May 21, 2009
  4. Grey
    Replies:
    4
    Views:
    1,971
    Mark Rae [MVP]
    Oct 17, 2007
  5. Maarten Porters
    Replies:
    1
    Views:
    429
    Florian Gilcher
    Jul 28, 2008
Loading...

Share This Page