473,666 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

asp .net / sql database / datagrid / export to excel

Hi all,

I have been using the following code to export my datagrids to excel
for quite some time. Very simple and very effective.

<code>
Sub btnExcelExport_ Click ( s As Object, e As EventArgs )
Response.Clear( )
Response.AddHea der("content-disposition",
"attachment;fil ename=FileName. xls")
Response.Charse t = ""
Response.Cache. SetCacheability (HttpCacheabili ty.NoCache)
Response.Conten tType = "applicatio n/vnd.xls"
Dim stringWrite As System.IO.Strin gWriter = New
System.IO.Strin gWriter()
Dim htmlWrite As System.Web.UI.H tmlTextWriter = New
HtmlTextWriter( stringWrite)
dgrdComplexSear chHidden.Render Control(htmlWri te)
Response.Write( stringWrite.ToS tring())
Response.End()
End Sub
</code>

The problem is when a user tries to export a very large datagrid--in
this case a query that is returning approx 1600 rows-- the instant the
export button is pressed (no delay) I get:

<error>The page cannot be displayed
The page you are looking for is currently unavailable. The Web site
might be experiencing technical difficulties, or you may need to adjust
your browser settings. </error>

I have seen a reference or two to this online but no solution as of
yet. My server is 2003 advanced server / iis 6.0/ asp .net 1.1 / sql
server 2000. I appreciate any and all help. This is a big deal to the
bosses who are addicted to excel....

Nov 19 '05 #1
3 1662
There are a variety of ways to export ASP.NET content to Excel.
Perhaps one of these alternate techniques will work out better for you:

http://SteveOrr.net/articles/ExcelExport.aspx
http://SteveOrr.net/articles/ExportPanel.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
<ma********@gma il.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
Hi all,

I have been using the following code to export my datagrids to excel
for quite some time. Very simple and very effective.

<code>
Sub btnExcelExport_ Click ( s As Object, e As EventArgs )
Response.Clear( )
Response.AddHea der("content-disposition",
"attachment;fil ename=FileName. xls")
Response.Charse t = ""
Response.Cache. SetCacheability (HttpCacheabili ty.NoCache)
Response.Conten tType = "applicatio n/vnd.xls"
Dim stringWrite As System.IO.Strin gWriter = New
System.IO.Strin gWriter()
Dim htmlWrite As System.Web.UI.H tmlTextWriter = New
HtmlTextWriter( stringWrite)
dgrdComplexSear chHidden.Render Control(htmlWri te)
Response.Write( stringWrite.ToS tring())
Response.End()
End Sub
</code>

The problem is when a user tries to export a very large datagrid--in
this case a query that is returning approx 1600 rows-- the instant the
export button is pressed (no delay) I get:

<error>The page cannot be displayed
The page you are looking for is currently unavailable. The Web site
might be experiencing technical difficulties, or you may need to adjust
your browser settings. </error>

I have seen a reference or two to this online but no solution as of
yet. My server is 2003 advanced server / iis 6.0/ asp .net 1.1 / sql
server 2000. I appreciate any and all help. This is a big deal to the
bosses who are addicted to excel....

Nov 19 '05 #2
I've used a similar pattern to create an Excel export. Rather than writing
to the response I override the Render method of the page. In the button
event handler I set a boolean flag to indicate that it should render to
Excel. I have not had any issues with rendering this way. The error may be
caused by the Response.End(). Here's the code patten I use translated from
C# (forgive me if I've made any syntax errors).

Dim sendAsExcel As Boolean = False

Sub btnExcelExport_ Click ( s As Object, e As EventArgs )
sendAsExcel = True
End Sub

Sub Overrides Render(HtmlWrit er writer)
If sendAsExcel Then
Response.AddHea der("content-disposition","a ttachment;filen ame=FileName.xl s")
Response.Cache. SetCacheability (HttpCacheabili ty.NoCache)
Response.Conten tType = "applicatio n/vnd.xls"
dgrdComplexSear chHidden.Render Control(writer)
Else
MyBase.Render(w riter)
End If
End Sub

IHTH

Jon
<ma********@gma il.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
Hi all,

I have been using the following code to export my datagrids to excel
for quite some time. Very simple and very effective.

<code>
Sub btnExcelExport_ Click ( s As Object, e As EventArgs )
Response.Clear( )
Response.AddHea der("content-disposition",
"attachment;fil ename=FileName. xls")
Response.Charse t = ""
Response.Cache. SetCacheability (HttpCacheabili ty.NoCache)
Response.Conten tType = "applicatio n/vnd.xls"
Dim stringWrite As System.IO.Strin gWriter = New
System.IO.Strin gWriter()
Dim htmlWrite As System.Web.UI.H tmlTextWriter = New
HtmlTextWriter( stringWrite)
dgrdComplexSear chHidden.Render Control(htmlWri te)
Response.Write( stringWrite.ToS tring())
Response.End()
End Sub
</code>

The problem is when a user tries to export a very large datagrid--in
this case a query that is returning approx 1600 rows-- the instant the
export button is pressed (no delay) I get:

<error>The page cannot be displayed
The page you are looking for is currently unavailable. The Web site
might be experiencing technical difficulties, or you may need to adjust
your browser settings. </error>

I have seen a reference or two to this online but no solution as of
yet. My server is 2003 advanced server / iis 6.0/ asp .net 1.1 / sql
server 2000. I appreciate any and all help. This is a big deal to the
bosses who are addicted to excel....

Nov 19 '05 #3
Thank you all. The answer was right under my nose and something
unrelated. I need to add this line to my web.config file.

<httpRuntime maxRequestLengt h = "20480"/>

The default was 4mb and I was surpassing it. The Excel code was fine.
Thanks again.

Nov 19 '05 #4

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

Similar topics

6
5887
by: Eric | last post by:
Dear All, I want to export datagrid content to a Excel file, the code just like below: Response.ContentType = "application/vnd.ms-excel" Response.Charset = "" Me.EnableViewState = False Dim tw As New System.IO.StringWriter() Dim hw As New System.Web.UI.HtmlTextWriter(tw) gridList.Columns(0).Visible = False
2
2469
by: Bidarkota | last post by:
Hi, I have a DataGrid in which there are some images and Data are displayed and in the webform.asp page i am using some stylesheets. when i export the datagrid all the images are also exported to datagrid and it is giving an alert that following stylesheets are missing when i export it. can anyone help me about this problem. I have seen the sites and articles regarding the datagrid export to excel. but still i am getting the same...
3
4346
by: Bidarkota | last post by:
When i export DataGrid to Excel all the HTML contents are also exporting to excel. i am using stylesheets in the ASPX Page and i am getting an alert message that stylesheets are missing. i need to export only the contents of the Datagrid to excel. i don't need all other stuff which is present on the page. is there any way to achieve it. Thanks in Advance
0
1734
by: sunilkumar Reddy via DotNetMonster.com | last post by:
hai all first we have taken binded grid Export to excel data from ultrawebgrid (or) datagrid rows, Each row data Expoting to one worksheet in excel workbook, like as grid row1 data contain sheet1 grid row2 data contain sheet2 grid row3 data contain sheet3 we have pressing one button ultrawebgrid (or) datagrid rows data to export
1
2130
by: Ed Chiu | last post by:
Hi, I copied the following codes from Internet to export datagrid to Excel, it works with datagrid not supporting sorting or paging. Response.ContentType = "application/vnd.ms-excel" Response.Charset = "" Response.AddHeader("Content-Disposition", "attachment;filename=Statement.xls") Me.EnableViewState = False
0
2918
by: kieran | last post by:
Hi, I have an 'export to excel' button on my Datagrid. All works well except that on my Datagrid, I have sub total rows. The subtotal rows use colspan so that the subtotals are given above certain columns When I export to excel, I do not have the colspan. As a result, the subtotals appear over the wrong columns in Excel.
6
3723
by: Sridhar | last post by:
Hi, I have to export data inside the datagrid into an excel worksheet. I have to do this on button click event. I am trying to do like this. First get the DataTable from the datagrid and iterate through each row to get the data. I am not able to get the DataTable from the datagrid. How could I do that? I tried like this but it is not working Dim dt as DataTable = CType(datagrid1.DataSource, DataTable)
4
3936
by: Frank | last post by:
Hello All, I ham using VS.NET 2003. Have followed instructions from http://gridviewguy.com/ArticleDetails.aspx?articleID=26 along with several other articles to no avail. I am pulling my hair out at this point. What I have is this : Members.aspx file:
6
2730
by: slinky | last post by:
I found the following code to transfer datagrid data to an Excel file. Is this written in C#?... I'm a vb.netter. I'm just not sure where to place the code to experiment on it. Should I place it in the event handler for a form button. Is there other ways to accomplish this? I looked at some on the web and usergroups, but was confused as to the functioning. BTW my datagrid is enclosed in a <div></divfor ease of scrolling if that would...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7385
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.