Export to Excel code

G

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
 
G

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top