【问题标题】:MIME Types and exporting to different applications (excel, word , pdf ...etc etc)MIME 类型和导出到不同的应用程序(excel、word、pdf ...等)
【发布时间】:2011-04-12 12:21:58
【问题描述】:

我想知道当我编写一个像 application/vnd.ms-excel 这样的 mime 类型的文件时,幕后究竟发生了什么?

我的意思是我像这样导出到excel:

  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();

我知道 excel 文件有特定的格式。所以我想知道它是如何在幕后完成的? 我想到的是它只是生成 HTML 并将其粘贴到 EXCEL 中,而不是真正导出为 EXCEL 格式?

如果我错了,谁能纠正我?或者告诉我幕后究竟发生了什么?

【问题讨论】:

    标签: c# asp.net excel mime-types


    【解决方案1】:

    没有什么神奇的事情发生——你确实只是在生成 HTML; MIME 类型仅用于指示应由哪个应用程序处理数据。操作系统和浏览器在这方面协同工作,因此当浏览器看到具有该 MIME 类型的响应时,它会为您打开 Excel。

    如果您没有跟随任何可能生成此请求的链接,而是执行了右键单击-另存为,那么您最终会在文件中得到 HTML。 ASP.NET 并不是在幕后将其翻译成 Excel 的原生格式。

    一般来说,如果您有兴趣了解这样的情况,您可能需要运行 Fiddler(诊断代理)或 Wireshark(网络嗅探器) - 两者都会让您查看浏览器和服务器之间正在交换哪些数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 2015-01-06
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多