2

我正在使用以下代码将数据导出到 Excel 表。

    private void ExportToExcel(string fileName)
    {
        fileName = "MyXML.xls";

        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        rptLinks.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }

一切正常,但是当我尝试通过双击打开 Excel 文件MyXML.xls时,会打开一个弹出/对话框,并显示以下消息:

“您尝试打开的文件,MyXML.xls的格式与文件扩展名指定的格式不同。”等..

我们可以对代码进行一些更改,以免弹出/对话框出现吗?

4

2 回答 2

3

希望这可以帮助

http://devblog.grinn.net/2008/06/file-you-are-trying-to-open-is-in.html

于 2012-04-26T09:51:30.437 回答
2

更改 Response.ContentType = "application/vnd.xls"; 到 Response.ContentType = "application/vnd.ms-excel"

于 2012-04-26T09:46:01.733 回答