Excel JET Driver strange behaviour

37 views Asked by At

I'm getting some odd behaviour when I use the MS JET driver to read from a workbook. I have three columns of numerical data on the main worksheet in the workbook and the last column originally had some numerical values replaced with hyphens (-). When I read the data into a .NET DataTable, I noticed that all the numerical values in this column were read using the formatting defined for that cell and not the actual underlying value.

Eg. I got 1.79 instead of 1.78565 because that cell is formatted to 2DP.

If I delete the hyphens so that the cells are blank, I still get the same issue. The two other columns in the sheet are being read correctly presumably because they were never pasted in with hyphens so I assume its some sort of formatting issue on the sheet that I'm not aware of.

Now the strange behaviour that I find is when I open the workbook on my machine and try reading the file again it then appears to read the underlying (correct) value.

Any ideas what might be causing this?

Here is the offending code:

$connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=`"$inputFilePath`";Extended Properties=`"Excel 12.0;HDR=YES;IMEX=1`";"
$conn = New-Object System.Data.OleDb.OleDbConnection($connectionString)
$conn.open() 

[System.Data.DataTable] $dtOutput = New-Object System.Data.DataTable
[System.Data.DataTable] $dtReturns = New-Object System.Data.DataTable

[void]$dtOutput.Columns.Add("EntityId")
[void]$dtOutput.Columns.Add("Date")
[void]$dtOutput.Columns.Add("ReturnValue")

$sql = "SELECT * FROM [Sheet1$]"
$cmdReturns = New-Object System.Data.OleDb.OleDbCommand($sql, $conn)
$daReturns = New-Object system.Data.OleDb.OleDbDataAdapter($cmdReturns)

[void]$daReturns.Fill($dtReturns)

$dtReturns is the DataTable being populated that contains the values.

0

There are 0 answers