aldovalerio
Programmer
I'm not sure this is the right forum for this, since it's more of an ADO question. I have an ActiveX Script Task that reads an Excel workbook and processes its worksheets with an ADO recordset. I process the worksheet column names (first row of worksheet) as recordset fields, but some field names have embedded square brackets in them, e.g., "CFT Alkaloids [%]", which get transformed into parentheses: "CFT Alkaloids (%)". Is there a way to prevent this? My alternate solution is to process Excel using just the Excel object and walk through its cells, but my tests show this to be only half as fast as using ADO.
I use SQL Server 2000 with Excel 2000 and MDAC v2.7.
My code:
' Create reference to Excel application.
Set objExcel = CreateObject("Excel.Application"
' Open Excel Workbook
Set objWorkbook = objExcel.Workbooks.Open(strExcelPathFile)
' Process worksheets in workbook.
intNbrOfWorksheets = 0
For Each objSheet in objWorkbook.WorkSheets
intNbrOfWorksheets = intNbrOfWorksheets + 1
strExcelWorksheetName(intNbrOfWorksheets) = objSheet.Name
Next ' For Each objSheet in objWorkbook.WorkSheets
'Clean Up Excel Objects
Set objSheet = Nothing
objWorkbook.Close
Set objWorkbook = Nothing
objExcel.Quit
Set objExcel = Nothing
Set objConnectXLS = CreateObject("ADODB.Connection"
' Open connection to Excel datasource.
objConnectXLS.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strExcelPathFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes; IMEX=1"""
' Process worksheets.
For X = 1 To intNbrOfWorksheets
strWorksheetName = strExcelWorksheetName(X)
' Create command and recordset objects.
Set objCmd = CreateObject("ADODB.Command"
Set objRst = CreateObject("ADODB.Recordset"
' Set up command.
With objCmd
.ActiveConnection = objConnectXLS
.CommandText = "SELECT * FROM [" & strWorksheetName & "$]"
.CommandType = adCmdText
.CommandTimeout = 60
End With ' objCmd
' Execute query and return results to ADO recordset
With objRst
.CursorLocation = adUseClient
.CursorType = adOpenForwardOnly
.LockType = adLockReadOnly
.CacheSize = 100
.Open objCmd
intNbrOfRows = .RecordCount
intNbrOfColumns = .Fields.Count
Do Until .EOF
For X = 1 To intNbrOfColumns
MsgBox "Field name <" & .Fields(X - 1).Name & ">"
Next ' For X = 1 To intNbrOfColumns
.MoveNext
Loop ' Do Until .EOF
End With ' objRst
Set objRst = Nothing
Set objCmd = Nothing
Next ' For X = 1 To intNbrOfWorksheets
' Close Excel and destroy connection for current workbook.
objConnectXLS.Close
Set objConnectXLS = Nothing
I use SQL Server 2000 with Excel 2000 and MDAC v2.7.
My code:
' Create reference to Excel application.
Set objExcel = CreateObject("Excel.Application"
' Open Excel Workbook
Set objWorkbook = objExcel.Workbooks.Open(strExcelPathFile)
' Process worksheets in workbook.
intNbrOfWorksheets = 0
For Each objSheet in objWorkbook.WorkSheets
intNbrOfWorksheets = intNbrOfWorksheets + 1
strExcelWorksheetName(intNbrOfWorksheets) = objSheet.Name
Next ' For Each objSheet in objWorkbook.WorkSheets
'Clean Up Excel Objects
Set objSheet = Nothing
objWorkbook.Close
Set objWorkbook = Nothing
objExcel.Quit
Set objExcel = Nothing
Set objConnectXLS = CreateObject("ADODB.Connection"
' Open connection to Excel datasource.
objConnectXLS.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strExcelPathFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes; IMEX=1"""
' Process worksheets.
For X = 1 To intNbrOfWorksheets
strWorksheetName = strExcelWorksheetName(X)
' Create command and recordset objects.
Set objCmd = CreateObject("ADODB.Command"
Set objRst = CreateObject("ADODB.Recordset"
' Set up command.
With objCmd
.ActiveConnection = objConnectXLS
.CommandText = "SELECT * FROM [" & strWorksheetName & "$]"
.CommandType = adCmdText
.CommandTimeout = 60
End With ' objCmd
' Execute query and return results to ADO recordset
With objRst
.CursorLocation = adUseClient
.CursorType = adOpenForwardOnly
.LockType = adLockReadOnly
.CacheSize = 100
.Open objCmd
intNbrOfRows = .RecordCount
intNbrOfColumns = .Fields.Count
Do Until .EOF
For X = 1 To intNbrOfColumns
MsgBox "Field name <" & .Fields(X - 1).Name & ">"
Next ' For X = 1 To intNbrOfColumns
.MoveNext
Loop ' Do Until .EOF
End With ' objRst
Set objRst = Nothing
Set objCmd = Nothing
Next ' For X = 1 To intNbrOfWorksheets
' Close Excel and destroy connection for current workbook.
objConnectXLS.Close
Set objConnectXLS = Nothing