Winforms项目打开Excel文件不一致

Winforms project opening Excel file inconsistently

提问人:wizard 提问时间:10/16/2023 最后编辑:wizard 更新时间:10/16/2023 访问量:51

问:

我有一个证券交易所模拟器,该应用程序在打开时打开并将 excel 工作簿的内容读取到列表中,该工作簿包含所有股票信息作为股票数据类型。 目前,程序有时无法打开项目,显示“呼叫被被调用者拒绝”错误。

有谁知道我应该对我的代码进行哪些更改,以便应用程序可以一致地打开并与 excel 文件通信?

    Public Sub LoadStocksToList()
        'open stocks file and instantiate new stock objects with values from the excel database

        'initialise variables 
        Dim ticker As String = ""
        Dim name As String = ""
        Dim val As Double = 0
        Dim vol As Long = 0
        Dim mktcap As Double = 0
        Dim owned As Long = 0


        If IsFileLocked(file) Then
            MessageBox.Show("Excel file is currently locked by another process")
            Application.Exit()
        End If

        Dim xl As New Excel.Application

        Dim wb As Excel.Workbook = Nothing
        Dim ws As Excel.Worksheet = Nothing

        Try

            Console.WriteLine("Connecting to database...")

            xl.DisplayAlerts = False
            xl.ScreenUpdating = False

            wb = xl.Workbooks.Open(file, [ReadOnly]:=True, UpdateLinks:=False)
            ws = DirectCast(wb.Worksheets("Sheet1"), Excel.Worksheet)
            wb.RefreshAll()


            Dim totalRow As Integer = xl.ActiveSheet.UsedRange.Rows.Count 'total number of rows

            For i = 1 To totalRow
                With xl.Worksheets("Sheet1")
                    ticker = .Range(("B" + i.ToString)).value  'assign value from cell in each row to variable
                    name = .Range(("C" + i.ToString)).value
                    val = .Range(("D" + i.ToString)).value
                    vol = .Range(("E" + i.ToString)).value
                    mktcap = .Range(("I" + i.ToString)).value
                    owned = False
                End With
                name = FormatStockName(name) 'format the stock name to exclude uneeded characters

                Dim Values As New List(Of Double) 'list of price values for that stock item
                Values.Add(val) 'add the first stock price to the list of vlaues 
                Dim Times As New List(Of DateTime) 'list of times of price change
                Times.Add(DateTime.Now) 'add first tm=ime to list of times
                'create new object and add to list of objects
                StocksList.Add(New Stock(ticker, name, vol, mktcap, Values, Times, owned))
            Next

        Catch ex As Exception

            MessageBox.Show("Database cannot currently be accessed. Please try again later" & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Application.Exit()

        Finally

            xl.DisplayAlerts = True
            xl.ScreenUpdating = True

            If ws IsNot Nothing Then Marshal.ReleaseComObject(ws)
            If ws IsNot Nothing Then
                wb.Close(False)
                Marshal.ReleaseComObject(wb)
            End If
            If xl IsNot Nothing Then
                xl.Quit()
                Marshal.ReleaseComObject(xl)
            End If

        End Try

我尝试创建一个新文件,更改excel中的自动保存设置,并添加代码块以在尝试打开文件之前检查文件是否繁忙

Excel vb.net WinForms

评论

0赞 Hans Passant 10/16/2023
该问题在描述错误消息方面做得不好。在这里寻找可能的匹配。
0赞 wizard 10/16/2023
@HansPassant谢谢,我试图修改它,错误只是说“呼叫被呼叫拒绝”
0赞 Hans Passant 10/16/2023
然后我链接了正确的 Q+A。

答: 暂无答案