By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

Viewing Excel worksheet in Webbrowser

P: 27
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
Share this question for a faster answer!
Share on Google+

Post your reply

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