views:

713

answers:

2

I have a tabbed series of grids on a data preview page. I now need to import the data from each grid into its own sheet in an Excel workbook.

How could I do this?

BTW, this is on a Mac or a PC.

+1  A: 

This is for .net C#

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition:", "attachment;filename=filename.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new  System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(Grid);
Grid.RenderControl(oHtmlTextWriter);

If you want to create multiple sheets in single work book then you will have to use a dll.

Samiksha
I don't want to send the file to the user, I want to create on the client machine.
ProfK
+1  A: 

I've done this in two ways in the past:

  1. Using Excel automation, and using the Excel API on a server to create multiple sheets. (Not recommended, but it does work.)

  2. Create the XML you would see when you save the equivalent spreadsheet in Excel and upload it as per Samiksha's suggestion.

    Normally I create a template in Excel, save as XML, then I alter the contents of it depending on the data I want to display.

Bravax