How to open excel file in same web page
<p>In my application there are different type of files which are uploaded and saved in an application directory folder. I have a list of files with a link button.</p>

<p>Now I want to open a file in the same webpage when the user click on linkbutton(with the filename) then that file should open in the same web page. I am able to open the files other than excel files... So I want to know how to open the excel file in same webpage... </p>

<p>I have tried many code but by that code a dialog box open for saving excel file... but i dont want that... i directly want to open a file in webpage</p>

<p>I have tried the code from below link but save as dialog box is opened
<a href="http://stackoverflow.com/questions/8161700/open-excel-file-in-asp-net">Open excel file in asp.net</a></p>

<p><strong>Below is my .aspx code</strong></p>

<pre><code> &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3"
        CssClass="grid" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" 
        BorderWidth="1px" onrowcommand="GridView1_RowCommand"&gt;
        &lt;RowStyle ForeColor="black" /&gt;
        &lt;Columns&gt;
         &lt;asp:TemplateField Visible="False" &gt;
                &lt;ItemTemplate&gt;
                    &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Bind("FK_UploadedFileID") %&gt;'&gt;&lt;/asp:Label&gt;
                &lt;/ItemTemplate&gt;
                &lt;HeaderStyle HorizontalAlign="Left" /&gt;
            &lt;/asp:TemplateField&gt;
            &lt;asp:TemplateField HeaderText="File Name"&gt;
                &lt;ItemTemplate&gt;
                    &lt;asp:Label ID="Label2" runat="server" Text='&lt;%# Bind("FileName") %&gt;'&gt;&lt;/asp:Label&gt;
                &lt;/ItemTemplate&gt;
                &lt;HeaderStyle HorizontalAlign="Left" /&gt;
            &lt;/asp:TemplateField&gt;
            &lt;asp:TemplateField HeaderText="Who shared"&gt;
                &lt;ItemTemplate&gt;
                    &lt;asp:Label ID="Label3" runat="server" Text='&lt;%# Bind("UserName") %&gt;'&gt;&lt;/asp:Label&gt;
                &lt;/ItemTemplate&gt;
                &lt;HeaderStyle HorizontalAlign="Left" /&gt;
                &lt;ItemStyle Width="200px" /&gt;
            &lt;/asp:TemplateField&gt;

            &lt;asp:TemplateField ShowHeader="False"&gt;
                &lt;ItemTemplate&gt;
                    &lt;asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandArgument='&lt;%#((GridViewRow)Container).RowIndex%&gt;'
                        Text='&lt;%# Bind("FileName") %&gt;' CommandName="ViewImages" &gt;&lt;/asp:LinkButton&gt;
                &lt;/ItemTemplate&gt;
            &lt;/asp:TemplateField&gt;
        &lt;/Columns&gt;
        &lt;FooterStyle BackColor="White" ForeColor="#000066" /&gt;
        &lt;PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /&gt;
        &lt;SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /&gt;
        &lt;HeaderStyle BackColor="black" Font-Bold="True" ForeColor="White" /&gt;
    &lt;/asp:GridView&gt;
</code></pre>

<p><strong>.cs</strong></p>

<pre><code>protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int inx = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "ViewImages")
            {
                string id = ((Label)GridView1.Rows[inx].FindControl("Label1")).Text;
                string filename = ((Label)GridView1.Rows[inx].FindControl("Label2")).Text;
                //Response.Redirect("Handler.ashx?FileName=" + filename);
                string path = HttpContext.Current.Server.MapPath("Uploadedfiles/" + filename);
                string name = System.IO.Path.GetFileName(path);
                string ext = System.IO.Path.GetExtension(path);
                string type = "";

                if (ext != null)
                {
                    switch (ext.ToLower())
                    {
                        case ".jpg":
                            type = "image/JPG";

                            Response.ContentType = type;

                            string FilePath = MapPath("Images/"+filename);
                            //Write the file directly to the HTTP content output stream.
                            Response.WriteFile(FilePath);
                            Response.End();
                            break;
                        case ".htm":
                        case ".html":
                            type = "text/HTML";
                            break;

                        case ".txt":
                            type = "text/plain";
                           // Response.Write(path);

                                  //Response.AppendHeader("content-disposition",
                                  //       "attachment; filename=\"" + name + "\"");

                                if (type != "")
                                    Response.ContentType = type;
                               Response.WriteFile(path); 
                                Response.End();
                            break;

                        case ".doc":
                        case ".rtf":
                            type = "Application/msword";
                            Convert_wordToHtml(filename);
                            break;

                        case ".pdf":

                            WebClient client = new WebClient();
                            Byte[] buffer = client.DownloadData(path);
                            type = "application/pdf";;
                            Response.ContentType = type;
                            Response.AddHeader("content-length", buffer.Length.ToString());
                            Response.BinaryWrite(buffer);
                            break;
                        case ".xls":
                        case ".xlsx":

                            System.IO.FileStream fs = null;
                            fs = System.IO.File.Open(path, System.IO.FileMode.Open);

                            byte[] btFile = new byte[fs.Length];
                            fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
                            fs.Close();
                            Response.AddHeader("Content-disposition", "attachment; filename=" + path);
                            Response.ContentType = "application/octet-stream";
                            Response.BinaryWrite(btFile);
                            Response.End();
                            break;
                        case ".wmv":

                            Response.Redirect("Default.aspx?FileName=" + filename);
                            break;  
                    }
                }
            }

        }

        catch (Exception ex)
        {
            string message = ex.Message;
        }
    }
</code></pre>
