0

Do you make this code to create an Excel spreadsheet with two tabs? This only does one presently.

string attachment = string.Empty;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

HtmlForm frm = new HtmlForm();
frm.Attributes["runat"] = "server";

attachment = "attachment; filename=MyFile.xls";
GridView1.Parent.Controls.Add(frm);
frm.Controls.Add(GridView1);

Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";

frm.RenderControl(htw);

Response.Write(sw.ToString());

Response.End();
1
  • MyFile.xls, does it exist already or is the system supposed to be creating it. If it exists, check what its defaults are, otherwise, look into command line and initialization defaults in excel, there are ways to change what excel defaults new files to Commented Feb 4, 2013 at 18:35

2 Answers 2

1

I'm pretty sure you can't do it unless you're using MS Office Interop assemblies for Excel or some library such as Open XML SDK.

Note that you are not creating genuine excel file with the method above but just tricking excel into opening data you sent as if those are genuine excel files. This is why you'll most probably get warning message when opening this in MS Excel.

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

Comments

0

There appears to a Microsoft Article detailing how to export data from an application into Excel, although it's a bit dated (2005) http://support.microsoft.com/kb/306023

It appears to cover multiple worksheets, although I've never used these methods myself.

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.