473,498 Members | 1,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Export to Excel code

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


Nov 18 '05 #1
2 4048
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

<me@privacy.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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



Nov 18 '05 #2
<<
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


Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5004
by: Matt | last post by:
I have an ASP page that calls ASP routines that I created that execute a database query and return the results to a recordset. I then iterate through the recordset and display the data in a table....
6
13098
by: Robin Cushman | last post by:
Hi all, I need some help -- I'm working with an A2K database, using DAO, and am trying to read records into a Crystal Report and then export it to a folder on our network as an Excel...
5
3743
by: Maria L. | last post by:
Hi, I need to export the content of a DataGrid (in Windows application in C#), into an Excel spreadsheet. Anyone knows how to do this? Any code snippets would help! thanks a lot, Maria
2
3970
by: Siu | last post by:
Hi, I use the following code to export and import a file Excel from resp. into a Web page with the following code: //EXPORT Response.Clear(); Response.Buffer = true; Response.ContentType =...
6
4042
by: Elena | last post by:
I'm trying to export data to an Excel worksheet. I can export the data in the cell values perfectly. I need the code to change a header and footer for the worksheet, not for the columns. Is...
13
13179
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"...
5
31883
by: Simon | last post by:
Dear reader, With the export command you can export a query to Excel. By activate this command a form pop's up with the following text:
1
9753
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
1
10464
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
2
6389
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the...
0
6998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5460
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4586
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.