Hutility

Reading CSV files

With HuLib, reading CSV files is even easier than it was in the first place. There is a class in HuLib called CSVFile. You can instantiate it with the name of the file you want to read. From there you can call Load() to populate the structure and read out values from this structure, or use the mapping framework.

Example: Creating a Lookup Dictionary

Here’s an example of creating a lookup dictionary:

Dictionary<string, string> custVendorLookup = Connection.GetOptFieldMap("CUSTTOSTORE");
string vendor = custVendorLookup["1100"]; // 0083

Example Model for CSV Mapping

This code creates a list of MightyLine objects (which have Mapping attributes):

public class MightyLine
{
    [Mapping(0)]
    public int Cust { get; set; }

    [Mapping(1)]
    public string CustomerName { get; set; }

    [Mapping(2)]
    public string Range { get; set; }

    [Mapping(3)]
    public string PartNumber { get; set; }
}

Generating Classes

Simply enter the name of the class you want to be generated, select an example CSV file you will be loading from, and hit Generate.

On the left, you have the model that will store the rows. On the right, you have code for a function that reads in a file to generate these objects. It even tries to figure out the type that you would want for the data (though be sure to verify it chose the correct one).

Leave A Comment

All fields marked with an asterisk (*) are required