I have 3 Excel files containing data related to Client details, Company of Stocks and Order Details of Stocks Purchase. I want to parse all the data into a Multi-layer Dictionary using C# and run "Sorting" and "Searching" Functions on the same. I am a novice when it comes to C# and was wondering what would be the code for the same.

Data Eg: Stock Symbol Company Name S&P Sector AAPL Apple Inc. IT

enter image description here

enter image description here

enter image description here

1 Answers

0
Brad Dixon On Best Solutions

I could be barking up the wrong tree but with what you've given us to work with, I'm assuming you want to take the data matrix in the relevant worksheet and from that data, create an enumerable list of data with the relevant type so you can perform operations over it like sorting, filtering, etc. If that's what you want then the below is an example of that.

This is the workbook I created with some test data ...

enter image description here

You said you're a novice with C#, but, to make the below work, create a new .NET Framework project and add the NuGet package ... Microsoft.Office.Interop.Excel. I called the project InterExcelDotNet but you can change that to be whatever you want.

using Microsoft.Office.Interop.Excel;
using System.Collections.Generic;
using System.Linq;

namespace ExcelInteropDotNet
{
    public class CompanyStockInfo
    {
        public string StockSymbol { get; set; }
        public string CompanyName { get; set; }
        public string SPSector { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Change the below variabes to the relevant values for your needs.
            string workbookName = @"c:\temp\Source Data.xlsx";
            string worksheetName = "CompanyStockData";

            // Create a new list with the type being the CompanyStockInfo type.
            var companyStockInfoList = new List<CompanyStockInfo>();

            // Create an instance of Excel, open the workbook, fetch the sheet and then
            // find the last row in column A.
            var xlApplication = new Application();
            var xlWorkbook = xlApplication.Workbooks.Open(workbookName, ReadOnly: true);
            var xlSrcSheet = xlWorkbook.Worksheets[worksheetName] as Worksheet;
            var lastRow = xlSrcSheet.Cells[xlSrcSheet.Rows.Count,1].End[XlDirection.xlUp].Row;

            // There may be a better way to do this but essentially, the below will loop through
            // all cells from the 2nd row to the last row and create a new item in the list
            // that stores all of the data.
            for (long row = 2; row <= lastRow; row++)
            {
                companyStockInfoList.Add(new CompanyStockInfo()
                {
                    StockSymbol = (xlSrcSheet.Cells[row, 1] as Range).Text,
                    CompanyName = (xlSrcSheet.Cells[row, 2] as Range).Text,
                    SPSector = (xlSrcSheet.Cells[row, 3] as Range).Text
                });
            }

            xlApplication.Quit();

            // You can use Linq to sort and search the list for the data you're wanting to
            // get your hands on.

            // Will filter all entries that have Inc. in the company name.
            var filteredList = companyStockInfoList.Where(item => item.CompanyName.Contains("Inc."));

            // Orders all entries by the company name in alphabetical order.
            var orderedList = companyStockInfoList.OrderBy(item => item.CompanyName);
        }
    }
}

Now, having given you the above, you should understand that the Excel library in C# does allow you to perform operations over the workbook directly like you can do within excel, like SORT and FILTER. That may be another way to achieve what you're wanting.

Sort

Sort

AdvancedFilter

AdvancedFilter

I'm not sure if all of that helps or not but I hope it does.

Good luck ...!