0

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

enter image description here

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?

1
  • 2
    Why would you actually OPEN excel file in IIS on server? Why? What you need is to let your user to download it, not Open in IIS. when I saw ProcessStartInfo a big red flag went up Commented Sep 20, 2017 at 6:39

2 Answers 2

2

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

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" />  
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply.. i have tried the above code but the excel file is getting downloaded. i don't want the file to be downloaded..what my requirement is when i click the read button the excel file need to open without downloading..
What you are describing is a user-controlled behavior. Not an application-controlled behavior.
@Surya - You can't do that. It's 2017 and running arbitrary apps on the user's computer is not something you can do, for good reason.
@Surya I've added a possible solution that uses Office URI schemes (<a href="ms-excel:...") that might work.
1

I don't know why you need to do that, but anyway: the iis user that runs your web application, simply do not have that much rights to run the excel app, you can change your application pool Identity (IIS manager -> ApplicationPools -> "YourApplicationPool" Advanced Settings -> Identity), from ApplicationPoolIdentity to LocalSystem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.