You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用MiniExcel.Query<dynamic>读取Excel报错及表头读取问题咨询

MiniExcel 1.34.2使用Query报错,无法读取Excel第一行表头问题

我在使用MiniExcel 1.34.2版本将Excel内容填充到DataGridView时遇到了问题:

  • 执行代码var records = MiniExcel.Query<dynamic>(fileName).ToList();时,抛出错误:System.InvalidOperationException: 'Object un-ignore properties count can't be 0'
  • 改用var records = MiniExcel.Query(fileName).ToList();后代码能正常运行,但这种写法会忽略Excel第一行的实际表头,改用A、B、C等列字母作为键名,不符合我的需求——我需要把Excel第一行作为表头,通过表头名称引用列,避免后续Excel列顺序或内容变更时出错。

我原本理解Query<dynamic>应该支持读取第一行作为表头的功能,但实际没生效,想知道这是库的bug还是我的操作有误。

最初尝试的代码

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;
    }
}

我期望通过表头名称直接引用列数据,生成包含国家及详情的表格。

修改后的代码(不理想的方案)

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;
    }
}

这段代码能运行,但会把Excel第一行的表头当作数据行处理,必须手动跳过第一行;而且只能用列字母(如A、C)引用数据,无法通过表头名称关联,一旦Excel列顺序或内容变更,代码就会出错,不符合我的需求。

内容的提问来源于stack exchange,提问作者Ssemakula Mukiibi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 19:53:16