如果首先打开工作表,vb.net 无法读取 excel 工作表

Posted

技术标签:

【中文标题】如果首先打开工作表,vb.net 无法读取 excel 工作表【英文标题】:vb.net cannot read excel sheet if sheet is open first 【发布时间】:2014-12-16 13:53:48 【问题描述】:

我的工作簿在加载表单之前已打开。

在excel中使用vba,这段代码可以正常工作:

 wbListPath = "C:\WAREHOUSE CONTROL ----- DO NOT DELETE\"
 wbListName = "WAREHOUSE CONTROL FORM ----- DO NOT DELETE.xlsm"


 Set wbList = Application.Workbooks("WAREHOUSE CONTROL FORM ----- DO NOT DELETE.xlsm")

If Not BookOpen(wbList.Sheets("Directories").Cells(15, 3)) Then                    ' inventory master
    wb2 = Workbooks.Open(wbList.Sheets("Directories").Cells(15, 2) & wbList.Sheets("Directories").Cells(15, 3))    'Cambridge Master
 Else
     wbLkup = wbList.Sheets("Directories").Cells(15, 3).Text
     Set wb2 = Application.Workbooks(wbLkup)
 End If




Function BookOpen(strBookName As String) As Boolean
    Dim oBk As Workbook
    On Error Resume Next
    Set oBk = Workbooks(strBookName)
    On Error GoTo 0
    If oBk Is Nothing Then
        BookOpen = False
    Else
        BookOpen = True
    End If
End Function

切换到 vb.net,我无法使用任何代码来检测在运行 form1 之前已打开的工作簿,我能找到的最接近的是:

Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
     Dim WorkBookNames() As String = TextBox1.Text, TextBox1.Text & "  [Compatibility Mode]", "Microsoft Excel - 001 Phone List  [Compatibility Mode]"
    exPhone = ChkNOpenWB(exPhone, WorkBookNames, TextBox12.Text)
    excelApp.Visible = True

End Sub

Function ChkNOpenWB(bookName As Workbook, WorkBookNames() As String, path As String) As Workbook
    Dim openFlag As Boolean = True
    For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses()
        If p.ProcessName = "EXCEL" Then
            For Each excelFile As String In WorkBookNames
                If p.MainWindowTitle.Contains(excelFile) Then
                    bookName = GetObject(TextBox12.Text & "\" & TextBox1.Text)
                    bookName.RefreshAll()
                    openFlag = False
                End If
            Next
        End If
    Next
    If (openFlag) Then
        bookName = excelApp.Workbooks.Open(TextBox12.Text & "\" & TextBox1.Text, ReadOnly:=False)
        bookName.Activate()
    End If
    Return (bookName)
End Function

【问题讨论】:

【参考方案1】:

您也可以使用编组。这将查看工作簿是否已打开。如果不是,它会打开它。如果它已经打开,则将工作簿分配给变量。

    Dim exApp AS Excel.Application = New Excel.Application
    Dim wb as excel.Workbook
    Dim exSheet As Excel.Worksheet = Nothing

    exApp.Visible = True

    wb = System.Runtime.InteropServices.Marshal.BindToMoniker(FileNamePath)
    exApp = wb.Parent

    'lets see it
    exApp.Visible = True
    exApp.Windows(1).Visible = True

    Dim tSheet As Excel.Worksheet = exApp.ActiveWorkbook.Sheets.Item(1)

【讨论】:

【参考方案2】:

您可以检查文件是否可以打开进行写入。如果在 Excel 中打开,检查将失败。

Public Shared Function FileInUse(ByVal Filename As String) As Boolean

    Dim thisFileInUse As Boolean = False
    If System.IO.File.Exists(Filename) Then
        Try
            Using f As New IO.FileStream(Filename, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                thisFileInUse = False
            End Using
        Catch
            thisFileInUse = True
        End Try
    End If
    Return thisFileInUse

End Function

【讨论】:

以上是关于如果首先打开工作表,vb.net 无法读取 excel 工作表的主要内容,如果未能解决你的问题,请参考以下文章

如何选择使用 VB.NET 打开的 Excel 版本?

用vb.net如何读取excel单元格内容?

vb.net怎么从DataSet中读取一行的数据

动态生成链接菜单 vb.net

关于VB.NET的CSV文件的读取

vb.net获取excel页面高度