473,387 Members | 1,899 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,387 software developers and data experts.

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_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles but_export_1.Click
Response.Clear()
Response.AddHeader("content-disposition",
"attachment;filename=FileName.xls")
Response.Charset = ""
'If you want the option to open the Excel file without saving than
'comment out the line below
'Response.Cache.SetCacheability(HttpCacheability.N oCache);

Response.ContentType = "application/vnd.xls"
Dim strw As New System.IO.StringWriter
Dim htmlw As New System.Web.UI.HtmlTextWriter(strw)
gvResult1.RenderControl(htmlw)
Response.Write(strw.ToString())
Response.End()
End Sub

Public Overrides Sub VerifyRenderingInServerForm(ByVal gvResult1 As
Control)
End Sub
==========================================
Nov 19 '05 #1
3 4077
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.Charset = ""
'set the response mime type for excel
response.ContentType = "application/vnd.ms-excel"
'create a string writer
Dim stringWrite As New System.IO.StringWriter
'create an htmltextwriter which uses the stringwriter
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
'instantiate a datagrid
Dim dg As New System.Web.UI.WebControls.DataGrid
'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.HorizontalAlign = HorizontalAlign.Left
dg.ItemStyle.Height = 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.RenderControl(htmlWrite)
'all that's left is to output the html
response.Write(stringWrite.ToString)
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.

"wizard04frms" 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.Charset = ""
'set the response mime type for excel
response.ContentType = "application/vnd.ms-excel"
'create a string writer
Dim stringWrite As New System.IO.StringWriter
'create an htmltextwriter which uses the stringwriter
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
'instantiate a datagrid
Dim dg As New System.Web.UI.WebControls.DataGrid
'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.HorizontalAlign = HorizontalAlign.Left
dg.ItemStyle.Height = 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.RenderControl(htmlWrite)
'all that's left is to output the html
response.Write(stringWrite.ToString)
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.OleDb.OleDbConnection(strConn)
Dim cmd As New System.Data.OleDb.OleDbCommand(sql, myConnection)
Dim da As New System.Data.OleDb.OleDbDataAdapter(cmd)
'instantiate a dataset
Dim ds As New System.Data.DataSet

Try
'populate the dataset
da.Fill(ds)
Finally
'check on connection status
If myConnection.State = System.Data.ConnectionState.Open Then
myConnection.Close()
End If
'get rid of connection object
myConnection.Dispose()
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
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...
4
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...
5
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...
3
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...
0
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",...
7
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...
7
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...
4
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...
1
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.