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

Export Gridview to Excel more than 65535 record

15
Thanks for your help. It really help to solve my work.
But now i have a bigger problem :
I had more than 65535 records in gridview divide by 10/pages. And when i tried to export the gridview in excel on every way :
1. I disabled the allowpaging in gridview and got and error messages like : System.OutOfMemoryException Was Thrown
2. My idea is to store row in gridview to virtable per pages. I create virtable as Table. Added row to virtable from gridview/page. But its only displayed the first page of gridview. Here is the code:
For i = 1 To grView.PageCount
grView.PageIndex = i
virTable.GridLines = grView.GridLines
If (Not (grView.HeaderRow) Is Nothing) Then
virTable.Rows.Add(grView.HeaderRow)
End If
For Each row As GridViewRow In grView.Rows
virTable.Rows.Add(row)
Next
If (Not (grView.FooterRow) Is Nothing) Then
virTable.Rows.Add(grView.FooterRow)
End If
Next
virTable.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter)
Response.End()
3. I tried like number 2 but i changed the position like :
For i = 1 To grView.PageCount
grView.PageIndex = i
virTable.GridLines = grView.GridLines
If (Not (grView.HeaderRow) Is Nothing) Then
virTable.Rows.Add(grView.HeaderRow)
End If
For Each row As GridViewRow In grView.Rows
virTable.Rows.Add(row)
Next
If (Not (grView.FooterRow) Is Nothing) Then
virTable.Rows.Add(grView.FooterRow)
End If
virTable.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter)
Next
Response.End()
But it gives an error message like number 1 : System.OutOfMemoryException Was Thrown
Oct 23 '08 #1
3 5180
-Number 1:
Exporting data from GridVview to excel, it should be done like this:

//export to excel
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(dg);
dg.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();

But in case you have Non-LiteralControl in girdview you may get run time exception, so let me know which one are you working now.


-Number 2: You got System.OutOfMemoryException. I guess you are calling Response.write() in loop for many time. Continate your output string(oStringWriter) then do Response.Write() outside loop.

Dane
Oct 23 '08 #2
ulai
15
Thx for your att,

Right now, i'm focusing 2

But in 2 i got no error message, cause i place the response.write outside looping. The problem in 2 is its only displayed the first page of gridview from 2400 pages.

In 3, i tried to fix the problem in 2. I placed the response.write inside the looping. And as you say its got an error : System.OutOfMemoryException

Here is the code in 2 :

Dim Str As String = "attachment; filename=ExcelFileName.xls"

If (File.Exists(srcPath)) Then
File.Delete(srcPath)
End If

Response.ClearContent()
Response.AddHeader("content-disposition", Str)
Response.ContentType = "application/ms-excel"

Dim oStringWriter As New StringWriter
Dim oHtmlTextWriter = New HtmlTextWriter(oStringWriter)

Dim virTable As Table = New Table
Dim i As Integer

For i = 1 To grView.PageCount
grView.PageIndex = i

virTable.Rows.Add(grView.HeaderRow)

For Each row As GridViewRow In grView.Rows
virTable.Rows.Add(row)
Next
Next

virTable.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter)
Response.End()


The problem is :
I assign the row on gridview (grView) at first page to last page To virTable. But when i debugged, i only got 10 rows in virTable (not accumulated from the page before).

So, if you have time, would you like to find the solution for me ?
Oct 23 '08 #3
You can not loop through all rows in gridview with paging enabled by just changing pageindex of gridview unless you rebind(DataBind()) your gridview though you will get only the current page.

Try following solutions:

-If you are using datatable as as datasource, try this:
//Suppose we binded DataTable to the gridview
DataTable dt = GridView1.DataSource as DataTable;
foreach (DataRow dr in dt.Rows)
{
//do adding row, virTable.Rows.Add(row)
}

-If you are using SqlDataSource control as a datasource , try this:

DataSourceSelectArguments dsaArgs = new DataSourceSelectArguments();
//SqlDataSource1 is a SqlDataSource control is used to bind to gridview
DataView view = (DataView)SqlDataSource1.Select(dsaArgs);
DataTable dt = view.ToTable();
foreach (DataRow dr in dt.Rows)
{
//do adding row, virTable.Rows.Add(row)
}

Dane,
Hey, Don't forget to go to my blog and give some comments.
http://savotdane.blogspot.com/
Oct 23 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Robin Cushman | last post by:
Hi all, I need some help -- I'm working with an A2K database, using DAO, and am trying to read records into a Crystal Report and then export it to a folder on our network as an Excel...
8
by: DC Gringo | last post by:
I have a simple button that should open another window and export a datagrid to an Excel file. I'm getting: "Name 'window' is not declared." What do I need to declare or import? <INPUT...
11
by: Not Me | last post by:
Hi, I'm trying to export from a gridview control, to an excel file using code intended for a datagrid control (it's all over the web, can post if requested) I get the error. Control...
3
by: =?Utf-8?B?bWFuaWthMDI=?= | last post by:
Hi, I have a GridView control in page called eventslisting which is inheriting from a MasterPage. The normal code to export to GridView does not work and gives me an error - "Control of type...
1
by: arnabit | last post by:
Hi, I have a problem with excel 2007. I have gridview where a report result is being shown when the report are exported it is saved in an excel file. In excel 2003 it is working fine .But in...
0
by: =?Utf-8?B?RGFpc3k=?= | last post by:
I have the following routine to export gridview data to excel. The excel file has an extra line added on the top. Is there anyway to get rid of it? The environment of dev box is WindowsXP, Msft...
0
by: Mike | last post by:
On my page I'm using an updatepanel, updateProgress, and a ModalProgress, everything works great But when I try to export my GridView to excel I'm getting the following error message: ...
0
by: mahesh123 | last post by:
Hi, I am dispalying data in the gridview.I want to export gridview data to excel sheet. Can any one help me how to write code in asp.net with VB. Thanks
11
by: ulai | last post by:
Hello everyone, I want to export GridView (25000 records) to Excel. But i got an error message like : System.OutOfMemoryException was Thrown. But i need to export the records (even more than...
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
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...
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,...
0
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...

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.