cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to read excel from OLEDB

JohanSörman
Level 5
Hello,

First of all, I never used OLEDB before. 

I'm trying to read a excel file into a db and then read it as collection.

My connection string looks like this:

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="&[Excel-file path]&";Extended Properties=""Excel 12.0 Xml;HDR=YES;"""

And my SQL querry in "Get Collection" action:

"SELECT * FROM [SHEET1$]"

is the syntax wrong or something else that could be it? 

Error message:

Internal : Could not execute code stage because exception thrown by code stage: Invalid argument.

Also saw this earlier but not anymore:

ERROR: Internal : Could not execute code stage because exception thrown by code stage: Could not find installable ISAM.

------------------------------
Johan Sörman
RPA Developer
Telia Company
Sweden
------------------------------
2 REPLIES 2

PvD_SE
Level 12
Hi Johan,

Indeed can OLEDB be a dream to work with or a pain in the behind to get started with. Once you get it working, it is both fast and able to handle larger volumes of data. Well worth the effort!

The order of all things OLEDB:
  1. Connect to the XL with action 'Set connection' and your connection string. You can find all about this here
    Mine usually look similar to this:
    Replace("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=XYZ;Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1;"";", "XYZ", [xl path])
  2. Wait a short while for things to fall into place. Usually 5 seconds does the trick for me
  3. Open OLEDB with action 'Open'
  4. Build your SQL statement, something like this: "select * from " & [xl sheet name] & " where " & [filter]
    With:
    'xl sheet name': "[" & [xl sheet name] & "$]"
    'filter': "(YEAR = '2022' and ProductCode = '123')"
  5. Run your SQL statement with action 'Get Collection'
  6. Wait a short while for things to happen. Again, 5 seconds works for me
  7. Close the connection with action 'Close'
'Known' issues:
  • Duplicate column names in your XL are not appreciated by OLEDB
  • OLEDB does not like illegal characters in XL column names (a dot in a column name is considered illegal, there are probably more)
Try this and see if it helps. Otherwise, there's more here.

As always, happy coding!
---------------------------
Paul
Sweden
Happy coding!
Paul, Sweden
(By all means, do not mark this as the best answer!)

MohammadNaveed
Level 4
In Connection String XML keyword should not be there .

I am posting my code here which i am using every day.

I written the Global Function code and i added 2 parameters to it, Full File Path and Sheet Name

Public Function ExcelSheetRead(ByVal filepath as String,sheetname as string) As DataTable
Dim dt As DataTable = New DataTable()
Dim Con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & filepath & ";Extended Properties=Excel 12.0;")
Con.Open()

Try
                                                
	Dim oCon As OleDbCommand = New OleDbCommand("Select * from [" & sheetname & "$]",Con)
	Dim adp As OleDbDataAdapter = New OleDbDataAdapter(OCon)
	adp.Fill(dt)
                                                
Catch e_ As Exception

End Try

	Con.Close()
	Return dt
	
End Function


35080.png