使用Spreadsheet Light v3.5.0与DocumentFormat.OpenXml v3.0.0时为何触发System.InvalidProgramException?
问题原因与解决方案
原因
SpreadsheetLight 3.5.0依赖DocumentFormat.OpenXml 2.x系列版本,而DocumentFormat.OpenXml 3.0.0属于重大版本更新,包含API破坏性变更,导致SpreadsheetLight调用旧版API时出现反射绑定失败的MissingMethodException。
解决方案
降级DocumentFormat.OpenXml
直接将项目中的DocumentFormat.OpenXml版本回退到v2.20.0(已验证可正常运行),修改项目文件:<PackageReference Include="DocumentFormat.OpenXml" Version="2.20.0" />等待SpreadsheetLight适配
等待SpreadsheetLight发布兼容DocumentFormat.OpenXml 3.0.0的新版本,届时升级SpreadsheetLight即可解决兼容性问题。
问题场景确认
- 触发异常的极简代码:
using SpreadsheetLight; var template = new SLDocument(); - 异常截图:

- 项目配置:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="DocumentFormat.OpenXml" Version="3.0.0" /> <PackageReference Include="SpreadsheetLight" Version="3.5.0" /> </ItemGroup> </Project>
内容的提问来源于stack exchange,提问作者Dave Moran
相关产品推荐
相关产品推荐

