Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Losing precision while reading value from Excel

Tags:

c#

excel

oledb

I need to read the values from Excel to DataSet using OleDb Connection. Below is my code.

OleDbCommand excelCommand = new OleDbCommand(); 
OleDbDataAdapter    excelDataAdapter = new OleDbDataAdapter();
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=" + filelocation + @";Extended Properties=""Excel 8.0; IMEX=1;HDR=NO;""";
OleDbConnection excelConn = new OleDbConnection(connectionString);
excelConn.Open();
excelCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", excelConn);
excelDataAdapter.SelectCommand = excelCommand;
excelDataAdapter.Fill(ds);

While reading I am losing precision in my cell.

E.g.: Excel Cell has value -0.46256613817052

My DataSet has value -0.4625661

Most of my cells in the Excel sheet are of Number type.

How to solve this? Please help.

like image 491
Sarath Avatar asked Mar 12 '26 21:03

Sarath


1 Answers

According to this answer for Microsoft.Jet.OLEDB.4.0 rounding excel data? , if you need to get exact values, you need to use visual studio tools for office.

I have solved the issue using Excel Data Reader/

like image 171
Sarath Avatar answered Mar 15 '26 09:03

Sarath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!