2
votes

I'm using the Linq-To-Excel package to read a CSV file. I created a class for holding the results:

public class ReconciliationLineLinqToExcel
{
    public string OperatingUnit { get; set; }
    public Int64 CustomerTenantId { get; set; }
    public string CustomerName { get; set; }
    public string SyndicationPartnerSubscriptionNumber { get; set; }
    public string DurableOfferId { get; set; }
    public DateTime SubscriptionStartDate { get; set; }
    public DateTime SubscriptionEndDate { get; set; }
    public DateTime ChargeStartDate { get; set; }
    public DateTime ChargeEndDate { get; set; }
    public string ChargeType { get; set; }
    public decimal UnitPrice { get; set; }
    public int Quantity { get; set; }
    public decimal Amount { get; set; }
    public decimal TotalOtherDiscount { get; set; }
    public decimal Subtotal { get; set; }
    public decimal Tax { get; set; }
    public decimal TotalForCustomer { get; set; }
    public string Currency { get; set; }
}

Each property is a column in the CSV file.

Header row of the CSV:

OperatingUnit,CustomerTenantId,CustomerName,MpnId,OrderId,SubscriptionId,SyndicationPartnerSubscriptionNumber,OfferId,DurableOfferId,OfferName,SubscriptionStartDate,SubscriptionEndDate,ChargeStartDate,ChargeEndDate,ChargeType,UnitPrice,Quantity,Amount,TotalOtherDiscount,Subtotal,Tax,TotalForCustomer,Currency

This is a sample row in the CSV file as input: (i've censored some data)

e588f1c2-69f1-4b78-3c4d-9b844d03c18e,"1338338173","COMPANY NAME",1382187,"567172081122654669","p3rITwAAAABBAAIA","68752F49-D9CA-4870-8955-3EEB26E37068","B0D93AE5-400E-4CBC-A465-D12B9FFA75AB","031C9E47-4845-4248-838E-778FB1D2CC05","OFFICE 365 BUSINESS PREMIUM",11/04/2015 00:00:00,11/23/2016 00:00:00,11/23/2015 00:00:00,12/22/2015 00:00:00,"CYCLE FEE",8.44,8,67.52,0,67.52,0,67.52,"EUR"

Using this code I open the CSV file and read the lines:

var csv = new ExcelQueryFactory(filename);
var data = csv.Worksheet<ReconciliationLineLinqToExcel>(0);

When I iterate the data variable and look up this row, all the decimal values are missing the decimal separator. for example the property 'Amount' has a value of 6752, instead of 67.52

This goes for all the decimal values in every row. I thought it might be related to some culture issue when parsing it to a decimal, so i tried to change the type of the property to string. but then i got a value of "6752", so no luck.

Does anyone has a clue about what i'm missing here, or is this some bug?

2
are trying to convert to XLS, or just read in the csv data? if you are just trying to read it in then Linq2Excel is probably overkill for your solutionChris McGrath

2 Answers

0
votes

The very first thing in your document is the OperatingUnit. You've defined it as a string but your CSV doesn't have quotes around it. The 2nd thing is an Int64, as you've defined, but you have quotes around it.

Make sure your data types actually match or else you'll surely get unexpected results.

-1
votes

If you're not stuck with Linq-To-Excel, you could try LINQ to CSV. It's pretty easy to setup (5 - 10 minutes) and maybe it will get you around your issue. It's powerful enough to handle certain special characters like commas, quotes, and decimals.

Here's a screenshot of a test spreadsheet I generated with some decimals.

LINQ to CSV with decimals

I read these decimals all the way from another CSV file; they are not hard-coded.