【问题标题】:Not Able to Open Excel File When Hosting In IIS Server In Asp.Net C#在 Asp.Net C# 中的 IIS 服务器中托管时无法打开 Excel 文件
【发布时间】:2017-09-20 04:56:43
【问题描述】:

我开发了一个 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 文件?

【问题讨论】:

  • 为什么要在服务器上的 IIS 中打开 excel 文件?为什么?您需要的是让您的用户下载它,而不是在 IIS 中打开。当我看到ProcessStartInfo 一面大红旗升起

标签: c# asp.net .net excel iis


【解决方案1】:

您不希望 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();
}

如果您确定用户已安装 Excel,您还可以在下载链接中使用 Office URI scheme,因此您可以像这样构建一个超链接:

<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" />  

【讨论】:

  • 感谢您的回复.. 我已经尝试了上面的代码,但是正在下载 excel 文件。我不希望文件被下载..我的要求是当我点击读取按钮时,excel文件需要在不下载的情况下打开..
  • 您所描述的是用户控制的行为。不是应用程序控制的行为。
  • @Surya - 你不能那样做。现在是 2017 年,您无法在用户的计算机上运行任意应用程序,这是有充分理由的。
  • @Surya 我添加了一个可能的解决方案,该解决方案使用可能有效的 Office URI 方案 (&lt;a href="ms-excel:...")。
【解决方案2】:

我不知道您为什么需要这样做,但无论如何:运行您的 Web 应用程序的 iis 用户根本没有运行 excel 应用程序的那么多权限,您可以更改您的应用程序池标识(IIS 管理器-> ApplicationPools -> "YourApplicationPool" Advanced Settings -> Identity),从ApplicationPoolIdentityLocalSystem

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多