473,395 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Viewing Excel worksheet in Webbrowser

Have an application that opens up a webbrowser control to display data in an Excel spreadsheet (using VB.NET in VS 2005). No problem with opening the spreadsheet or displaying the data, but I'm having problems with repeating the process. I want to create an Excel spreadsheet, add some data to it, and display the results in a webbrowser control. I want to be able to then later delete the old Excel spreadsheet, create a new one, add some new data, and navigate the webbrowser to the new file. The errors I'm getting seem to imply that the webbrowser is not releasing the old Excel spreadsheet properly before I delete it and create a new one. Please take a look at the code below and tell me what I'm doing wrong.

Expand|Select|Wrap|Line Numbers
  1.   Private Sub GraphSpreadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GraphSpreadButton.Click
  2.  
  3.         Dim ExcelApp As Microsoft.Office.Interop.Excel.Application = _
  4.                            New Microsoft.Office.Interop.Excel.Application()
  5.  
  6.         Try
  7.  
  8.                   Dim sFileName As String = System.AppDomain.CurrentDomain.BaseDirectory & "SpreadGraph.xls"
  9.             Dim fFile As New FileInfo(sFileName)
  10.  
  11.             Dim ExcelWorkbook As Microsoft.Office.Interop.Excel.Workbook
  12.             Dim spreadChart As Microsoft.Office.Interop.Excel.Chart
  13.             Dim spreadData As Microsoft.Office.Interop.Excel.Worksheet
  14.  
  15. ' if the file exists, delete it so we can start fresh
  16.             If fFile.Exists Then
  17.                 ExcelWorkbook = ExcelApp.Workbooks.Open(sFileName)
  18.                 ExcelWorkbook.Save()
  19.                 ExcelWorkbook.Close()
  20.                 fFile.Delete()
  21.             End If
  22.  
  23. ' create a new workbook
  24.             ExcelWorkbook = ExcelApp.Workbooks.Add()
  25.  
  26. ' remove two sheets and add a chart
  27.             Dim removeSheet As Microsoft.Office.Interop.Excel.Worksheet = CType(ExcelWorkbook.Sheets.Item(3), Microsoft.Office.Interop.Excel.Worksheet)
  28.             removeSheet.Delete()
  29.             removeSheet = CType(ExcelWorkbook.Sheets.Item(2), Microsoft.Office.Interop.Excel.Worksheet)
  30.             removeSheet.Delete()
  31.             spreadData = CType(ExcelWorkbook.Sheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
  32.             spreadData.Name = "Data"
  33.  
  34.             spreadChart = CType(ExcelWorkbook.Charts.Add(), Microsoft.Office.Interop.Excel.Chart)
  35.             spreadChart.Name = "Surface"
  36.  
  37. ' this is where code to add data to sheet would be
  38.  
  39. ' display data in a surface chart
  40.              spreadChart.SetSourceData(spreadData.Range("A1", "U7"))
  41.  
  42.             spreadChart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlSurface
  43.  
  44.             spreadData.Visible = Microsoft.Office.Interop.Excel.XlSheetVisibility.xlSheetVisible
  45.             spreadChart.Visible = Microsoft.Office.Interop.Excel.XlSheetVisibility.xlSheetVisible
  46.  
  47. ' save new workbook
  48.             ExcelWorkbook.SaveAs(sFileName)
  49.             ExcelWorkbook.Close()
  50.             ExcelApp.Quit()
  51.  
  52.             SpreadExcelBrowse.Navigate(sFileName)
  53.  
  54.         Catch ex As Exception
  55.             MsgBox(ex.ToString)
  56.             ExcelApp.Quit()
  57.         End Try
  58.  
  59.     End Sub
  60.  
Dec 14 '07 #1
0 2657

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
1
by: javzxp | last post by:
Hi I'd like to use C# to open an existing Excel workbook and save each worksheet it contains into a new Excel file. The name of each new Excel file should be the name of the worksheet copied...
2
by: Shawn | last post by:
Does anybody know how to embed an Excel Worksheet into a (vb .net) form? I want the worksheet to appear as though it's a control on the form with all the functionality of a worksheet. I don't...
1
by: Gary | last post by:
Is there a way of selecting a specific worksheet in Excel and open it in the webbrowser control in vb.net thank Gary
1
by: Randall Arnold | last post by:
I'm converting a vbscript program to vb.net. Witht he exception of .net idiosyncrasies, most of it is working well with the same code. My only problem is that some properties and methods are...
4
by: zhangyl | last post by:
I want to embed the excel worksheet into my application and use it like datagrid, if somebody can tell a solution, whether visual studio 2005 can do it ? thanks.
9
by: G .Net | last post by:
Hi Can anybody tell me how I can view an Excel file from within a vb.net application? Thanks in advance G
0
by: arindams | last post by:
I'm getting problem in pasting HTML text in Excel 2007 whereas it is working fine in Excel 2003. The data pasted in excel 2007 is showing the the HTML code which is not desired. I need to have the...
0
by: arindams | last post by:
I've a form where an webrowser contains a html file. I want to copy a selective portion of the html content and paste that content in an excel 2007 file where it will be pasted as a normal text (not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.