473,761 Members | 7,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Export datagrid to excel

Hi,

I copied the following codes from Internet to export datagrid to Excel, it
works with datagrid not supporting sorting or paging.

Response.Conten tType = "applicatio n/vnd.ms-excel"
Response.Charse t = ""
Response.AddHea der("Content-Disposition", "attachment;fil ename=Statement .xls")
Me.EnableViewSt ate = False

Dim tw As New System.IO.Strin gWriter
Dim hw As New System.Web.UI.H tmlTextWriter(t w)
DataGrid1.Rende rControl(hw)
Response.Write( "<table><tr><td ><b>" + lblMessage.Text +
"</b></td></tr><tr><td>" + tw.ToString() + "</td></tr></table>")
Response.End()

Looks like if datagrid supports sorting or paging, ASP.Net will render HTML
with Javascript inside, and the codes here do not like that. Is there a
workaround?

TIA
Nov 19 '05 #1
1 2136
You should disable datagrid paging when exporting to Excel. In that case
just write out all the records in one long page because this is how Excel
likes to deal with data. Excel doesn't do JavaScript.

Here's more info:
http://www.SteveOrr.net/articles/ExcelExport.aspx
http://www.SteveOrr.net/articles/ExportPanel.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"Ed Chiu" <Ed****@discuss ions.microsoft. com> wrote in message
news:65******** *************** ***********@mic rosoft.com...
Hi,

I copied the following codes from Internet to export datagrid to Excel, it
works with datagrid not supporting sorting or paging.

Response.Conten tType = "applicatio n/vnd.ms-excel"
Response.Charse t = ""
Response.AddHea der("Content-Disposition",
"attachment;fil ename=Statement .xls")
Me.EnableViewSt ate = False

Dim tw As New System.IO.Strin gWriter
Dim hw As New System.Web.UI.H tmlTextWriter(t w)
DataGrid1.Rende rControl(hw)
Response.Write( "<table><tr><td ><b>" + lblMessage.Text +
"</b></td></tr><tr><td>" + tw.ToString() + "</td></tr></table>")
Response.End()

Looks like if datagrid supports sorting or paging, ASP.Net will render
HTML
with Javascript inside, and the codes here do not like that. Is there a
workaround?

TIA

Nov 19 '05 #2

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

Similar topics

6
5896
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
3
4353
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
2934
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
3730
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)
19
11232
by: cj2 | last post by:
#1 Is there a quick way to export a datatable to an excel file? Or delimited file? #2 Where is the appropriate Microsoft monitored group to ask about writing reports in SQL Reporting services or SQL Server Business Intelligence Development Studio or whatever it's called?
0
9345
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
10115
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
9957
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...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9775
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...
1
7332
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6609
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
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
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.