我已经整理了一些可以工作的代码,但我必须承认,我不了解它所做的事情的本质,我似乎打开了两个连接,这似乎缓慢而混乱,一个是为了获得我认为我需要的SQL调用的sheetname,另一个是调用本身。
我把它作为函数使用,但是为了改进它,我把它剥离成了一个Sub。它经常被用来从多达700个独立的文件中提取实用程序数据,作为一个进程的一部分,并在多个客户端上运行。所以如果它能被简化,它将大大减少时间。
文件格式因任务而异:报警数据51列宽,7行或700个站点 *7行 Jmeter 数据50列宽或99列宽,51、15行或最多700 * 15行处为空白列我无法控制文件格式/长度,也不知道工作表名称,因为它可能因源而异
任何帮助整理它是非常感谢。脚痛
编辑:文件将永远只有一个工作表,但名称未知。我只需要该工作表。
Function ReadExcelFile(ByRef InputFileArray() As Variant, InputFileName As String, InputFileLocation As String, HeaderYesNo As String)
'Reads Excel File and returns InputFileArray
Dim ReadFileArray() As Variant
Dim connectionString As String
Dim sql As String
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FileExists(InputFileLocation & InputFileName) = True Then
connectionString = _
"Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=""" & InputFileLocation & InputFileName & """;" & _
"Extended Properties=""Excel 12.0;HDR=" & HeaderYesNo & ";IMEX=1"""
'This assumes the Excel file contains column headers -- HDR=Yes
'Routine to get unknown sheet name
Set conn = CreateObject("ADODB.Connection")
conn.connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=""" & InputFileLocation & InputFileName & """;" & _
"Extended Properties=""Excel 12.0;HDR=Yes"""
conn.Open
Set bs = conn.OpenSchema(20) ' 20 = adSchemaTables
Do Until bs.EOF = True
'Debug.Print bs.Fields!Table_Name.Value
SheetName = bs.Fields!Table_Name.Value
bs.MoveNext
Loop
bs.Close: conn.Close
Set bs = Nothing
Set conn = Nothing
'Get the contents of the Excel via SQL saves opening file
sql = "SELECT * FROM [" + SheetName + "]" '
'Go to the VBE's Tools, References then locate and put a check beside 'Microsoft ActiveX Data Objects 6.1 Library' to include the library in your project.
Dim rs As New ADODB.Recordset
rs.Open sql, connectionString
ReadFileArray() = rs.GetRows 'Puts the data from the recordset into an array
rs.Close
Set rs = Nothing
'Debugging Tool
'Dim row As Variant, column As Variant
'For row = 0 To UBound(TotalFileArray, 2)
' For column = 0 To UBound(InputFileArray, 1)
' Debug.Print InputFileArray(column, row)
' Next
'Next
'Limitations mean the columns and rows are read in wrong order.
'Public Sub to transpose array
TransposeArray ReadFileArray, InputFileArray
Erase ReadFileArray
Else
End If
End Function
1条答案
按热度按时间zfycwa2u1#
您可以重复使用一个连接和记录集。请注意,如果您的输入文件有多个工作表和/或命名区域,那么它只会选择列出的第一个。
此外,您不会在返回的数组中获得字段标题。