Home » excel » c# – The right way to lookup a value from within a table and fill it with data

c# – The right way to lookup a value from within a table and fill it with data

Posted by: admin March 9, 2020 Leave a comment


My current project is basically the conversion of an excel spreadsheet to a windows application. I came across a section of the worksheet using a table and vlookups. Assume the table looks like this (exactly like this only each quarter with an additional row added to the bottom):

enter image description here

I want to imitate the vlookups in C# and I can think of several different ways, however I am looking for what the “best” way is. By “best”, I mean easiest to code and most maintainable, I don’t mean speed, if it works in under 10 seconds its good enough. this is happening in a method that has the region name and Yr/Qrt passed in as strings. Using those, what data structures should I use to store the table data in and how can I do it in the least amount of code?


I have now recieved the actual excel file that I will be extracting this data from. Any advice on how to get only the “yearQtr” fields and the HRTC fields with C# would be greatly appreciated


just discovered that them machine running this application will not have excel. So the file shown above will have to be changed into another format before it is read in to the application(this will be handled by manually saving the file from excel as csv or xml or whatever format. So I guess my question is how to get the above described data out of a csv or xml

How to&Answers:

You may do it this way:

public class MyClass
    public List<string> columns;

Then in the form, you can do the look up like this:

List<MyClass> myValues;

public List<MyClass> LookUp(string value, int columnIndex)
    return this.myValues.Where(
                     input => input.columns[columnIndex] == value


I am currently using this architecture solution to meet my data needs. I am creating a new table every time I need it (like I said in the question, speed doesn’t matter). The table is only for a specific region per table. As you can tell, I haven’t figured out the Excel data extraction part yet.

class Table
        Dictionary<String, double> _regionTimeValues;
        String _region;

        public Table(String region)
            _regionTimeValues = new Dictionary<string, double>();
            _region = region;

        private void suckInValues()
            //Go find the File, get the appropriate Values
            //for each value found

        internal double locateRelevantValue(string yearQuarter)
            double locatedValue = 0.0;
            _regionTimeValues.TryGetValue(yearQuarter,out locatedValue);
            return locatedValue;