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
0
votes
1 answer

Filehelpers reading data from external source

I am trying to read an external source CSV file with filehelpers. Here is my code: var engine = new FileHelperAsyncEngine(); using (engine.BeginReadFile("https://dl.dropboxusercontent.com/s/xxxyyyzzz/data.csv")) { …
user5602172
0
votes
2 answers

Using filehelpers ExcelStorage - Excel File not opening

I am using filehelpers ExcelStorage somewhat like this: ExcelStorage provider = new ExcelStorage(typeof(Img)); provider.StartRow = 2; provider.StartColumn = 1; provider.FileName = "Customers.xls"; provider.HeaderRows =…
hot33331
  • 805
  • 11
  • 25
0
votes
0 answers

How do I import an Excel data table using FileHelpers and ExcelNPOIStorage?

I'm attempting to import a simple Excel data table into a strongly typed IEnumerable in C#. I'm using FileHelpers ExcelNPOIStorage engine to do this, assuming this won't require Excel to be installed on the user's machine (as the deprecated…
Jonathan Shay
  • 916
  • 8
  • 12
0
votes
0 answers

Handling Carraige return in Filehelpers

Am using the excellent FileHelpers library to parse a number of different files. One of these files has (some) lines that look like this. id|name|comments|date 01|Abc|bla bla bla bla 02|Bcd|bla bla bla bla 03|Cde|bla bla \r\n bla bla And my class…
prasadd
  • 324
  • 2
  • 6
0
votes
1 answer

FileHelpers Tab Delimited Two Headers

I need to make a tab delimited text file from a spreadsheet: https://s3.amazonaws.com/seller-templates/ff/na/us/Flat.File.Listingloader.xls You will see that there are two headers: I am using the C# FileHelper library. I decided to create the…
Joseph Anderson
  • 4,114
  • 4
  • 45
  • 99
0
votes
1 answer

Create dynamic custom selector

I have MultiRecordEngine with a CustomSelector in which I'm trying to loop through many lines starting with different strings Instead of a long list of 'ifs' like this (which works fine): if (recordLine.Length == 0) return null; if…
rugydp
  • 18
  • 2
0
votes
2 answers

"The type does not exist in the namespace" when specifing custom FieldConverter

I'm trying to use my custom converter with FileHelpers v. 3.1.5 After trying: classBuilder.LastField.Converter.TypeName = typeof(NoValueConverter).ToString(); I get "The type or namespace name 'DN' could not be found". When I try t specify the…
0
votes
3 answers

FileHelpers without a type

Is there a way to use FileHelpers without a type? I don't know what comes in the CSV file, I just want to loop through all the cells. I just know that the first row will contain the column names and the others the values. Thanks EDIT: var engine =…
Timothy
  • 608
  • 3
  • 10
  • 24
0
votes
1 answer

Using FileHelpers to read CSV by column name instead of position

Given the following record definition [DelimitedRecord(",")] public class Line { public string FirstName; public string LastName; } And the following code to import the CSV var engine = new FileHelperEngine(); // Ignore header…
Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76
0
votes
2 answers

C# Add an element to a each position in a collection object

My goal is to write values to a preexisting .csv file stored in a collection. Currently, I am reading the values stored in the .csv file and storing them in a collection using the filerhelper library. But I want to add 4 column fields {Product…
danny taki
  • 450
  • 7
  • 26
0
votes
0 answers

FileHelpers + async-await?

I’m looking at using FileHelpers in a file reading/writing library. This library will be highly parallel and concurrent using async-await as well as Tasks. As I understand it, FileHelperAsyncEngine is asynchronous internally, but there’s no *Async…
Kristian Wedberg
  • 465
  • 4
  • 10
0
votes
1 answer

FileHelpers library escape separator when reading Stream

need help to figure out how handling following scenario. We have a .NET application using FileHelpers library to parse CSV files. We don't use physical files, instead we have a SQL server database where CSV files are stored in a VARBINARY field.…
Enrico Massone
  • 6,464
  • 1
  • 28
  • 56
0
votes
1 answer

How to cast object of type 'especific' to type 'FileHelpers.Events.INotifyRead in Multirecording

I'm trying to centralize all formatting and conversion rule in a single class especific. Using the interface INotifyRead(Of T As Class). When I implement the methods BeforeRead/AfterRead throws an exception: Unable to cast object of type 'Especific'…
cfasilva
  • 3
  • 4
0
votes
3 answers

Convert a List to csv file using FileHelpers

I have a class like so public class MyObject { public string Name { get; set; } public string Location {get; set; } } This is then converted to a list of the type above with data. I want to use filehelpers to convert my list object to a csv…
user20358
  • 14,182
  • 36
  • 114
  • 186
0
votes
1 answer

Include column headers in csv exported using filehelpers v2.0

How can I add a header row to the CSV output from FileHelper v2.0? I've seen listings with answers that must predate this version as things like FieldOrder and FieldTitle are not valid attributes and GetFileHeader is not a valid mathod for the…
StuartMc
  • 666
  • 7
  • 12