简体   繁体   English

如何将数据从多个 excel/csv 文件传输到 C# 中的字典

[英]How can one transfer data from multiple excel/csv files to a Dictionary in C#

I have 3 Excel files containing data related to Client details, Company of Stocks and Order Details of Stocks Purchase.我有 3 个 Excel 文件,其中包含与客户详细信息、股票公司和股票购买订单详细信息相关的数据。 I want to parse all the data into a Multi-layer Dictionary using C# and run "Sorting" and "Searching" Functions on the same.我想使用 C# 将所有数据解析为一个多层字典,并在同一个函数上运行“排序”和“搜索”函数。 I am a novice when it comes to C# and was wondering what would be the code for the same.我是 C# 的新手,想知道相同的代码是什么。

Data Eg: Stock Symbol Company Name S&P Sector AAPL Apple Inc. IT数据 例如:股票代码 公司名称 S&P Sector AAPL Apple Inc. IT

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

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 ...这是我用一些测试数据创建的工作簿......

在此处输入图片说明

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 .你说你是 C# 的新手,但是,为了使下面的工作正常工作,创建一个新的 .NET Framework 项目并添加 NuGet 包... Microsoft.Office.Interop.Excel I called the project InterExcelDotNet but you can change that to be whatever you want.我将项目称为 InterExcelDotNet,但您可以将其更改为任何您想要的。

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.现在,了解了以上内容后,您应该了解 C# 中的 Excel 库确实允许您像在 excel 中一样直接对工作簿执行操作,例如 SORT 和 FILTER。 That may be another way to achieve what you're wanting.这可能是实现您想要的另一种方式。

Sort种类

种类

AdvancedFilter高级过滤器

高级过滤器

I'm not sure if all of that helps or not but I hope it does.我不确定所有这些是否有帮助,但我希望它有帮助。

Good luck ...!祝你好运 ...!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM