从VB.NET表单实时更新Excel表格中的数据

问题描述 投票:0回答:1

我有一个VB.NET与.NET 4.5的表单。我有一个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
vb.net excel interop excel-interop winforms-interop
1个回答
1
投票

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

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
© www.soinside.com 2019 - 2024. All rights reserved.