Quantcast
Channel: Microsoft Dynamics NAV
Viewing all articles
Browse latest Browse all 64865

Blog Post: Reading Excel – Alternative

$
0
0

Most of us had already experienced the joy of getting an error while reading Excel through C/AL:

To support the <Language> (<Country>) language, install the Language Package for your version of Microsoft Office.

It seems that even by using automations, DotNet or the Excel buffer, you still get this nice error…
If you search the internet, you get a solution that states you need to change the culture info to en-US, but trust me, that is not easy in NAV. I believe I have tried doing this in the past, but never actually succeeded…

Today, I can offer you another method of reading your Excel file, without this pain in the ass.Since the problem only occurs on RTC as far as I know, this solution is using DotNet variables. I will also assume that all of you use Excel with a version of at least 2007.

Ok, but how? Maybe you have heard of the “database engine” in Microsoft Office products which is called Joint Engine Technology (or JET). It allows to read data from Excel as if excel was a database. The latest versions has been renamed to ACE.

By using OleDB, we can easily access the data inside the Excel file and read that data. This way, we do not need to have another language package installed nor active.

These are the variables I have declared to read the file:

OleDBConn DotNet 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Data.OleDb.OleDbConnection   OleDBCmd DotNet 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Data.OleDb.OleDbCommand   OleDBReader DotNet 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Data.OleDb.OleDbDataReader

I have these declared to run on ClientSide however for my example.
Next thing is to “open” the file and read the file.

OleDBConn := OleDBConn.OleDbConnection('Provider=Microsoft.ACE.OLEDB.12.0;Data Source='+ FileName + ';Extended Properties="Excel 12.0;HDR=No;IMEX=1"');    OleDBConn.Open();  OleDBCmd := OleDBCmd.OleDbCommand('select * from [' + SheetName + '$]',OleDBConn);  OleDBReader := OleDBCmd.ExecuteReader();    WHILE OleDBReader.Read() DO    MESSAGE(FORMAT(OleDBReader.GetValue(0)));  OleDBConn.Close();

The code above assumes that “FileName” contains the complete path to the file, and “SheetName” has the sheet name to read. It will return the value of each first column.
The GetValue function returns the value for each column, by supplying the column number, except it is zero based, meaning the first column is zero, the second is 1, and so on.

Some remarks:- The DataReader also has other functions as GetBoolean, GetDouble, …
- If a cell is empty the GetValue returns “null”, this could probably be checked with the Reader Function IsDBNull.
- If you have a sheet where the data is 2 columns (eg. X and Y) and only starts on row 10. I noticed that the first row read, is row 10. GetValue(0) will return column X
- In the connection string, you see the property HDR=No. If changed to Yes, it will skip reading the first row, this property states that you have a header data row.
- IMEX=1 implies to process cells as text for the OleDB Connection. In some cases, where you read the header, and the rest of the column contains numeric data, the connection could crash. However, supplying IMEX=1 would make sure it wouldn’t.

That’s about it. Enjoy it :)


Viewing all articles
Browse latest Browse all 64865

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>