【问题标题】:Bundling Microsoft.Office.Interop.Excel.dll as a resource not working将 Microsoft.Office.Interop.Excel.dll 捆绑为资源不起作用
【发布时间】:2011-06-21 13:16:34
【问题描述】:

我正在编写一个使用 SQLite 和 Excel 的 C# 程序。当然,我需要 System.Data.SQLite.dll 和 Microsoft.Office.Interop.Excel.dll。我希望我的程序是单个 exe,所以我想将这些 DLL 捆绑在程序集中。这是我的 .csproj 文件:

<Project DefaultTargets="Compile"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
    <appname>AppName</appname>
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>

<ItemGroup>
    <CSFile Include="*.cs"/>
</ItemGroup>

<ItemGroup>
    <DLLResource Include="System.Data.SQLite.dll"/>
    <DLLResource Include="Microsoft.Office.Interop.Excel.dll"/>

    <DLLReference Include="System.Data.SQLite.dll"/>
    <DLLReference Include="Microsoft.Office.Interop.Excel.dll"/>
</ItemGroup>

<ItemGroup>
    <BundledResource Include="std_db_build.xml"/>
    <BundledResource Include="std_report_make.xml"/>
</ItemGroup>

<Target Name="Compile">
    <CSC
            Sources="@(CSFile)"
            Resources="@(DLLResource);@(BundledResource)"
            References="@(DLLReference)"
            OutputAssembly="$(appname).exe">
    </CSC>
</Target>

<Target Name="Run">
    <Exec Command="start $(COMSPEC) /k &quot;$(PathTo)$(appname).exe &amp; pause>null &amp; exit&quot;" />
</Target>

<Target Name="Clean">
    <Delete Files="$(appname).exe"/>
</Target>
</Project>

现在,这对 System.Data.SQLite.dll 工作正常 - 构建程序后,我的程序不需要 DLL 位于同一文件夹中即可工作。但是,可执行文件确实要求 Microsoft.Office.Interop.Excel.dll 位于同一个文件夹中,即使我将其捆绑为就像捆绑 System.Data.SQLite.dll 一样。

现在,我知道 Microsot.Office.Interop.Excel.dll 在程序集中,因为它将文件大小从 928k 增加到 1952k - 增加了 1024k,这正是 Microseft.Office.Interop 的大小。 Excel.dll。所以我假设即使 DLL 在程序集中,程序的某些部分仍将其作为单独的文件查找。

那么,我做错了什么?我该如何解决?

【问题讨论】:

    标签: c# .net excel dll interop


    【解决方案1】:

    消除对互操作程序集的需求是可能的。这可能是您最好的选择。

    MSDN 上的这个 article 应该会有所帮助。

    简而言之:

    为您的 Interop 库选择参考。在“属性”窗口中,确保“嵌入互操作类型”属性设置为 True。

    【讨论】:

    • 谢谢,但我没有使用VS,我无法弄清楚如何在MSBuild项目文件中做到这一点......
    • 看起来这是 .NET 4.0 的语言功能,而您使用的是 3.5。除非您可以选择升级,否则此方法对您不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 2016-03-15
    • 2017-12-16
    相关资源
    最近更新 更多