Reputation: 1
Can anybody help me I am a beginner and have no concept how the conversion can be made.
Here is my code:
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
Response.ContentType = "application/vnd.ms-excel";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Response.ContentEncoding = System.Text.Encoding.Unicode;
fuFile.RenderControl(hw);
Response.Output.Write(sw.ToString().Replace(" ", "").Replace("&", "&"));
Response.End();
Is this not enough to export an HTML file to Excel?
Upvotes: 0
Views: 15337
Reputation: 113
I have found this works:
String content = "<html><body><table><tr><td>your table</td></tr></table></body></html>";
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
Response.ContentType = "application/vnd.xls";
Response.Cache.SetCacheability(HttpCacheability.NoCache); // not necessarily required
Response.Charset = "";
Response.Output.Write(content);
Response.End();
As long as your content is well-formed HTML, this should work. Also note that comments, VBA code/macros, and multiple work-sheets do not work with this method. Additionally any styling that is not inline will also not render.
And some other considerations on this matter:
How can I export tables to excel from a webpage
http://www.c-sharpcorner.com/UploadFile/ankurmalik123/export-html-to-excel-and-more/
Upvotes: 2