Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional quotation marks in filehelpers

Tags:

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. :-(

like image 587
Daniel Wu Avatar asked Jun 13 '16 07:06

Daniel Wu


1 Answers

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;
}
like image 197
Marcos Meli Avatar answered Sep 28 '22 02:09

Marcos Meli