zimmer9
asked on
Using ASP.NET with C# and VS2010, how would you write code on a button click to export over 5,000 records to an Excel 2003 file using Web forms?
I am writing my first ASP.NET application using C# with VS2010 and Web forms.
What code would you write for a button click event to transfer the records from either a Stored Procdure or a GridView to export over 5,000 rows to an Excel 2003 file.
For a field record layout example:
SELECT transferdate, processdate, Bank, Type, Office, CheckNo, RefNo, Description
export to file Clients.xls
What code would you write for a button click event to transfer the records from either a Stored Procdure or a GridView to export over 5,000 rows to an Excel 2003 file.
For a field record layout example:
SELECT transferdate, processdate, Bank, Type, Office, CheckNo, RefNo, Description
export to file Clients.xls
Chinmay's first link looks very good but the mime types are wrong. You should update this line:
Response.ContentType = "application/ms-excel";
...to the correct mime type:Response.ContentType = "application/vnd.ms-excel" // classic Excel .xls
-- OR --
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" // modern Excel .xlsx
@Angel:
application/ms-excel should work just fine give it a try.
application/ms-excel should work just fine give it a try.
Yes it may well work but it's wrong. If you use the correct mime type you can have more confidence it will continue to work in the future.
http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/05/08/office-2007-open-xml-mime-types.aspx
http://en.wikipedia.org/wiki/Microsoft_Excel#File_formats
http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/05/08/office-2007-open-xml-mime-types.aspx
http://en.wikipedia.org/wiki/Microsoft_Excel#File_formats
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I had used the following code in the past as you suggested.
The problem is that when I export 5,000 rows of data, I get the error message:
Problems duing load
Problems came up in the following areas during load:
cell data too large
-------------------------- ---------- ---------- ---------- ---------- ---
protected void Button1_Click1(object sender, EventArgs e)
{
string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();
Response.AddHeader("conten et-disposi tion", attachment);
Response.ContentType = "application/vnd.ms-excel" ;
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWrite r);
GridView1.RenderControl(ht mlTextWrit er);
Response.Write(stringWrite r.ToString ());
Response.End();
}
public override void VerifyRenderingInServerFor m(Control control)
{
}
The problem is that when I export 5,000 rows of data, I get the error message:
Problems duing load
Problems came up in the following areas during load:
cell data too large
--------------------------
protected void Button1_Click1(object sender, EventArgs e)
{
string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();
Response.AddHeader("conten
Response.ContentType = "application/vnd.ms-excel"
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWrite
GridView1.RenderControl(ht
Response.Write(stringWrite
Response.End();
}
public override void VerifyRenderingInServerFor
{
}
ASKER
I'm only exporting 5 fields of data per row as follow:
-------------------------- ---------- ---------- ---------
TransDate file type datetime
ProcessDate file type datetime
Bank nvarchar(255)
Type nvarchar(255)
Office nvarchar(255)
--------------------------
TransDate file type datetime
ProcessDate file type datetime
Bank nvarchar(255)
Type nvarchar(255)
Office nvarchar(255)
Try this
Response.ContentType = "application/vnd.xls";
ASKER
Response.ContentType = "application/vnd.xls";
-------------------------- ---------- ---------- ---------- --------
This revised code results in the following error:
The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
The operation completed successfully. Error processing resource 'http://localhost:4380/default.aspx'. Line 8, Position 92
<td>9/26/2013 12:00:00 AM</td><td>9/26/2013 12:00:00 AM</td><td>0184</td><td>A& ...
--------------------------
This revised code results in the following error:
The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.
--------------------------
The operation completed successfully. Error processing resource 'http://localhost:4380/default.aspx'. Line 8, Position 92
<td>9/26/2013 12:00:00 AM</td><td>9/26/2013 12:00:00 AM</td><td>0184</td><td>A&
Alright try this:
Source:
http://www.experts-exchang e.com/Prog ramming/La nguages/.N ET/ASP.NET /Q_2404944 4.html
Response.ContentType = "application/vnd.xls";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Source:
http://www.experts-exchang
ASKER
string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();
Response.AddHeader("conten et-disposi tion", attachment);
Response.ContentType = "application/vnd.xls";
Response.ContentEncoding = System.Text.Encoding.UTF8;
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWrite r);
GridView1.RenderControl(ht mlTextWrit er);
Response.Write(stringWrite r.ToString ());
Response.End();
-------------------------- ---------- --------
The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
The operation completed successfully. Error processing resource 'http://localhost:4380/default.aspx'. Line 8, Position 92
<td>9/26/2013 12:00:00 AM</td><td>9/26/2013 12:00:00 AM</td><td>0184</td><td>A& ...
Response.ClearContent();
Response.AddHeader("conten
Response.ContentType = "application/vnd.xls";
Response.ContentEncoding = System.Text.Encoding.UTF8;
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWrite
GridView1.RenderControl(ht
Response.Write(stringWrite
Response.End();
--------------------------
The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.
--------------------------
The operation completed successfully. Error processing resource 'http://localhost:4380/default.aspx'. Line 8, Position 92
<td>9/26/2013 12:00:00 AM</td><td>9/26/2013 12:00:00 AM</td><td>0184</td><td>A&
Can you send a screenshot of the error please. Thanks.
Hi zimmer9,
Was it a correct answer? If yes then it is ok I got bit confused as you are using C# code and that sample is in VB.
Regards,
Chinmay.
Was it a correct answer? If yes then it is ok I got bit confused as you are using C# code and that sample is in VB.
Regards,
Chinmay.
ASKER
Thanks for asking. Yes the answer I chose works for my application.
A very detailed example is listed here:
http://www.c-sharpcorner.com/UploadFile/DipalChoksi/exportxl_asp2_dc11032006003657AM/exportxl_asp2_dc.aspx
and if you are looking for a downloadable sample:
http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html