views:

64

answers:

1

i was wondering what really happen behind the scene when i write a file with mime type like this application/vnd.ms-excel

i mean i export to excel like this:

  gridView.Page.Response.Clear();

        gridView.Page.Response.Buffer = true;



        gridView.Page.Response.AddHeader("content-disposition",

        "attachment;filename=GridViewExport.xls");

        gridView.Page.Response.Charset = "";

        gridView.Page.Response.ContentType = "application/vnd.ms-excel";




        StringWriter sw = new StringWriter();

        HtmlTextWriter hw = new HtmlTextWriter(sw);



        gridView.AllowPaging = false;
        gridView.AllowSorting = false;
        gridView.DataBind();
        gridView.RenderControl(hw);




        //style to format numbers to string

        string style = @"<style> .textmode { mso-number-format:\@; } </style>";

        gridView.Page.Response.Write(style);

        gridView.Page.Response.Output.Write(sw.ToString());

        gridView.Page.Response.Flush();

        gridView.Page.Response.End();

i know excel files have specific format.so i was wondering how it gets done behind the scene ? what i think of is it just generate HTML and paste it into EXCEL and its not really exporting to EXCEL format ?

can any one correct me if im wrong ? or tell me what really happens behind the scene ?

+1  A: 

Nothing magical is happening - you are indeed just generating HTML; the MIME type is merely there to indicate which application should handle the data. The operating system and browser work together on that front, so when the browser sees a response with that MIME type, it opens Excel for you.

If instead of following whatever link might generate this request, you did a Right-Click-Save-As, you'd just end up with HTML in the file. It's not like ASP.NET is translating it into Excel's native format behind the scenes.

Just on a general note, if you're interested in seeing what goes on like this, you might want to run either Fiddler (a diagnostic proxy) or Wireshark (a network sniffer) - both will let you see what data is being exchanged between the browser and the server.

Jon Skeet