473,785 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Blank Spreadsheet when Gridview Exported to Excel

Hello all,

I am doing my web-based reporting tool in ASP.Net 2.0 using Visual Web
Developer 2005.

I try to export my populated gridview to Excel spreadsheet but what I got is
just a blank spreadsheet with only <div></div> tag in the first cell.

Can anyone tell me what actually is the problem? Thanks in advance.

Below are the codes in vb:

=============== ===============
Protected Sub but_export_1_Cl ick(ByVal sender As Object, ByVal e As
System.EventArg s) Handles but_export_1.Cl ick
Response.Clear( )
Response.AddHea der("content-disposition",
"attachment;fil ename=FileName. xls")
Response.Charse t = ""
'If you want the option to open the Excel file without saving than
'comment out the line below
'Response.Cache .SetCacheabilit y(HttpCacheabil ity.NoCache);

Response.Conten tType = "applicatio n/vnd.xls"
Dim strw As New System.IO.Strin gWriter
Dim htmlw As New System.Web.UI.H tmlTextWriter(s trw)
gvResult1.Rende rControl(htmlw)
Response.Write( strw.ToString() )
Response.End()
End Sub

Public Overrides Sub VerifyRendering InServerForm(By Val gvResult1 As
Control)
End Sub
=============== =============== ============
Nov 19 '05 #1
3 4106
You have to make sure the data is bound to the control again before
using RenderControl since the click event is done on the server. I had
problems with this scenario myself, and ended up making the button
redirect to a second page. On that second page, in Page_Load, I put
the code you have above plus some code to bind data to the datagrid.
Mine looks like this:

Sub Page_Load()

'... code to fill dataset ds

'first let's clean up the response.object
response.Clear( )
response.Charse t = ""
'set the response mime type for excel
response.Conten tType = "applicatio n/vnd.ms-excel"
'create a string writer
Dim stringWrite As New System.IO.Strin gWriter
'create an htmltextwriter which uses the stringwriter
Dim htmlWrite As New System.Web.UI.H tmlTextWriter(s tringWrite)
'instantiate a datagrid
Dim dg As New System.Web.UI.W ebControls.Data Grid
'set the datagrid datasource to the dataset ds
dg.DataSource = ds.Tables(0)

'last minute changes to formatting
dg.GridLines = GridLines.None
dg.HeaderStyle. Font.Bold = True
dg.HeaderStyle. HorizontalAlign = HorizontalAlign .Left
dg.ItemStyle.Ho rizontalAlign = HorizontalAlign .Left
dg.ItemStyle.He ight = new Unit(17) 'set height to 17 px (12.75 pt)

'bind the datagrid
dg.DataBind()
'tell the datagrid to render itself to our htmltextwriter
dg.RenderContro l(htmlWrite)
'all that's left is to output the html
response.Write( stringWrite.ToS tring)
response.End()

End Sub

Nov 19 '05 #2
Thanks for your reply.

I change a bit of my previous codes to save the gridview into session. When
the Export To Excel button event is triggered, it will retrieve the gridview
from the session. But that did not work out as well ...

I am using gridview bound with sql datasource control. I can't find any
dataset associated with my gridview. Do you mind tell me where can I locate
it?

Thanks again.

"wizard04fr ms" wrote:
You have to make sure the data is bound to the control again before
using RenderControl since the click event is done on the server. I had
problems with this scenario myself, and ended up making the button
redirect to a second page. On that second page, in Page_Load, I put
the code you have above plus some code to bind data to the datagrid.
Mine looks like this:

Sub Page_Load()

'... code to fill dataset ds

'first let's clean up the response.object
response.Clear( )
response.Charse t = ""
'set the response mime type for excel
response.Conten tType = "applicatio n/vnd.ms-excel"
'create a string writer
Dim stringWrite As New System.IO.Strin gWriter
'create an htmltextwriter which uses the stringwriter
Dim htmlWrite As New System.Web.UI.H tmlTextWriter(s tringWrite)
'instantiate a datagrid
Dim dg As New System.Web.UI.W ebControls.Data Grid
'set the datagrid datasource to the dataset ds
dg.DataSource = ds.Tables(0)

'last minute changes to formatting
dg.GridLines = GridLines.None
dg.HeaderStyle. Font.Bold = True
dg.HeaderStyle. HorizontalAlign = HorizontalAlign .Left
dg.ItemStyle.Ho rizontalAlign = HorizontalAlign .Left
dg.ItemStyle.He ight = new Unit(17) 'set height to 17 px (12.75 pt)

