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

使用ClosedXML导入Excel时遇方法未找到错误,请求解决

解决ClosedXML导入Excel时的"Method not found: DocumentFormat.OpenXml.OpenXmlElement.get_ChildElements()"错误

你遇到的这个错误是ClosedXML与依赖的DocumentFormat.OpenXml库版本不兼容导致的,下面是具体解决步骤:

问题复现代码

Imports ClosedXML.Excel 

Sub import()

    'Open the Excel file using ClosedXML.
    Using workBook As New XLWorkbook(file_pr)
        'Read the first Sheet from Excel file.
        Dim workSheet As IXLWorksheet = workBook.Worksheet(1)

        'Create a New DataTable.
        Dim dt As New DataTable()

        'Loop through the Worksheet rows.
        Dim firstRow As Boolean = True
        For Each row As IXLRow In workSheet.Rows()
            'Use the first row to add columns to DataTable.
            If firstRow Then
                For Each cell As IXLCell In row.Cells()
                    dt.Columns.Add(cell.Value.ToString())
                Next
                firstRow = False
            Else
                'Add rows to DataTable.
                dt.Rows.Add()
                Dim i As Integer = 0
                For Each cell As IXLCell In row.Cells()
                    dt.Rows(dt.Rows.Count - 1)(i) = cell.Value
                    i += 1
                Next

            End If
        Next
    End Using
End Sub

错误信息

'Method not found:'DocumentFormat.OpenXml.OpenXmlElementList
DocumentFormat.OpenXml.OpenXmlElement.get_ChildElements()'.'

解决步骤

  1. 清理现有冲突依赖
    打开NuGet包管理器控制台,执行以下命令卸载现有包:

    Uninstall-Package ClosedXML -RemoveDependencies
    Uninstall-Package DocumentFormat.OpenXml -RemoveDependencies
    

    这一步会彻底移除所有相关的包和依赖,避免版本残留。

  2. 重新安装兼容版本
    直接安装ClosedXML,NuGet会自动为你匹配并安装对应的DocumentFormat.OpenXml版本:

    Install-Package ClosedXML
    
  3. 验证依赖版本
    右键项目 → 管理NuGet程序包,查看已安装的包,确认DocumentFormat.OpenXml的版本是ClosedXML自动引入的版本(无需手动指定版本,避免再次冲突)。

额外提示

原代码中file_pr变量需要确保是有效的Excel文件路径(比如C:\test.xlsx),避免因路径问题引发其他错误,但当前的方法找不到错误和路径无关,核心还是依赖版本问题。

内容的提问来源于stack exchange,提问作者Ari's

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 09:47:18