2012-05-04 49 views
0

我已经创建了一个ashx文件来在网站上打开excel 2007。如何使用.ashx在浏览器上打开excel 2007/2010?

这里是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应用程序打开。 请帮帮我。

非常感谢。

回答

0

context.Response.WriteFile(filePath); //你正试图写入一个文件块。

在此之前,您需要将所有数据提取到内存中。更改喜欢这个

context.Response.Write(YourExcelData); 
+0

喜拉维, 我想下面编辑代码,但它的主要错误(无法打开ebcause文件格式或文件ext是无效的),当打开文件: 的FileStream FS =新的FileStream( filePath,FileMode.Open,FileAccess.Read); byte [] b =新字节[(int)fs.Length]; fs.Read(b,0,(int)fs.Length); fs.Close(); context.Response.Write(b); –

相关问题