| | |
how to open an excel file with in the asp.net web page
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
You want to "embed" the excel application inside of the page? That isn't going to be easy... if its' even possible.
•
•
Join Date: Jul 2008
Posts: 147
Reputation:
Solved Threads: 10
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:
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.
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:
ASP.NET Syntax (Toggle Plain Text)
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.
Last edited by peter_budo; Aug 6th, 2009 at 11:07 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
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.
At this point i'm not sure your posts merit answers if you are not going to follow the forum rules.
Well go back and hit "Edit Message" and put code tags in 
Why don't you remove

Why don't you remove
target="_blank" so the browser doesn't open a new window? •
•
Join Date: Jul 2009
Posts: 19
Reputation:
Solved Threads: 3
The following code may help you to open excel file with in the same asp.net web page
asp Syntax (Toggle Plain Text)
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(); } } }
•
•
Join Date: Feb 2009
Posts: 5
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Updated : Simple ASP.Net Login Page (ASP.NET)
- Simple ASP.Net Login Page using C# (C#)
- How to Open PDF document in asp.net web page without asking save/open/cacel dialogue (ASP.NET)
- ASP.Net / C# Web Developer (Web Development Job Offers)
- Batch file in ASP.Net to convert Excel files into higher version (ASP.NET)
- Simple ASP.Net Login Page (Using VB.Net) (ASP.NET)
Other Threads in the ASP.NET Forum
- Previous Thread: Problem With a Function
- Next Thread: Tips & Trick in Testing Web Application
| Thread Tools | Search this Thread |
.net 2.0 activexcontrol advice ajax alltypeofvideos appliances application asp asp.net bc30451 beginner bottomasp.net box browser button c# cac checkbox click commonfunctions control dataaccesslayer database datagridview datagridviewcheckbox datalist deployment development dgv dialog dropdownlist dynamic dynamically edit embeddingactivexcontrol expose feedback fileuploader fill findcontrol flash form formatdecimal formview gridview gudi iframe iis javascript listbox login microsoft mono mouse mssql news novell numerical opera panelmasterpagebuttoncontrols parent radio redirect registration relationaldatabases reportemail save schoolproject search security select sessionvariables silverlight smartcard smoobjects software sql-server sqlserver2005 ssl suse textbox tracking treeview unauthorized validatedate validation vb.net video videos vista visualstudio web webapplications webdevelopemnt webdevelopment webprogramming webservice xsl youareanotmemberofthedebuggerusers






