visual-studio 如何从 Visual Studio C# 使用 Office?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/401637/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Office from Visual Studio C#?
提问by Ian Boyd
The technique for adding a reference to the COM interop of Office in Visual Studio is to go to:
在 Visual Studio 中添加对 Office 的 COM 互操作的引用的技术是去:
- References
- Add Reference
- Select the COMtab
- Select Microsoft Office 11.0 Object Library
- 参考
- 添加参考
- 选择COM选项卡
- 选择Microsoft Office 11.0 对象库
And magically named reference appears:
出现神奇命名的引用:
Microsoft.Office.Core
The Project.csprojfile shows the details of the reference:
该Project.csproj文件显示了参考的详细信息:
<COMReference Include="Microsoft.Office.Core">
   <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
   <VersionMajor>2</VersionMajor>
   <VersionMinor>3</VersionMinor>
   <Lcid>0</Lcid>
   <WrapperTool>primary</WrapperTool>
   <Isolated>False</Isolated>
</COMReference>
And the project is checked into source control and all is well.
并且该项目已签入源代码管理并且一切正常。
Then a developer with Office 2007gets the project from source control, and cannot build it because such a reference doesn't exist.
然后,使用Office 2007的开发人员从源代码管理中获取项目,并且无法构建它,因为这样的引用不存在。
He (i.e. me) checks out the .csproj file, deletes the reference to
他(即我)检查了 .csproj 文件,删除了对
Microsoft Office 11.0 Object Library
and re-adds the COM reference as
并将 COM 引用重新添加为
Microsoft Office 12.0 Object Library
And magically a named reference appears:
并且神奇地出现了一个命名引用:
Microsoft.Office.Core
The Project.csprojfile shows the details of the reference:
该Project.csproj文件显示了参考的详细信息:
<COMReference Include="Microsoft.Office.Core">
  <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
  <VersionMajor>2</VersionMajor>
  <VersionMinor>4</VersionMinor>
  <Lcid>0</Lcid>
  <WrapperTool>primary</WrapperTool>
  <Isolated>False</Isolated>
</COMReference>
And the project is checked into source control and all is well.
并且该项目已签入源代码管理并且一切正常。
Then a developer with Office 2003gets the project from source control, and cannot build it because such a reference doesn't exist.
然后,使用Office 2003的开发人员从源代码管理中获取项目,并且无法构建它,因为这样的引用不存在。
He (i.e. not me) checks out the .csproj file, deletes the reference to
他(即不是我)检查了 .csproj 文件,删除了对
Microsoft Office 12.0 Object Library
and re-adds the COM reference as
并将 COM 引用重新添加为
Microsoft Office 11.0 Object Library
And magically a named reference appears:
并且神奇地出现了一个命名引用:
Microsoft.Office.Core
The Project.csprojfile shows the details of the reference:
该Project.csproj文件显示了参考的详细信息:
<COMReference Include="Microsoft.Office.Core">
  <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
  <VersionMajor>2</VersionMajor>
  <VersionMinor>3</VersionMinor>
  <Lcid>0</Lcid>
  <WrapperTool>primary</WrapperTool>
  <Isolated>False</Isolated>
</COMReference>
And the project is checked into source control and all is well.
并且该项目已签入源代码管理并且一切正常。
Then the project is built, pressed onto CDs, and sent to the customers who have Office 2007.
然后构建项目,将其压在CD 上,然后发送给拥有Office 2007的客户。
And all is not well.
而且一切都不好。
In the olden days (i.e. before .NET dll hell), we would reference the Office objects using a version independant ProgID, i.e.:
在过去(即在 .NET dll 地狱之前),我们会使用与版本无关的 ProgID来引用 Office 对象,即:
"Excel.Application"
which resolves to a clsidof the installed Office, e.g.
它解析为已安装 Office的clsid,例如
{00024500-0000-0000-C000-000000000046}    
of which a class is then constructed using a call to COM (language-netural pseudo-code):
然后使用对 COM 的调用(语言网络伪代码)构造一个类:
public IUnknown CreateOleObject(string className)
{
    IUnknown unk;
    Clsid classID = ProgIDToClassID(className);
    CoCreateInstance(classID, null, 
          CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, 
          IUnknown, out unk);
    return unk;
}
Questions
问题
1) What is the approved technique to automate the installed Office applications?
1) 使安装的 Office 应用程序自动化的已批准技术是什么?
2) What are the Office 2003 Primary Interop Assembliesuseful for?
2) Office 2003 主要互操作程序集有什么用?
3) If i use the Office 2003 Primary Interop Assemblies, do i have to have office 2003 installed?
3) 如果我使用 Office 2003 Primary Interop Assemblies,我是否必须安装 Office 2003?
4) If i build with the Office 2003 Primary Interop Assemblies, are my customers tied to Office 20003 forever?
4) 如果我使用 Office 2003 主互操作程序集进行构建,我的客户是否会永远绑定到 Office 20003?
5) Are there any Office 2007Primary Interop Assemblies?
5) 是否有任何Office 2007主要互操作程序集?
6) If i install the Office 2007 Primary Interop Assemblies do i have to have Office 2007 installed?
6) 如果我安装 Office 2007 主互操作程序集,我是否必须安装 Office 2007?
7) What is wrong with using standard COM interop to drive Excel, Word, or Outlook? e.g.:
7) 使用标准 COM 互操作来驱动 Excel、Word 或 Outlook 有什么问题?例如:
[ComImport]
[Guid("00024500-0000-0000-C000-000000000046")]
public class Excel
{
}
8) What is one achieving when one adds a
8) 当一个人添加一个时,什么是一个实现
- Referenceto items on the COM tab,
- as opposed to using [ComImport],
- as opposed to using the Office 2007 Primary Interop Assemblies?
- 参考COM 选项卡上的项目,
- 与使用 [ComImport] 相反,
- 与使用Office 2007 主要互操作程序集相反?
9) Is adding a reference using the COM tabidentical to using COM interop, except that it needs a type librarybefore you can see it?
9) 使用COM 选项卡添加引用是否与使用COM interop相同,只是它需要一个类型库才能看到?
10) Are the Office 2003 Primary Interop Assemblies backwards and forwards compatible with: - Office 14 - Office 2007 - Office 2003 - Office XP - Office 2000 - Office 97 - Office 95
10) Office 2003 主互操作程序集是否前后兼容: - Office 14 - Office 2007 - Office 2003 - Office XP - Office 2000 - Office 97 - Office 95
If a customer, and a developer, installs a new version of Office, will it still work?
如果客户和开发人员安装了新版本的 Office,它是否仍然有效?
11) Do we have to ship the Office 2003 Primary Interop Assemblies with our application?
11) 我们是否必须随应用程序一起提供 Office 2003 主互操作程序集?
12) Does the customer have to install the Office 2003 Primary Interop Assemblies before they can use our application?
12) 客户在使用我们的应用程序之前是否必须安装 Office 2003 Primary Interop Assemblies?
13) If a customer installs the Office 2003 Primary Interop Assemblies do they have to have Officeinstalled?
13) 如果客户安装 Office 2003 主要互操作程序集,他们是否必须安装Office?
14) If a customer installs the Office 2003 Primary Interop Assemblies do they have to have Office 2003installed?
14) 如果客户安装 Office 2003 主互操作程序集,他们是否必须安装Office 2003?
15) Are the Office 2003 Primary Interop Assembles a free, lite, redistributable version of Office 2003?
15) Office 2003 Primary Interop Assembles 是 Office 2003 的免费、精简、可再发行版本吗?
16) If my development machine has Office 2007, can i use the Office 2003 PIAs, and ship to a customer with Office XP installed?
16) 如果我的开发机器有 Office 2007,我可以使用 Office 2003 PIA 并运送给安装了 Office XP 的客户吗?
采纳答案by Ian Boyd
The answer is to "Copy Local" whatever assembly dll you get for the interop. Once you have the assembly dll in your output folder, add a reference to it, and check it into source control.
答案是“复制本地”您为互操作获得的任何程序集 dll。在输出文件夹中有程序集 dll 后,添加对它的引用,并将其签入源代码管理。
Now everyone has the referenced assembly dll.
现在每个人都有引用的程序集dll。
回答by jcollum
Wow that's a huge number of questions. I think that in general if your app is using the PIAs then you're assuming that your target audience has some version of Office installed. The PIAs will be installed in the GAC when the target user installs Office. If they don't have Office installed then why are you targeting Office?
哇,问题太多了。我认为一般来说,如果您的应用程序使用 PIA,那么您就假设您的目标受众安装了某个版本的 Office。当目标用户安装 Office 时,PIA 将安装在 GAC 中。如果他们没有安装 Office 那么你为什么要针对 Office?
Yes, the Office dlls are the correct way to automate Office. There's a list of the assemblies here, including some for Office 2007.
是的,Office dll 是自动化 Office 的正确方法。还有的组件列表在这里,其中包括一些为Office 2007。
回答by Jonno
An old thread, and probably most people would be happy with CopyLocal=True, however here's another way.. Use both(or more..? thinking Office 2010 if the problem still exists..) references in your project files, and either ignore or just tell MSBuild to ignore the "MSB3284" warning (Library not found). So include this in your .csproj file:
一个旧线程,可能大多数人都会对 CopyLocal=True 感到满意,但是这是另一种方式..在您的项目文件中同时使用(或更多..?如果问题仍然存在,请考虑 Office 2010..)引用,或者忽略或者只是告诉 MSBuild 忽略“MSB3284”警告(未找到库)。因此,将其包含在您的 .csproj 文件中:
<COMReference Include="Microsoft.Office.Core">
   <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
   <VersionMajor>2</VersionMajor>
   <VersionMinor>3</VersionMinor>
   <Lcid>0</Lcid>
   <WrapperTool>primary</WrapperTool>
   <Isolated>False</Isolated>
</COMReference>
Followed by:
其次是:
<COMReference Include="Microsoft.Office.Core">
   <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
   <VersionMajor>2</VersionMajor>
   <VersionMinor>4</VersionMinor>
   <Lcid>0</Lcid>
   <WrapperTool>primary</WrapperTool>
   <Isolated>False</Isolated>
</COMReference>
I would be interested to see if Microsoft provides a NuGet library for this - just to get everyone on to the same approach. I think that would remove the necessity for people to search the web for these answers... I believe this would be against Microsoft Office's license so they are the only ones to supply it.
我很想知道微软是否为此提供了一个 NuGet 库——只是为了让每个人都采用相同的方法。我认为这将消除人们在网络上搜索这些答案的必要性......我相信这将违反 Microsoft Office 的许可,因此他们是唯一提供它的人。
BTW with copy local you have to be careful not to redistribute these library by mistake.
顺便说一句,使用本地复制时,您必须小心不要错误地重新分发这些库。
回答by Roni Schwarts
do you use VSTO (Visual studio tools for office)?
你使用 VSTO(Visual Studio 办公工具)吗?

