Hi all,
<a href= "filename.xls" target="_blank">Open Excel</a>;


by using above line of code i can able to open the excel file as separate file.
But i want to open that excel file with in the same asp.net web page not separately.

Recommended Answers

All 9 Replies

You want to "embed" the excel application inside of the page? That isn't going to be easy... if its' even possible.

If you want to open the excel file in the same window
the following steps can be followed:

1. Go to Windows Explorer.
2. On the Tools menu, click Folder Options, and then click on the File Types tab.
3. From the Registered file types list box, select the XLS extension, and then click Advanced.
4. In the Edit File Type dialog box, set the Confirm open after download to selected.
5. Make sure the Browse in same window option is not selected, and then click OK.

The above steps will make sure that we get the dialog box as shown above. However, since this is an option set at the client computer, these steps cannot be mandated to be followed in every computer that browses the application.

So, from the code level, we must make sure that the excel file is opened in a separate window. One possible option for this is to Save the file to the web server, and then open the file in a separate window.

The code for this is given below:

private void ExportToExcel(DataGrid dgExport)
{
try
{
string strFileName = String.Empty, strFilePath= String.Empty;
strFilePath = Server.MapPath(@"../Excel/") + "ExcelFileName" + ".xls";
if (File.Exists(strFilePath))
{
File.Delete(strFilePath);
}
System.IO.StringWriter oStringWriter =new StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
StreamWriter objStreamWriter;
string strStyle =@" .text { mso-number-format:\@; } 
";
objStreamWriter = File.AppendText(strFilePath);
dgExport.RenderControl(oHtmlTextWriter);
objStreamWriter.WriteLine(strStyle);
objStreamWriter.WriteLine(oStringWriter.ToString());
objStreamWriter.Close();
string strScript = "<script language=JavaScript>window.open('../Excel/" + "ExcelFileName" +
".xls','dn','width=1,height=1,toolbar=no,top=300,left=400,right=1, 

scrollbars=no,locaton=1,resizable=1');</script>";
if(!Page.IsStartupScriptRegistered("clientScript"))
{
Page.RegisterStartupScript("clientScript", strScript);
}
}
catch(Exception)
{
//Handle Exception
}
}

In the above method, the file is saved to the Web Server inside the folder "Excel". Of course, this folder must have write permissions for the user. But it will definitely ensure that the excel file is opened in a new window in the client computer.

You have opened three threads about javascript not working with a master page now you're posting code without tags -- and i'm almost certain this is related to three other threads you posted.

At this point i'm not sure your posts merit answers if you are not going to follow the forum rules.

I'm really sorry...It will definitely not happen again..and this post is not related to my earlier posts..again m sorry

Well go back and hit "Edit Message" and put code tags in :)

Why don't you remove target="_blank" so the browser doesn't open a new window?

even if we remove target field, the excel file is opening as separate file. Can't we use iframes in asp.net. any idea?

The following code may help you to open excel file with in the same asp.net web page

using System.Data.OleDb;
		


public partial class _Default : System.Web.UI.Page 
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        OleDbConnection ExcelConection = null;
        OleDbCommand ExcelCommand = null;
        OleDbDataReader ExcelReader = null;
        OleDbConnectionStringBuilder OleStringBuilder = null;

        try
        {
            OleStringBuilder =
                new OleDbConnectionStringBuilder(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';");
            OleStringBuilder.DataSource = MapPath(@"~\MyExcel.xls");

            ExcelConection = new OleDbConnection();
            ExcelConection.ConnectionString = OleStringBuilder.ConnectionString;

            ExcelCommand = new OleDbCommand();
            ExcelCommand.Connection = ExcelConection;
            ExcelCommand.CommandText = "Select * From [Sheet1$]";

            ExcelConection.Open();
            ExcelReader = ExcelCommand.ExecuteReader();

            GridView1.DataSource = ExcelReader;
            GridView1.DataBind();
        }
        catch (Exception Args)
        {
            LabelErrorMsg.Text = "Could not open Excel file: " + Args.Message;
        }
        finally
        {
            if (ExcelCommand != null)
                ExcelCommand.Dispose();
            if (ExcelReader != null)
                ExcelReader.Dispose();
            if (ExcelConection != null)
                ExcelConection.Dispose();
        }  
    }
}

Hi Chithrasujith..
Thanq so much for ur response.
By the code which u provided, the excel file content is rewriting to asp web page. We can't able to do any manipulations and edit the data there rgt? But i want the whole excel file has to open in web page n moreover have to enter some input and will hv to do some manipulations based on formulae cells. Is thr any chance to achieve this?
Hope u understand my pblm.

hello.. I realy need to open the original excel file through a browser in a different computer and edit the details.When am trying using the HREF procedure, it is opening a copy of the file. How do I go about this please??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.