繁体   English   中英

在Asp.Net C#的IIS服务器中托管时,无法打开Excel文件

[英]Not Able to Open Excel File When Hosting In IIS Server In Asp.Net C#

我已经开发了一个asp.net,c#应用程序。 在该应用程序中,我有一个像打开excel文件这样的任务,我在开发Visual Studio时就完成了该任务, 它在本地可以正常工作

但是当我将应用程序托管到IIS服务器时,单击“读取”按钮时它没有响应。

我的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 URI方案 ,如果您确定用户已安装Excel,那么您可以构造一个超链接,如下所示:

<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应用程序,您可以更改应用程序池标识(IIS管理器-> ApplicationPools ->“您的ApplicationPoolIdentity池”高级设置->身份),从ApplicationPoolIdentityLocalSystem

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM