0

我已经创建了一个 ashx 文件来在网站上打开 excel 2007。

这是test.aspx中的代码:(调用test.htm打开一个弹出窗口)

    <a href="test.htm" target="_blank">Export</a>

这是test.htm中的代码:(使用iframe加载excel文件)

<iframe src="handler1.ashx" style="height: 421px; width: 800px" ></iframe>

这是handle.ashx中的代码:

    void ProcessRequest(HttpContext context)
    {

        string filePath = "E:/test.xlsx";
        string filename = Path.GetFileName(filePath);
        context.Response.Clear();
        context.Response.AddHeader("Content-Disposition", "inline;filename=test.xlsx");
        context.Response.Charset = "";
        context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        context.Response.WriteFile(filePath);
        context.Response.Flush();
        context.Response.End();
    }

但 Excel 文件始终由 Microsoft Excel 应用程序打开。请帮我。

太感谢了。

4

1 回答 1

0

context.Response.WriteFile(filePath); // 您正在尝试写入文件块。

在此之前,您需要将所有数据提取到内存中。改成这样

context.Response.Write(YourExcelData);
于 2012-05-04T03:51:51.307 回答