paulyoder / LinqToExcel

Use LINQ to retrieve data from spreadsheets and csv files
MIT License
1.06k stars 299 forks source link

Reading decimal from *.xlsx -> no decimal seperators #143

Closed Gekkenhuis closed 6 years ago

Gekkenhuis commented 6 years ago

Hello,

I've tried my luck on SO (https://stackoverflow.com/questions/50157719/linqtoexcel-reading-a-decimal-from-an-xlsx-file) but to no solution yet. Please excuse me for asking here (it's not a bug probably).

I've been fiddling around with this for a day and a half now since i've posted on SO. I'm missing something.

I'm trying to read an excel sheet with Linq to Excel. Everything works in order, except for decimals in the sheet. I've tried a couple of different approaches, none of which seem to work.

The column I'm trying to read is a currency column. My model is decimal. When I leave the column in excel on the Currency format, I get 0's from the linq query.

When I set the colum to number, I get values with no decimal seperator.

The testfile I created has a sheet called DecimalSheet. The column is called DecimalTest and contains a number; 4587,61

I've got a testmodel like so;

public class DecimalModel
{
    [ExcelColumn("DecimalTest")]
    public decimal DecimalTest { get; set; }
}

Querying the file is implemented like this:

var testFile = new ExcelQueryFactory(fileLocation)
{
    DatabaseEngine = LinqToExcel.Domain.DatabaseEngine.Ace,
    TrimSpaces = LinqToExcel.Query.TrimSpacesType.Both,
    UsePersistentConnection = false, 
    ReadOnly = true
};

var decimalTestModel = from tb in testFile.Worksheet<DecimalModel>("DecimalSheet")
                       select tb;

var lines = decimalTestModel.ToList();

The readout is like this: 45876100000000000M I've tried different approaches (see SO post), but nothing works. I'm not able to read the decimal in the dutch localization.

My development server is an english windows version, with english excel. The production server is a dutch windows server with dutch excel (Remote desktop farm for end-users). On the production server the readout is correct. My development machine doesn't work.

When I leave Excel open, it works on my development server. When i close Excel, it doesn't work.

Please advise,

Much obliged, Francois

mrworkman commented 6 years ago

This definitely sounds like a locale issue. I don't know how much effort was put into LinqTioExcel to support different locales.. out of curiosity, does it work if you use a . instead of a ,?

I know excel is influenced by the locale set in Windows. What are your Windows settings like for currency (see Control Panel\Clock, Language, and Region -> Region -> Additional Settings -> Currency) ? What about the numbers tab?

image

Gekkenhuis commented 6 years ago

Hello MrWorkman,

That did the trick. Perfect! Works like a charm now.

Thanks a lot,

Best regards, Francois