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

使用.NET6-Windows时Excel-DNA未生成Addin.dna文件的解决方案咨询

.NET6环境下使用Excel-DNA的解决办法

1. 手动创建DNA文件存储Ribbon配置

直接在项目根目录新建<你的项目名>-Addin.dna文件,把Ribbon的XML代码和必要配置写入其中,示例代码如下:

<DnaLibrary Name="你的Excel加载项" RuntimeVersion="net6.0-windows">
  <ExternalLibrary Path="你的项目输出.dll" LoadFromBytes="true" />
  <CustomUI>
    <!-- 这里放置你的Ribbon XML代码 -->
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
      <ribbon>
        <tabs>
          <tab id="CustomTab" label="我的功能区">
            <group id="CustomGroup" label="我的工具">
              <button id="CustomButton" label="点击执行" onAction="YourNamespace.YourClass.YourMethod" />
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>
  </CustomUI>
</DnaLibrary>

右键该文件,在属性面板将复制到输出目录设置为“始终复制”,这样构建时文件会同步到bin目录,你也能直接在这个文件里持久维护Ribbon的XML配置。

2. 修改项目配置自动生成DNA文件

编辑项目的.csproj文件,在<Project>节点中添加一段MSBuild任务,让构建过程自动生成DNA文件:

<Target Name="GenerateDnaFile" AfterTargets="Build">
  <WriteLinesToFile File="$(ProjectDir)$(AssemblyName)-Addin.dna" Overwrite="true" Lines='@(DnaContent)' />
</Target>
<ItemGroup>
  <DnaContent Include='<DnaLibrary Name="$(AssemblyName) Addin" RuntimeVersion="net6.0-windows">' />
  <DnaContent Include='  <ExternalLibrary Path="$(AssemblyName).dll" LoadFromBytes="true" />' />
  <DnaContent Include='  <CustomUI>' />
  <DnaContent Include='    <!-- 可直接写Ribbon XML,也可用File属性引用外部XML文件 -->' />
  <DnaContent Include='    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">' />
  <DnaContent Include='      <!-- 你的Ribbon结构代码 -->' />
  <DnaContent Include='    </customUI>' />
  <DnaContent Include='  </CustomUI>' />
  <DnaContent Include='</DnaLibrary>' />
</ItemGroup>

每次构建完成后,项目根目录会自动生成DNA文件。你可以在<DnaContent>节点里直接编写Ribbon代码,也可以把Ribbon XML单独存为一个文件,用<CustomUI File="Ribbon.xml" />引用,管理起来更灵活。

3. 调整NuGet包配置触发自动生成

先确保安装的是最新版本的ExcelDna.AddIn NuGet包,旧版本对.NET6的支持可能存在缺陷。然后编辑.csproj文件,添加以下配置:

<PropertyGroup>
  <ExcelDnaGenerateDnaFile>true</ExcelDnaGenerateDnaFile>
  <ExcelDnaRuntimeVersion>net6.0-windows</ExcelDnaRuntimeVersion>
</PropertyGroup>

如果仍然无法生成文件,尝试清理项目后重新构建,缓存问题可能会导致生成异常。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 00:37:33