MiniExcel.Query<dynamic>返回System.InvalidOperationException:“对象未忽略属性计数不能为0”并且无法获取标题

问题描述 投票:0回答:1

我正在尝试使用 Excel 文件中的内容填充 DataGridView。我正在使用 MiniExcel 库版本 1.34.2 来执行此操作。

当我使用

var records = MiniExcel.Query<dynamic>(fileName).ToList();

我收到错误:错误消息是:System.InvalidOperationException:'对象未忽略属性计数不能为 0'

但是,当我将其更改为

var records = MiniExcel.Query(fileName).ToList();
时,它就起作用了。 在第二个实例中,它忽略第一行中的标题并使用 A、B、C 等作为标题。我的目标是使用第一行作为我的标题

据我所知,这就是

var records = MiniExcel.Query<dynamic>(fileName).ToList();
应该做的事情,但事实并非如此。

这是一个错误还是我做错了什么?

这就是我正在尝试的:

using MiniExcelLibs;

private void FillRecords(string fileName, int noRecords)
{
    var records = MiniExcel.Query<dynamic>(fileName).ToList();
    int countRecs = 0;
    foreach (var record in records)
    {
        int rowIndex = testDataGridView.Rows.Add();
        testDataGridView.Rows[rowIndex].Cells["CountryName"].Value = record["name"];
        testDataGridView.Rows[rowIndex].Cells["Alpha3"].Value = record["alpha-3"];
        testDataGridView.Rows[rowIndex].Cells["Alpha2"].Value = record["alpha-2"];
        testDataGridView.Rows[rowIndex].Cells["CountryCode"].Value = record["country-code"];
        testDataGridView.Rows[rowIndex].Cells["CountryRegion"].Value = record["region"];
        testDataGridView.Rows[rowIndex].Cells["SubRegion"].Value = record["sub-region"];
        testDataGridView.Rows[rowIndex].Cells["IntermediateRegion"].Value = record["intermediate-region"];
        testDataGridView.Rows[rowIndex].Cells["RegionCode"].Value = record["region-code"];
        testDataGridView.Rows[rowIndex].Cells["SubRegionCode"].Value = record["sub-region-code"];
        testDataGridView.Rows[rowIndex].Cells["IntermediateRegionCode"].Value = record["intermediate-region-code"];
        countRecs++;
        if (noRecords > 0 && countRecs >= noRecords) return;
    }
}

我期待一个包含国家和详细信息的网格。

我将其更改为这样,但它不太理想,因为我想知道我引用了哪些列,以防 Excel 工作表更改顺序或内容。

private void FillRecords(string fileName, int noRecords)
{
    var records = MiniExcel.Query(fileName).ToList();
    int countRecs = 0;
    foreach (var record in records)
    {
        var dict = (IDictionary<string, object>)record;
        /*foreach (var key in dict.Keys)
        {
            testListBox.Items.Add($"Header: {key}");
        }*/
        if (countRecs > 0)
        {
            int rowIndex = testDataGridView.Rows.Add();
            testDataGridView.Rows[rowIndex].Cells["CountryName"].Value = dict["A"];
            testDataGridView.Rows[rowIndex].Cells["Alpha3"].Value = dict["C"];
            testDataGridView.Rows[rowIndex].Cells["Alpha2"].Value = dict["B"];
            testDataGridView.Rows[rowIndex].Cells["CountryCode"].Value = dict["D"];
            testDataGridView.Rows[rowIndex].Cells["CountryRegion"].Value = dict["F"];
            testDataGridView.Rows[rowIndex].Cells["SubRegion"].Value = dict["G"];
            testDataGridView.Rows[rowIndex].Cells["IntermediateRegion"].Value = dict["H"];
            testDataGridView.Rows[rowIndex].Cells["RegionCode"].Value = dict["I"];
            testDataGridView.Rows[rowIndex].Cells["SubRegionCode"].Value = dict["J"];
            testDataGridView.Rows[rowIndex].Cells["IntermediateRegionCode"].Value = dict["K"];
        }
        countRecs++;
        if (noRecords > 0 && countRecs >= noRecords) return;
    }
}

这有效,但它给了我第一行(应该是标题)作为我的数据的一部分,所以我被迫对其进行调整。

c#
1个回答
0
投票

我刚刚遇到了同样的例外。不确定原因是否与您的相同,但在我将类及其属性的访问修饰符从

internal
更改为
public
后,它就消失了。

© www.soinside.com 2019 - 2024. All rights reserved.