Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using below code to export datatable to excel and it just works fine if the rows count is less than 10k and it throws "404 - File or directory not found." error if records count exceeds 10k limit. Please let me know if any idea on how to solve this.


DataTable dt= GetDataFromDB();
HtmlForm form = new HtmlForm();
form.Attributes["runat"] = "server";
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Test.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
form.Controls.Add(dt);
this.Controls.Add(form);
form.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

Thanks in advance,

Vasanth

What I have tried:

I tried to export to .csv format also but same issue.
Posted
Updated 14-Apr-16 7:40am
Comments
Gautham Prabhu K 14-Apr-16 8:28am    
What is the file size in MB?
vasumagadi 15-Apr-16 7:31am    
it's 4MB

You are really not exporting anything as an Excel file. What you are doing is exporting an html with an "xls" extension that when opened in Excel would render as a native "xls" file.

What you should be doing is exporting to a native Excel document. There are several free and commercial libraries to help with this.

A couple a good free one are:

EPPlus - EPPlus-Create advanced Excel spreadsheets on the server - Home[^]
OpenXML SDK - Welcome to the Open XML SDK 2.5 for Office[^]

If you are able to look at commercial products I would recommend looking at Aspose. Great product that has been around for several year.

The one product to avoid is Office Interop. Even MS does not recommend using this in today environments.
 
Share this answer
 
The default size is 4MB. So we can increase the size (max limit) in the web.config
XML
<configuration>
  <system.web>
    <httpRuntime maxRequestLength="xxx" />
  </system.web>
</configuration>


Please follow the below link:
asp.net - Maximum request length exceeded[^]

I anticipate this would be the issue. Please check once.
Thanks
 
Share this answer
 
Comments
Richard Deeming 14-Apr-16 12:34pm    
How would changing the maximum request length affect a problem with the length of the response?
vasumagadi 15-Apr-16 1:52am    
Hi Passion,

i have increased the limit of maxrequestLength but no luck, same error is getting displayed. appreciate your help. Please let me know if any idea on this.

Thanks,

Vasanth

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900