0

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?

3
  • What are you trying to export? Data in the html file? Commented Sep 17, 2013 at 15:04
  • It looks like you're trying to upload a file (fuFile), and render its contents back to the user in Excel format? If that's true, update your question with what you are attempting to do and we can guide you to a workable solution. Commented Sep 17, 2013 at 15:53
  • i am trying to let the user upload a html file and then convert it into Excel file on button click event. Commented Sep 18, 2013 at 15:28

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.