Skip to main content
 首页 » 编程设计

c#之在浏览器中打开 doc/docx/excel 文件(Asp.net、C#)

2024年02月24日16bonelee

我正在文件系统中存储一些文件,如 PDF、Doc、Docx、xls 等。

现在我必须在浏览器上显示这些文件以供用户查看。

基本上,数据网格中有一个链接按钮,用户单击该链接按钮即可查看。

到目前为止我的代码是这样的

Response.Clear(); 
 
// Response.ContentType = "application/pdf"; 
//Response.ContentType = "application/x-msexcel";        
//Response.ContentType = "application/msword"; 
 
string strFilePath = @"C:\test.pdf"; 
// string strFilePath = @"C:\test.doc"; 
 
Response.WriteFile(strFilePath); 
Response.End(); 

这适用于 pdf 文件,但不适用于 word 或 excel 文件。在浏览器中,我可以查看 pdf,但其他文件无法在浏览器中打开,但需要

Save , Open 对话框选项。如果我点击 Open,它将在普通窗口而不是浏览器中打开。

我怎样才能实现我的目标?浏览器为IE

提前致谢

请您参考如下方法:

你试过添加这样的东西吗?

Response.AppendHeader("Content-Disposition", "inline;filename=" + "ExcelFile.xls");