Questions tagged [filehelpers]

FileHelpers is a .net utility library to help applications manage flat file input and output.

FileHelpers is a utility library to help .NET framework applications manage flat file input and output. The bulk of the library lives in an assembly with a number of "engine" classes that are used to drive I/O along with attributes that are used to decorate classes in your application.

Your code uses these attributes to define records:

namespace OBG.FileHelpers.Tests
{
    [FixedLengthRecord(FixedMode.ExactLength)]
    internal class FixedRec1
    {
        [FieldFixedLength(10)]
        [FieldAlign(AlignMode.Left)]
        [FieldNullValue("n/a")]
        [FieldTrim(TrimMode.Both)]
        public String String10Field1;

        [FieldFixedLength(10)]
        [FieldConverter(ConverterKind.Date)]
        public DateTime DateField2;

        [FieldFixedLength(12)]
        [FieldConverter(typeof(MoneyFieldConverter))]
        public decimal MoneyField3;
    }
}

To read or write files, then, one of the FileHelper engines works with the records you've defined to manipulate, format, and validate data:

var recs = new List<FixedRec1>();
recs.Add(new FixedRec1 { String10Field1 = "abc", DateField2 = DateTime.Today, MoneyField3 = 123.45M });

// Show truncation of field 1
recs.Add(new FixedRec1 { String10Field1 = "abcdefghijklmnopqrstuvwxyz", DateField2 = DateTime.Today, MoneyField3 = 123.45M });

// Show null translation of field 1
recs.Add(new FixedRec1 { DateField2 = DateTime.Today, MoneyField3 = 123.45M });

// Show illegal value for field3
recs.Add(new FixedRec1 { String10Field1 = "abc", DateField2 = DateTime.Today, MoneyField3 = -0.00001M });

// To write, use: 
engine.WriteFile("FileOut.txt", recs.ToArray());

You can extend FileHelpers by constructing your own custom attributes, such as converters to handle formats not natively provided by FileHelpers.

FileHelpers is open source software released under the MIT.

Roslyn Analyzer

Best practices and quick fixes for the library:

Image

References

454 questions
5
votes
2 answers

How to define a default value for a field of a FileHelpers element class

I'm trying to load a CSV file (delims are ';' and quotes are '"'). I have successfully created everything (the wizard tool is awesome), but there's one thing that I can't find a solution for. Basically, I have an integer (System.Int32) column. In…
Paulius
  • 5,790
  • 7
  • 42
  • 47
5
votes
3 answers

Filehelpers ExcelStorage.ExtractRecords fails when first cell is empty

When the first cell of an excel sheet to import using ExcelStorage.ExtractRecords is empty, the process fail. Ie. If the data starts at col 1, row 2, if the cell (2,1) has an empty value, the method fails. Does anybody know how to work-around this?…
Sebastian
  • 1,491
  • 6
  • 20
  • 29
4
votes
2 answers

Filehelpers NullReferenceException when trying to write a null decimal value

When using the FileHelpers library I am getting a NullReferenceException when trying to write a .csv file. I have narrowed the problem down. Whenever I have a null decimal? it throws this exception. It works fine on reading, just not writing. I…
Erick
  • 853
  • 8
  • 17
4
votes
1 answer

How to make FileHelpers ignore columns after the defined columns? (i.e. ignore columns at the end)

Suppose I have the following FileHelpers Record definition: [DelimitedRecord(",")] [IgnoreEmptyLines] public class TestRecord { [FieldCaption("A")] [FieldQuoted(QuoteMode.OptionalForBoth)] public string A; [FieldCaption("B")] …
Denis
  • 11,796
  • 16
  • 88
  • 150
4
votes
1 answer

Class in c# (with filehelpers) - nullable strings giving an error when other nullable types aren't

I have a class (used by filehelpers) which gives me an error when I try to define a nullable string: public String? ItemNum; The error is: Error 1 The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the…
Glinkot
  • 2,924
  • 10
  • 42
  • 67
4
votes
2 answers

Why is "where T : class" required in this example?

Sample code : using System.Collections.Generic; using FileHelpers; .... private void Save(string destFilename, IEnumerable data) where T : class { var engine = new FileHelperEngine((typeof(T))); engine.HeaderText =…
Moe Sisko
  • 11,665
  • 8
  • 50
  • 80
4
votes
2 answers

.NET how to output csv from enumeration of anonymous type?

Using FileHelpers, I decorated a class with [DelimitedRecord(",")] and was going to output an enumeration of objects of that type as CSV. But, it didn't work because my class inherits from ActiveRecordLinqBase, which caused some problems. So, I…
Ronnie Overby
  • 45,287
  • 73
  • 267
  • 346
4
votes
2 answers

Using an array in the FileHelpers mapping class

I have been searching for a way to allow one element of my FileHelpers mapping class to be an array of specific length. For instance, I have a class like this: [DelimitedRecord(",")] public class Example { public string code; public int…
daharon
  • 2,000
  • 2
  • 17
  • 20
4
votes
2 answers

C# FileHelpers Nulls in Tab Delimited file

I'm parsing a tab delimited file using FileHelpers. The null values are being ignored after using the FieldNullValue attribute and I am ending up with the error log can't be found after the field 'filed name' at line 4 (the record has less fields,…
R Davies
  • 105
  • 1
  • 11
4
votes
1 answer

Ignore dollar currency sign ($) in decimal field with Filehelpers library

Suppose I have a Filehelpers class like this: [DelimitedRecord(",")] public class SomeRecord { public string Field1; public decimal Field2; } If I try to import a CSV record like this: hello,$4.00 I get a FileHelpers.ConvertException:…
dan-gph
  • 16,301
  • 12
  • 61
  • 79
4
votes
3 answers

FileHelpers: mixing Delimited and Fixed Length Records

Here I have to write out a file which records are Pipe Separated, using FileHelpers and C#. Great part of fields have variable length (so, my records would be [DelimitedRecord("|")] ). But some fields must have fixed length (they must have paddings,…
cezarlamann
  • 1,465
  • 2
  • 28
  • 43
4
votes
1 answer

FileHelpers nested quotes and commas - parsing error

I'm trying to parse a CSV file from hell, using the fantastic FileHelpers library. It's failing to handle a row of the form: "TOYS R"" US"," INC.""",fld2,fld3,"","","",fld7, FileHelper is very good…
trilson86
  • 939
  • 1
  • 9
  • 20
4
votes
2 answers

XamlParseException in PresentationFramework.dll when calling FileHelperEngine constructor

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll Additional information: 'The invocation of the constructor on type 'filehelpertest.MainWindow' that matches the specified…
Doochz
  • 1,039
  • 2
  • 14
  • 25
4
votes
3 answers

DataTable from FileHelpers Class -- type.GetProperties() returns empty array

I've successfully pulled a CSV file into the following class: [DelimitedRecord(",")] [IgnoreFirst(1)] // ignores first line of file, since it's a header public class Employee { public string EmployeeId; public string FirstName; public…
Nate
  • 1,330
  • 1
  • 13
  • 23
4
votes
1 answer

FileHelpers Csv reader - Failing to convert dd-mmm-yyyy DateTime Format

"NIFTY","13-Jun-2012","28-Jun-2012","7100.00" As one can notice that the Date format of above csv sample is - dd-mmm-yyyy but Date time format of File helper( may be, Default one ) is - dd-mm-yyyy while trying to convert csv file im getting…
panindra
  • 646
  • 2
  • 11
  • 33
1 2
3
30 31