Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Export to Excel code

Reply
Thread Tools

Export to Excel code

 
 
Guest
Posts: n/a
 
      03-03-2004
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






 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      03-04-2004
There are quite a few articles on doing this, so maybe one of them will get
you going?

http://www.c-sharpcorner.com/Code/20...ridToExcel.asp

http://www.aspnetpro.com/NewsletterA...200309so_l.asp

http://weblogs.asp.net/dneimke/archi.../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
>
>
>
>
>
>


 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      03-04-2004
<<
http://www.c-sharpcorner.com/Code/20...ridToExcel.asp

http://www.aspnetpro.com/NewsletterA...200309so_l.asp
http://weblogs.asp.net/dneimke/archi.../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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export to Excel (Default File Type - Excel) =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?= ASP .Net 15 05-21-2009 12:01 PM
Export to excel in asp.net using excel template Grey ASP .Net 4 10-17-2007 08:08 AM
using Microsoft Excel image for Export to Excel button =?Utf-8?B?U3JpZGhhcg==?= ASP .Net 0 12-09-2005 08:58 PM
Export to Excel code ASP .Net 2 03-04-2004 03:31 PM
Code the WorksheetOptions on export to Excel Matt ASP .Net 4 12-19-2003 12:35 PM



Advertisments