5

My recent project requires me to read csv file. People direct me to filehelpers, since "you shouldn't reinvent the whell". However, the documentation really sucks, I can't seem to find a way to deal with optional quotation marks. Here is my csv:

2734000585,IDR,04/04/2016,04/04/2016,0000000,1010,SETOR TUNAI,"783275305006511 VENDY",,"820,000.00","5,820,000.00"

I am not the one who generated above csv, it's from bank. As you can see, their csv file sucks and have some serious issue. Some strings are enclosed by quotation marks, some are not. Furthermore, the currency values are encoded as string.

I made a class for it:

using System;
using FileHelpers;

[DelimitedRecord(",")]

public class ImporBii
{
    public long RekNum;

    public string Currency;

    [FieldConverter(ConverterKind.Date, "dd/MM/yyyy")]
    public DateTime TransDate;

    [FieldConverter(ConverterKind.Date, "dd/MM/yyyy")]
    public DateTime RecordDate;

    public string Unused1;

    public int TransCode;

    public string TransCodeStr;

    public string Keterangan;

    [FieldConverter(ConverterKind.Decimal, "#,##0.00")]
    public decimal Debet;

    [FieldConverter(ConverterKind.Decimal, "#,##0.00")]
    public decimal Kredit;

    [FieldConverter(ConverterKind.Decimal, "#,##0.00")]
    public decimal Saldo;
}

I thought filehelpers is clever enough to see the quotation marks by itself, but the result is total disaster. Please help me. :-(

Daniel Wu
  • 124
  • 1
  • 9
  • its difficult to read a csv (comma separated values) file which uses comma as a number separator. can you use excel file instead? – Dr. Stitch Jun 13 '16 at 07:51
  • EPPlus seems to be able to open this troublesome CSV just fine, but it requires me to create an intermediate Excel file, which is not elegant at all. I probably need to reinvent the wheel after all. – Daniel Wu Jun 13 '16 at 07:56

1 Answers1

4

You need to add [FieldQuoted()] to the fields that can have quotation

[DelimitedRecord(",")]
public class ImporBii
{
    public long RekNum;

    public string Currency;

    [FieldConverter(ConverterKind.Date, "dd/MM/yyyy")]
    public DateTime TransDate;

    [FieldConverter(ConverterKind.Date, "dd/MM/yyyy")]
    public DateTime RecordDate;

    public string Unused1;

    public int TransCode;

    public string TransCodeStr;

    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public string Keterangan;

    [FieldConverter(ConverterKind.Decimal, ".")]
    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public decimal? Drebet;

    [FieldConverter(ConverterKind.Decimal, ".")]
    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public decimal? Kredit;

    [FieldConverter(ConverterKind.Decimal, ".")]
    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public decimal? Saldo;
}
Marcos Meli
  • 3,468
  • 24
  • 29
  • Thanks. It works. I still can't make Debet, Kredit, and Saldo into decimal though. It seems that Filehelpers only supports decimal separator, it can't understand thousand separator. – Daniel Wu Jun 14 '16 at 03:37
  • @DanielWu You must go the MustRead docs in the converters section you can check the decimals and float converters http://www.filehelpers.net/mustread/ I just edited the answer – Marcos Meli Jun 14 '16 at 12:16
  • Using your code, some errors still occured, because _Debet_ and _Kredit_ are empty string sometimes, they makes Filehelpers fail to parse. I was able to fix it by myself, by changing `public decimal` to nullable `public decimal?`. Thanks a lot man. Case closed. :-D – Daniel Wu Jun 14 '16 at 13:47