Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scientific notation when importing from Excel in .Net

Tags:

c#

.net

excel

oledb

I have a C#/.Net job that imports data from Excel and then processes it. Our client drops off the files and we process them. I don't have any control over the original file.

I use the OleDb library to fill up a dataset. The file contains some numbers like 30829300, 30071500, etc... The data type for those columns is "Text".

Those numbers are converted to scientific notation when I import the data. Is there anyway to prevent this from happening?

like image 733
ChrisDiRulli Avatar asked Jan 09 '09 21:01

ChrisDiRulli


People also ask

Why is Excel giving me scientific notation?

By default, when you enter a number over 12 digits in an Excel spreadsheet, it auto-corrects the number to scientific notation for brevity. For example, "879860004073" is converted to "8.7986E+11". When Excel exports the value to a CSV or Text file, it will export what you see, not the actual 12-digit value.

How do you force Excel to not use scientific notation?

Unfortunately excel does not allow you to turn this functionality off by default. However if you select your data, right click, and click "Format cells..." and choose Number you can stop excel from changing your data to scientific notation.


4 Answers

One workaround to this issue is to change your select statement, instead of SELECT * do this:

"SELECT Format([F1], 'General Number')  From [Sheet1$]"
 -or-
"SELECT Format([F1], \"#####\")  From [Sheet1$]"

However, doing so will blow up if your cells contain more than 255 characters with the following error: "Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done."

Fortunately my customer didn't care about erroring out in this scenario.

This page has a bunch of good things to try as well: http://www.dicks-blog.com/archives/2004/06/03/external-data-mixed-data-types/

like image 178
BA TabNabber Avatar answered Nov 15 '22 17:11

BA TabNabber


The OleDb library will, more often than not, mess up your data in an Excel spreadsheet. This is largely because it forces everything into a fixed-type column layout, guessing at the type of each column from the values in the first 8 cells in each column. If it guesses wrong, you end up with digit strings converted to scientific-notation. Blech!

To avoid this you're better off skipping the OleDb and reading the sheet directly yourself. You can do this using the COM interface of Excel (also blech!), or a third-party .NET Excel-compatible reader. SpreadsheetGear is one such library that works reasonably well, and has an interface that's very similar to Excel's COM interface.

like image 32
P Daddy Avatar answered Nov 15 '22 19:11

P Daddy


Using this connection string:

Provider=Microsoft.ACE.OLEDB.12.0; data source={0}; Extended Properties=\"Excel 12.0;HDR=NO;IMEX=1\"

with Excel 2010 I have noticed the following. If the Excel file is open when you run the OLEDB SELECT then you get the current version of the cells, not the saved file values. Furthermore the string values returned for a long number, decimal value and date look like this:

5.0130370071e+012
4.08
36808

If the file is not open then the returned values are:

5013037007084
£4.08
Monday, October 09, 2000
like image 36
johndsamuels Avatar answered Nov 15 '22 17:11

johndsamuels


If you look at the actual .XSLX file using Open XML SDK 2.0 Productivity Tool (or simply unzip the file and view the XML in notepad) you will see that Excel 2007 actually stores the raw data in scientific format.

For example 0.00001 is stored as 1.0000000000000001E-5

<x:c r="C18" s="11" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  <x:v>1.0000000000000001E-5</x:v>
</x:c>

Looking at the cell in Excel its displayed as 0.00001 in both the cell and the formula bar. So it not always true that OleDB is causing the issue.

like image 30
stoomm Avatar answered Nov 15 '22 18:11

stoomm