不能打开Excel文件,当宿主在Asp.Net中的IIS服务器C#

我开发了一个asp.net,C#应用程序。 在那个应用程序中,我有一个像打开excel文件一样的任务,在开发visual studio时,我已经完成了任务, 在本地工作的很好

但是当我把我的应用程序托pipe到IIS服务器时,它没有响应,当我点击阅读button。

我的IIS版本 – 7.5.7600

在这里输入图像说明

Asp.Net代码:

<asp:TemplateField HeaderText="Read"> <ItemTemplate> <asp:LinkButton runat="server" ID="lnkKpiName" Text='✉ Read' CommandName="View" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CssClass="label" ForeColor="Green"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> 

C#代码:

 if (e.CommandName == "View") { LinkButton lnkBtn = new LinkButton(); lnkBtn.Attributes.Add("target", "_blank"); int index = Convert.ToInt32(e.CommandArgument); string FileName = ((Label)gvwUserManual.Rows[index].FindControl("lblFileName")).Text; ProcessStartInfo sInf = new ProcessStartInfo(); sInf.FileName = "EXCEL.EXE"; string XlsPath = Server.MapPath(@"~/SupportDocuments/" + Request.Cookies["BCookies"]["SessionUserName"].Trim().ToString() +"/" + FileName); sInf.Arguments = XlsPath; Process.Start(sInf); } 

我有任何权限通过IIS打开Excel文件?

您不希望Excel在您的Web服务器上启动。 而是使用Response.WriteFile()例如发送文件给用户

 if (e.CommandName == "View") { int index = Convert.ToInt32(e.CommandArgument); string FileName = ((Label)gvwUserManual.Rows[index].FindControl("lblFileName")).Text; string XlsPath = Server.MapPath(@"~/SupportDocuments/" + Request.Cookies["BCookies"]["SessionUserName"].Trim().ToString() +"/" + FileName); // send file to user Response.Clear(); Response.AppendHeader("content-disposition", "attachment; filename=" + FileName); Response.ContentType = "application/octet-stream"; Response.WriteFile(XlsPath); Response.Flush(); Response.End(); } 

你可以做的其他事情是在下载链接中使用Office URIscheme ,如果你确定用户安装了Excel,那么你可以像这样构build一个超链接:

 <a href="ms-excel:ofv|u|https://www.example.com/SupportDocuments/foo.xls">Open XLSX</a> 

那个ms-excel:ofv|u| 前缀应该由Excel处理(如果已安装)。

下载链接的模板可能如下所示:

 <asp:HyperLink runat="server" NavigateUrl='<%# "ms-excel:ofv|u|" + new Uri(Request.Uri, "/SupportDocuments/" + your_filename_variable ).AbsoluteUri %>' Text="Read" /> 

我不知道为什么你需要这样做,但无论如何:运行你的web应用程序的iis用户,根本没有太多的权限来运行excel应用程序,你可以改变你的应用程序池标识(IISpipe理器 – > ApplicationPools – >“YourApplicationPool”高级设置 – >身份),从ApplicationPoolIdentityLocalSystem