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

439 questions
51
votes
6 answers

Column headers in CSV using fileHelpers library?

Is there a built-in field attribute in the FileHelper library which will add a header row in the final generated CSV? I have Googled and didn't find much info on it. Currently I have this: DelimitedFileEngine _engine = new…
Heinnge
  • 838
  • 3
  • 8
  • 14
16
votes
3 answers

FileHelpers: How to skip first and last line reading fixed width text

Code below is used to read fixed width uploaded file content text file using FileHelpers in ASP .NET MVC2 First and last line lengths are smaller and ReadStream causes exception due to this. All other lines have proper fixed width. How to skipt…
Andrus
  • 22,189
  • 50
  • 171
  • 330
12
votes
1 answer

Issue importing CSV file using FileHelpers

I'm using FileHelpers library to import csv files into the database table. I'm having an issue importing the file that has the field that starts with the number ('800NUMBER') in the file header. Code to import: DataTable data =…
OlegR
  • 194
  • 1
  • 9
12
votes
1 answer

Handling quotes with FileHelpers

I'm using the excellent FileHelpers library to import many csv files, but run into a problem. I have a csv file with these three example lines id,text,number 120,"good line this one",789 121,""not good" line", 4456 122,,5446 and this (example)…
edosoft
  • 16,270
  • 24
  • 75
  • 108
11
votes
2 answers

What is the latest production-ready version of the FileHelpers .NET library and where is it?

The SourceForge page - www.filehelpers.net - was last updated in 2007, and no downloads are available. The Github repo - github.com/MarcosMeli/FileHelpers - looks recent but the last tagged release is 2.1 There is a NuGet package -…
John Vance
  • 536
  • 4
  • 15
10
votes
1 answer

C# - Can FileHelper FieldConverter routines refer to other fields in the record?

I am using the excellent FileHelpers library to process a fixed-length airline schedule file. I have a date field, then a few fields later on in the record, a time field. I want to combine both of these in the FileHelpers record class, and know…
Pete
  • 101
  • 3
9
votes
2 answers

Skipping a column in FileHelper

Using the FileHelper library for .Net, can I somehow skip a number of columns from the source file? According to docs and samples, I have to add fields for all columns. Alas, I have an excel sheet with 216 columns to import, from which as few as 13…
MZywitza
  • 3,081
  • 1
  • 15
  • 11
9
votes
2 answers

FileHelpers and CSV: what to do when a record can expand unbounded, horizontally

I'm trying to parse this type of CSV file with FileHelpers: Tom,1,2,3,4,5,6,7,8,9,10 Steve,1,2,3 Bob,1,2,3,4,5,6 Cthulhu,1,2,3,4,5 Greg,1,2,3,4,5,6,7,8,9,10,11,12,13,14 I can't figure out how to parse this with FileHelpers. I would imagine I should…
Rob
  • 24,839
  • 30
  • 106
  • 150
9
votes
5 answers

How to export large SQL Server table into a CSV file using the FileHelpers library?

I'm looking to export a large SQL Server table into a CSV file using C# and the FileHelpers library. I could consider C# and bcp as well, but I thought FileHelpers would be more flexible than bcp. Speed is not a special…
mircea
  • 93
  • 1
  • 1
  • 3
8
votes
1 answer

Why does the returned DataTable has readonly columns in FileHelpers

I am wondering why filehelpers return readonly columns. I had a huge problem with them not updating values and could not figure out why. Now I have to have another loop to go through all the columns and change them to be not readonly. Is there a…
chobo2
  • 75,304
  • 170
  • 472
  • 780
8
votes
1 answer

FileHelpers - How to read in ® character?

I am reading in a CSV file and everything is working correctly. All fields are going to the correct places but it is converting ® to �. var engine = new FileHelperEngine(typeof(T)); return engine.ReadStream(new StreamReader(stream)) as T[]; Any…
Ryan Drost
  • 1,084
  • 9
  • 19
8
votes
1 answer

Filehelpers CSV parsing. How to use FieldQuoted attribute?

I have a CSV that looks like: a,b,c a,b,c a,"b,c",d I'm marking 2nd field in delimited class with attribute: [FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.AllowForRead)] public String ExchangeRate; But 3rd line still "b,c" being…
Sergejs
  • 2,390
  • 5
  • 30
  • 49
7
votes
1 answer

Is there a way to do field order using the FileHelpers library?

I downloaded FileHelpers from nuget but I am not sure if this feature does not exist or if I don't have the right version or what. I been looking around and it seems that FileHelpers may have an attribute to specify the field order. I downloaded…
chobo2
  • 75,304
  • 170
  • 472
  • 780
7
votes
4 answers

Ignoring properties in FileHelpers

I am using FileHelpers for exporting models to CSV. It has a [FieldNotInFile()] attribute which excludes fields when exporting, but I need to use properties as I need to some other attributes as well from another 3rd party library which only work…
mclaassen
  • 4,652
  • 3
  • 27
  • 51
6
votes
2 answers

Use Reflection to Build a Class (to build a dynamic FileHelper class)

Can I build a class as shown below dynamically using reflection? There are no methods, just public variables, some have custom attributes. Is the .Emit method required (from what I've seen, "Emit" looks a little challenging). I'm using software…
NealWalters
  • 14,090
  • 34
  • 109
  • 199
1
2 3
29 30