【问题标题】:how to maintain connection with excel with rapidly data fetching如何通过快速获取数据来保持与 Excel 的连接
【发布时间】:2011-04-28 06:54:33
【问题描述】:

我正在制作一个交易网站,交易提要来自 Excel 表格中的来源。我必须在网格视图中显示来自 excel 工作表的数据。当我建立连接时,由于数据快速变化,它会失败;工作表中的每个单元格每秒更改 1-3 次值。我正在使用间隔为 100 的 Ajax 计时器。这是我的代码:

  Public Function RetrieveExcelData(ByVal excelSheetName As String, ByVal sheetNumber As Integer) As DataSet
        Dim objConn As OleDbConnection = Nothing
        Dim dt As System.Data.DataTable = Nothing
        Try
            ' Connection String.
            Dim connString As [String] = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Users\Vishal\Desktop\TESTING COLOURfor web1.xls;Extended Properties=Excel 8.0;"
            ' Create connection object by using the preceding connection string.
            objConn = New OleDbConnection(connString)
            ' Open connection with the database.
            objConn.Open()
            ' Get the data table containg the schema guid.
            dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
            If dt Is Nothing Then
                Return Nothing
            End If
            Dim excelSheets As [String]() = New [String](dt.Rows.Count - 1) {}
            Dim i As Integer = 0
            ' Add the sheet name to the string array.
            For Each row As DataRow In dt.Rows
                excelSheets(i) = row("TABLE_NAME").ToString()
                i += 1
                If i = sheetNumber Then
                    Exit For
                End If
            Next
            Dim excelCommand As New OleDbCommand("Select * from [" + excelSheets(sheetNumber - 1) & "]", objConn)
            Dim excelAdapter As New OleDbDataAdapter(excelCommand)
            Dim excelDataSet As New DataSet()
            excelAdapter.Fill(excelDataSet)
            Return excelDataSet
        Catch ex As OleDbException
            Throw ex
        Catch ex As Exception
            Throw ex
        Finally
            ' Clean up.
            If objConn IsNot Nothing Then
                objConn.Close()
                objConn.Dispose()
            End If
            If dt IsNot Nothing Then
                dt.Dispose()
            End If
        End Try
    End Function

【问题讨论】:

    标签: asp.net vb.net excel gridview


    【解决方案1】:

    说实话 - 我看不出它是如何工作的。您正在尝试使用 Excel 电子表格作为数据库来实时存储和检索数据,而 Excel 从来没有打算或设计过。

    您提到 Excel 每秒会获取数次数据。数据的来源是什么? RTD 组件?彭博API?我会尽量避免在电子表格中存储数据的中间步骤。

    【讨论】:

      猜你喜欢
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 2017-02-17
      • 1970-01-01
      相关资源
      最近更新 更多