简体   繁体   中英

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

I have developed an asp.net,c# application. In that application I have a task like open excel file, I've done the task when developing visual studio, its working fine locally .

but when I hosted my application into IIS server, it is not responding when I click read button.

My IIS Version - 7.5.7600

在此处输入图片说明

Asp.Net Code:

<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# Code :

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);
}

Do I have give any permission to open excel file through IIS?

You don't want Excel to start on your web server. Instead send the file to the user using Response.WriteFile() eg

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();
}

Something else you can do is use an Office URI scheme in the download links, if you are sure the user has Excel installed, so you contruct a hyperlink like so:

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

That ms-excel:ofv|u| prefix should get handled by Excel (if they have it installed).

The template for the download link might look like this:

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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