2016-03-21 6 views
0

Ich habe ein Formular in VB.NET mit .NET 4.5. Ich habe eine EXCEL-Datei neben dem Formular geöffnet.Live Update-Daten in Excel-Tabelle von VB.NET Formular

Ich möchte die aktualisierten Daten aus Code, LIVE, in der EXCEL-Tabelle sehen. Aber kann die Daten nicht sehen.

Unten ist der Code

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 

Antwort

1

Das folgende Beispiel der Haupttreffen Arbeit in einem Codemodul erfolgt, Objekt des Arbeitsblatts, wenn das Blatt gefunden wurde in diesem Fall mit dem Anrufer, eine Formular-Schaltfläche klicken zurückgegeben .

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 

im Anschluss an die am oberen Rand des Formulars

Imports Excel = Microsoft.Office.Interop.Excel 

Add

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

In einem Button-Klick Ereignisebene Variablen bilden die Funktion oben mit als erstes Argument nennen Dateiname und Pfad gefolgt vom Namen des Blattes übergeben Sie dann unsere Variable Erfolg oben.

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

Jetzt ändern Sie einen Zellenwert und sehen Sie es live. Fügen Sie eine TextBox hinzu, fügen Sie etwas ein und rufen Sie sie wie folgt auf. Beachten Sie, dass die Zelladresse das sein kann, was Sie möchten.

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