473,396 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Export to Excel. Browser setting?

I've used the following code countless times to successfully create an
excel export from a datagrid.

Protected Sub ExportToExcel(ByVal datagrid As Control)
'Create an MS Excel Response
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""

Dim oStringWriter As System.IO.StringWriter = New
System.IO.StringWriter
Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New
System.Web.UI.HtmlTextWriter(oStringWriter)

datagrid.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter.ToString)
Response.End()
End Sub

However, for some reason on one particular project I'm only getting
the first row of results
when the browser returns with the response.

If I comment out the line

Response.ContentType = "application/vnd.ms-excel"

I receive all rows. Is there a browser directive that I'm missing?

Bill E.
Hollywood, FL

Sep 7 '07 #1
2 1602
Hello,

I have seen a sample on the web and the code used it's a bit different,
try the sample, it should work fine:

http://www.dotnetjohn.com/articles.aspx?articleid=231

protected void btnExport_Click ( object sender, EventArgs e )

{

Response.Clear ( );

Response.AddHeader ( "content-disposition",
"attachment;filename=FileName.xls" );

Response.Charset = "";

Response.ContentType = "application/vnd.xls";

StringWriter StringWriter = new System.IO.StringWriter ( );

HtmlTextWriter HtmlTextWriter = new HtmlTextWriter ( StringWriter );

gvToExport.RenderControl ( HtmlTextWriter );

Response.Write ( StringWriter.ToString ( ) );

Response.End ( );

}

To check for more resources about generating Excel from ASP .net:

http://www.tipsdotnet.com/ArticleBlo...el&PageIndex=0


/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------


"Bill E." wrote:
I've used the following code countless times to successfully create an
excel export from a datagrid.

Protected Sub ExportToExcel(ByVal datagrid As Control)
'Create an MS Excel Response
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""

Dim oStringWriter As System.IO.StringWriter = New
System.IO.StringWriter
Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New
System.Web.UI.HtmlTextWriter(oStringWriter)

datagrid.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter.ToString)
Response.End()
End Sub

However, for some reason on one particular project I'm only getting
the first row of results
when the browser returns with the response.

If I comment out the line

Response.ContentType = "application/vnd.ms-excel"

I receive all rows. Is there a browser directive that I'm missing?

Bill E.
Hollywood, FL

Sep 7 '07 #2
Braulio,

Thanks for the reply. There really is no difference between the code
you posted and what I posted except the content type, which I don't
believe is correct in your case.

I discovered that the Response.End statement was the culprit. For
whatever reason, the response was getting truncated by this, and only
when the content type was Excel. I'll have to look at it in more
depth because this has not happened before to me.

Bill

Sep 7 '07 #3

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

Similar topics

1
by: frankvfox | last post by:
I need a code sample using VB ASP.Net 2003 which exports a tab delimited text file resident on the server into an Excel spreadsheet which will be saved through the browser to the client's local...
1
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....
1
by: Kevin Blakeley | last post by:
I know this was just posted but I did not want this message to get lost in the other thread as it's slightly different. Yes I want to export my dataset to excel for my clients, but I don't want...
2
by: C | last post by:
Hi, I have a dataset which I bind to a datagrid on my ASPX. I then export the datagrid to Excel and stream the file to the user by setting the MIME Type etc etc. Response.ContentType =...
3
by: Marja Ribbers-de Vroed | last post by:
Hi, One of my clients has a strange problem with my webapplication which I think may be related to some firewall setting on his computer. On several screens in the application, it is possible...
6
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
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"...
2
by: mike | last post by:
Hi, I have surfed through several posting about exporting gridview to excel and it works great. using the following code... response.Clear() response.ContentType = "application/vnd.ms-excel"...
1
by: Randy | last post by:
I have an application with a datagrid and a button to export the grid to excel. For the most part this is working fine. Here's the export method: Private Sub btnExcel_Click(ByVal sender As...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
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
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,...

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.