捆绑Microsoft.Office.Interop.Excel.dll作为资源不起作用

我在写一个使用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工作正常 – 在构build程序之后,我的程序不需要DLL在相同的文件夹中工作。 但是,可执行文件确实需要Microsoft.Office.Interop.Excel.dll在同一个文件夹中,即使我捆绑它就像捆绑System.Data.SQLite.dll。

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

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

可以消除对Interop程序集的需求。 这可能是你最好的select。

MSDN上的这篇文章应该有所帮助。

简而言之:

selectInterop库的参考。 在“属性”窗口中,确保Embed Interop Types属性设置为True。