'bind the datagrid
dg.DataBind()
'tell the datagrid to render itself to our htmltextwriter
dg.RenderContro l(htmlWrite)
'all that's left is to output the html
response.Write( stringWrite.ToS tring)
response.End()

End Sub

Nov 19 '05 #3
Well, I haven't actually used the new GridView control. I found a
webpage that may help you out:

http://msdn.microsoft.com/asp.net/de...idViewEx02.asp

With my DataGrid and my Access datasource, I did the following to
populate a dataset:

'sql is the query string
'strConn is the connection string

Dim myConnection As New System.Data.Ole Db.OleDbConnect ion(strConn)
Dim cmd As New System.Data.Ole Db.OleDbCommand (sql, myConnection)
Dim da As New System.Data.Ole Db.OleDbDataAda pter(cmd)
'instantiate a dataset
Dim ds As New System.Data.Dat aSet

Try
'populate the dataset
da.Fill(ds)
Finally
'check on connection status
If myConnection.St ate = System.Data.Con nectionState.Op en Then
myConnection.Cl ose()
End If
'get rid of connection object
myConnection.Di spose()
End Try

'now the ds is populated

Nov 19 '05 #4

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

Similar topics

2
3477
by: Regnab | last post by:
I've got my code working so that it'll count the number of columns in the table and move across (eg Range A-P and then range Q-W). Problem is when I get to the end of the single letters and get into AA etc. Have got an idea of how I could do it by using Chr() and having an ongoing counter which is divided by 26 when it goes past Z to work out location. However, I'd dearly like there to be an easier way. Any ideas? Another problem I'm...
4
2308
by: washoetech | last post by:
Hello, I am working on a project where I need to be able to grab the data from an Excel spreadsheet and create a new table in my database based on the columns in the spreadsheet. After the table is created then I need to fill the table with the data from that spreadsheet. I am using ASP.NET 2.0 and SQL 2000 for my database. What would be the best way to do this? Should I go Excel to XML or Excel to a Dataset?
5
1833
by: Dave | last post by:
In a VB.NET application, I have a datagrid that I export to an Excel spreadsheet like this: ' Set the content type to Excel Response.ContentType = "application/vnd.ms-excel" Dim tw As New System.IO.StringWriter Dim hw As New System.Web.UI.HtmlTextWriter(tw) ' Get the HTML for the control.
3
11689
by: Scott M. Lyon | last post by:
I'm trying to figure out a way to export data (actually the result of a Stored Procedure call from SQL Server) into a specified Excel spreadsheet format. Currently, I have the data read into a DataSet that consists of one DataTable. That DataTable has a specific name (for arguments purposes, let's say it's called "DataGrid"), that I need to use as the tab name in the exported Excel
0
1357
by: Mike P | last post by:
I am using the following code to export my gridview to excel : protected void btnExport_Click(object sender, ImageClickEventArgs e) { Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=FileName.xls"); Response.Charset = "";
7
5770
by: semijoyful | last post by:
OS: Win XP SP2 Access version: 2003 Excel version: 2003 I am new at this, as I am sure you have gathered from this post title:) I am working on a form where users can input data in Access and at the end of the day be able to press a button on the form that would update that data to a single exel spreadsheet. The following is the code for the module. When I click on the button in the form, It takes me to VBS Debugger. I then type...
7
4954
by: tasmontique | last post by:
Hi All, I have finally succeeded in exporting to a preformated excel spreadsheet. I have one tiny setback. One of the sheets I am exporting to must be password protected. When I do this and export I am prompted for the password. This defeats the point.
4
1762
by: =?Utf-8?B?Q2FybGl0b3M=?= | last post by:
Hi there, I have a asp.net page that dynamically constructs a table based on data retrieved from a database. This is an excerpt of the code used: ' Create a row Dim row As HtmlTableRow = New HtmlTableRow ' Create a column and add it to the row Dim cell As HtmlTableCell = New HtmlTableCell cell.InnerText = sValue
1
1420
by: dkriese | last post by:
I am using Quickbooks reporting tool that can export an xls or csv file. The data being exported needs to create a classroom scheduler. The data being exported is from a sales receipt via QB and contains date and time, etc. Question is how can I get that row of data imported into access to create a calendar type schedule for those days and for the month. I need to see all the students scheduled for the each of the dates, the class...
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
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,...
1
10097
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
8983
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
6742
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
5386
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...
1
4055
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 we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
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.