繁体   English   中英

VB.NET表单中Excel工作表中的实时更新数据

[英]Live update data in Excel sheet from VB.NET Form

我在带有.NET 4.5的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

下面的示例主要excel工作是在代码模块中完成的,如果找到工作表,则将工作表对象返回给调用者,在这种情况下,单击表单按钮。

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)

现在更改一个单元格值并实时查看它。 添加一个TextBox,在其中放置一些东西,然后按以下方式进行调用。 注意单元格地址可以是您想要的。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM