【问题标题】:How can one transfer data from multiple excel/csv files to a Dictionary in C#如何将数据从多个 excel/csv 文件传输到 C# 中的字典
【发布时间】:2019-04-29 00:54:49
【问题描述】:

我有 3 个 Excel 文件,其中包含与客户详细信息、股票公司和股票购买订单详细信息相关的数据。我想使用 C# 将所有数据解析为多层字典并在其上运行“排序”和“搜索”功能。我是 C# 的新手,想知道相同的代码是什么。

数据例如:股票代码 公司名称 S&P 行业 AAPL 苹果公司 IT

【问题讨论】:

  • 欢迎您!但是,您需要提供更多信息,我的朋友。也许阅读此stackoverflow.com/help/how-to-ask,然后重新调整您的问题以包含很多内容。
  • 我已经粘贴了需要存储在 C# 字典中的 excel 屏幕截图。任何有关代码的帮助将不胜感激:)
  • 干得好。但是直接将它们添加到问题中会更好。
  • 抱歉...你知道我在哪里可以找到代码吗?

标签: c# excel csv parsing dictionary


【解决方案1】:

我可能会找错树,但是根据您给我们的工作,我假设您想获取相关工作表中的数据矩阵,并从该数据中创建一个可枚举的数据列表相关类型,以便您可以对其执行排序、过滤等操作。如果这是您想要的,那么下面就是一个示例。

这是我用一些测试数据创建的工作簿...

您说您是 C# 的新手,但是,为了使以下工作正常进行,请创建一个新的 .NET Framework 项目并添加 NuGet 包... Microsoft.Office.Interop.Excel .我将项目称为 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);
        }
    }
}

现在,在为您提供了上述内容后,您应该了解 C# 中的 Excel 库确实允许您直接对工作簿执行操作,就像您可以在 Excel 中执行的操作一样,例如 SORT 和 FILTER。这可能是实现您想要的另一种方式。

排序

高级过滤器

我不确定所有这些是否有帮助,但我希望有帮助。

祝你好运……!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    相关资源
    最近更新 更多