2016-03-21 88 views
0

我有一個VB.NET與.NET 4.5中的窗體。 我有一個EXCEL文件並排打開窗體。從VB.NET Excel表格中的實時更新數據表格

我想在EXCEL工作表中看到來自代碼LIVE的更新數據。 但不能看到數據。

下面是代碼

Imports Microsoft.Office.Interop.Excel 
Public Class Form1 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    'Me.OpenFileDialog1.ShowDialog() 
    Dim xlApp As Application 
    Dim xlWorkBook As Workbook 
    Dim xlWorkSheet As Worksheet 

    xlApp = New ApplicationClass 
    'xlApp.ScreenUpdating = False 
    xlWorkBook = xlApp.Workbooks.Open("E:\BACKUP\TRY.xls") 
    xlWorkSheet = xlWorkBook.Worksheets("Sheet1") 
    'display the cells value B2 
    MsgBox(xlWorkSheet.Cells(8, 1).value) 'GETTING EXISTING VALUE OK 
    'edit the cell with new value 
    xlWorkSheet.Cells(2, 2) = "HI" 'WANT TO SEE THIS DATA BEING LIVE UPDATED 
    'xlWorkBook.Close() 'DONT WANT TO CLOSE THE OPENED SHEET/WORKBOOK 
    'xlApp.ScreenUpdating = True 
    xlApp.Quit() 

    releaseObject(xlApp) 
    releaseObject(xlWorkBook) 
    releaseObject(xlWorkSheet) 

End Sub 

Private Sub releaseObject(ByVal obj As Object) 
    Try 
     System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) 
     obj = Nothing 
    Catch ex As Exception 
     obj = Nothing 
    Finally 
     GC.Collect() 
    End Try 
End Sub 
End Class 

回答

1

下面的示例主擅長的工作是在一個代碼模塊完成後,工作表對象,如果片材被發現被返回給調用者,在這種情況下,表格按鈕點擊。

Option Strict On 
Imports Excel = Microsoft.Office.Interop.Excel 
Imports Microsoft.Office 
Imports ST = System.Runtime.InteropServices 
Module OpenWorkSheets2 
    ''' <summary> 
    ''' Open file, activate sheet while keeping 
    ''' the excel file open 
    ''' </summary> 
    ''' <param name="FileName">Path and file name to open</param> 
    ''' <param name="SheetName">Worksheet to work with</param> 
    ''' <param name="FoundSheet">True indicates we are good to use the returned sheet object</param> 
    ''' <returns></returns> 
    ''' <remarks></remarks> 
    Public Function OpenExcel1(
     ByVal FileName As String, 
     ByVal SheetName As String, 
     ByRef FoundSheet As Boolean) As Excel.Worksheet 

     Dim xlApp As Excel.Application = Nothing 
     Dim xlWorkBooks As Excel.Workbooks = Nothing 
     Dim xlWorkBook As Excel.Workbook = Nothing 
     Dim xlWorkSheet As Excel.Worksheet = Nothing 
     Dim xlWorkSheets As Excel.Sheets = Nothing 

     xlApp = New Excel.Application 
     xlApp.DisplayAlerts = False 
     xlWorkBooks = xlApp.Workbooks 
     xlWorkBook = xlWorkBooks.Open(FileName) 
     xlApp.Visible = True 
     xlWorkSheets = xlWorkBook.Sheets 

     For x As Integer = 1 To xlWorkSheets.Count 
      xlWorkSheet = CType(xlWorkSheets(x), Excel.Worksheet) 

      If xlWorkSheet.Name = SheetName Then 
       xlWorkSheet.Activate() 
       FoundSheet = True 
       Exit For 
      End If 

      ST.Marshal.FinalReleaseComObject(xlWorkSheet) 
      xlWorkSheet = Nothing 
     Next 

     Return xlWorkSheet 

    End Function 
End Module 

發生在形式

Imports Excel = Microsoft.Office.Interop.Excel 

添加的頂部以下,形成級別的變量

Private xlWorkSheet As Excel.Worksheet = Nothing 
Private Success As Boolean = False 

在一個按鈕單擊事件調用上面的函數作爲第一個參數文件名和路徑,然後傳入我們上面的變量Success。

xlWorkSheet = OpenExcel1(
    IO.Path.Combine(Application.StartupPath, "Customers.xlsx"), 
    "Orders", 
    Success) 

現在改變一個單元格的值,並且現場看到它。添加一個文本框,在其中放置一些內容並按如下所示調用它。注意單元地址可以是你想要的。

If Not String.IsNullOrWhiteSpace(TextBox1.Text) Then 
    If Me.Success Then 
     xlWorkSheet.Cells(2, 2) = TextBox1.Text 
    Else 
     MessageBox.Show("Failed") 
    End If 
Else 
    MessageBox.Show("Please enter a value") 
End If 
相關問題