[英]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
池”高級設置->身份),從ApplicationPoolIdentity
到LocalSystem
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.