I have an excel file that is being read with using ODBC driver with the following code:
filePath = "folder\myExcelFile.xlsx"
odbcPath = "DRIVER={Microsoft Excel Driver (*.xls)}; IMEX=1; HDR=YES; "&_
"Excel 8.0; DBQ=" & Server.MapPath(filePath) & ";"
'xlsx driver 2007+
if InStr( lcase(filePath),".xlsx" ) > 0 then
odbcPath= "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)}; "&_
"DBQ=" & Server.MapPath(filePath) & ";"
end if
'Excel processing
Dim objConn
Dim objRs
set objConn = Server.CreateObject("ADODB.Connection")
set objRs = Server.CreateObject("ADODB.Recordset")
objConn.Open odbcPath
strSQL = "SELECT * FROM A1:Q50000"
objRs.LockType = 1
set objRs = objConn.Execute(strSQL)
Do Until objRs.EOF
''Do stuff
myfield = objRs.Fields("fieldName")
''...other code...
Loop
objRs.Close
objRs.ActiveConnection = Nothing
set objRs = Nothing
objConn.Close
set objConn = Nothing
Inmediately after this, when I attempt to delete the file with the following:
Dim objFSO_Del
Set objFSO_Del = CreateObject("Scripting.FileSystemObject")
objFSO_Del.DeleteFile Server.MapPath(filePath), True
I get a permission denied
error, which shouldn't happen because my IUSR account has all the privileges (total control) to the folder where this action takes place. In fact when I comment the code posted above and attempt to delete the file, the operation is successful
I was thinking on fetching the excel records using:
set objExcel = CreateObject("Excel.Application")
set objWorkbook = objExcel.Workbooks.Open(Server.MapPath(filePath))
As this would allow me to have precise control to close the file and quit the Excel process.
Is there a way I can release and delete the excel file efficiently using the ODBC technique?. I've done a fair amount of research and haven't found a solution for now.
Thank you very much for the insight.
****Update****
I tested my original code again and it actually works on xls but not on xlsx files.
When I use ACE.OLEDB engine as suggested by oracle certified professional, I get no permission denied errors:
odbcPath = "Provider=Microsoft.ACE.OLEDB.12.0;" &_
"Data Source=""" & Server.MapPath(filePath) & """;" &_
"Extended Properties=Excel 8.0;"
However I think should be a way to do this using the original engine to make this work on xlsx files.