how to open an excel file with in the asp.net web page

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 5
Reputation: anithasalavadi is an unknown quantity at this point 
Solved Threads: 0
anithasalavadi anithasalavadi is offline Offline
Newbie Poster

how to open an excel file with in the asp.net web page

 
0
  #1
Aug 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,242
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 577
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: how to open an excel file with in the asp.net web page

 
0
  #2
Aug 6th, 2009
You want to "embed" the excel application inside of the page? That isn't going to be easy... if its' even possible.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 147
Reputation: anuj576 is an unknown quantity at this point 
Solved Threads: 10
anuj576 anuj576 is offline Offline
Junior Poster

Re: how to open an excel file with in the asp.net web page

 
0
  #3
Aug 6th, 2009
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:



  1. private void ExportToExcel(DataGrid dgExport)
  2. {
  3. try
  4. {
  5. string strFileName = String.Empty, strFilePath= String.Empty;
  6. strFilePath = Server.MapPath(@"../Excel/") + "ExcelFileName" + ".xls";
  7. if (File.Exists(strFilePath))
  8. {
  9. File.Delete(strFilePath);
  10. }
  11. System.IO.StringWriter oStringWriter =new StringWriter();
  12. System.Web.UI.HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
  13. StreamWriter objStreamWriter;
  14. string strStyle =@" .text { mso-number-format:\@; }
  15. ";
  16. objStreamWriter = File.AppendText(strFilePath);
  17. dgExport.RenderControl(oHtmlTextWriter);
  18. objStreamWriter.WriteLine(strStyle);
  19. objStreamWriter.WriteLine(oStringWriter.ToString());
  20. objStreamWriter.Close();
  21. string strScript = "<script language=JavaScript>window.open('../Excel/" + "ExcelFileName" +
  22. ".xls','dn','width=1,height=1,toolbar=no,top=300,left=400,right=1,
  23.  
  24. scrollbars=no,locaton=1,resizable=1');</script>";
  25. if(!Page.IsStartupScriptRegistered("clientScript"))
  26. {
  27. Page.RegisterStartupScript("clientScript", strScript);
  28. }
  29. }
  30. catch(Exception)
  31. {
  32. //Handle Exception
  33. }
  34. }

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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,242
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 577
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: how to open an excel file with in the asp.net web page

 
0
  #4
Aug 6th, 2009
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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 147
Reputation: anuj576 is an unknown quantity at this point 
Solved Threads: 10
anuj576 anuj576 is offline Offline
Junior Poster

Re: how to open an excel file with in the asp.net web page

 
0
  #5
Aug 6th, 2009
I'm really sorry...It will definitely not happen again..and this post is not related to my earlier posts..again m sorry
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,242
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 577
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: how to open an excel file with in the asp.net web page

 
0
  #6
Aug 6th, 2009
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?
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 5
Reputation: anithasalavadi is an unknown quantity at this point 
Solved Threads: 0
anithasalavadi anithasalavadi is offline Offline
Newbie Poster

Re: how to open an excel file with in the asp.net web page

 
0
  #7
Aug 6th, 2009
even if we remove target field, the excel file is opening as separate file. Can't we use iframes in asp.net. any idea?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 19
Reputation: chithrasujith is an unknown quantity at this point 
Solved Threads: 3
chithrasujith chithrasujith is offline Offline
Newbie Poster

Re: how to open an excel file with in the asp.net web page

 
0
  #8
Aug 6th, 2009
The following code may help you to open excel file with in the same asp.net web page

  1. using System.Data.OleDb;
  2.  
  3.  
  4.  
  5. public partial class _Default : System.Web.UI.Page
  6. {
  7.  
  8. protected void Page_Load(object sender, EventArgs e)
  9. {
  10. OleDbConnection ExcelConection = null;
  11. OleDbCommand ExcelCommand = null;
  12. OleDbDataReader ExcelReader = null;
  13. OleDbConnectionStringBuilder OleStringBuilder = null;
  14.  
  15. try
  16. {
  17. OleStringBuilder =
  18. new OleDbConnectionStringBuilder(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';");
  19. OleStringBuilder.DataSource = MapPath(@"~\MyExcel.xls");
  20.  
  21. ExcelConection = new OleDbConnection();
  22. ExcelConection.ConnectionString = OleStringBuilder.ConnectionString;
  23.  
  24. ExcelCommand = new OleDbCommand();
  25. ExcelCommand.Connection = ExcelConection;
  26. ExcelCommand.CommandText = "Select * From [Sheet1$]";
  27.  
  28. ExcelConection.Open();
  29. ExcelReader = ExcelCommand.ExecuteReader();
  30.  
  31. GridView1.DataSource = ExcelReader;
  32. GridView1.DataBind();
  33. }
  34. catch (Exception Args)
  35. {
  36. LabelErrorMsg.Text = "Could not open Excel file: " + Args.Message;
  37. }
  38. finally
  39. {
  40. if (ExcelCommand != null)
  41. ExcelCommand.Dispose();
  42. if (ExcelReader != null)
  43. ExcelReader.Dispose();
  44. if (ExcelConection != null)
  45. ExcelConection.Dispose();
  46. }
  47. }
  48. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 5
Reputation: anithasalavadi is an unknown quantity at this point 
Solved Threads: 0
anithasalavadi anithasalavadi is offline Offline
Newbie Poster

Re: how to open an excel file with in the asp.net web page

 
0
  #9
Aug 7th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC