2

I am using the following code to export excel from html string.

Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition",
                               "attachment;filename=" + DateTime.Now.ToString("MM-  dd-yyyy H:mm:ss") +
                               "-FormAndImunizationTracker" +
                               ".xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(htmlString);
Response.End();
return null;

This is working fine but i need to add 2 more worksheets in the same excel for different report. I already have created html string for those report. How would I create worksheets and add the content into that. Thanks for you help.

Rajeev.Ranjan
  • 273
  • 1
  • 3
  • 14

2 Answers2

0

The easiest way to create true Excel files from C# is to use the ClosedXML library. ClosedXML is also available as a NuGet package to make it easy to get started.

  • If you have done earlier then can you provide me some input. I tried this link approach did not work. – Rajeev.Ranjan Nov 14 '13 at 09:27
  • Can you let me know what you tried exactly and how it went wrong? The sample directly on the front page shows how to add a single worksheet, but simply repeating that will give you multiple worksheets. I have used this approach in the past successfully in more complicated situations. – Wouter van der Neut Nov 18 '13 at 09:44
0

You will not be able to add worksheets to the output Excel by simply editing the htmlString. You will need to create an actual Ecxel file.

You can use the Microsoft.Office.Interop.Excel class to manipulate Excel files (new or existing)

Brendan
  • 1,237
  • 1
  • 16
  • 34
  • Will this give error if any system does not have Microsoft Office installed? – Rajeev.Ranjan Nov 14 '13 at 09:29
  • You will need Office installed - see : http://stackoverflow.com/questions/11448197/how-to-use-microsoft-office-interop-excel-on-a-machine-without-installed-ms-offi – Brendan Nov 14 '13 at 10:12