C# Microsoft.Office.Interop.Excel.ApplicationClass 没有定义构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14016264/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:24:51  来源:igfitidea点击:

Microsoft.Office.Interop.Excel.ApplicationClass has no constructor defined

c#excelinteropexcel-interop

提问by horgh

I tried to follow How to open an Excel file in C# tutorial, i.e. added a reference on the Comtab to Microsoft Office 14.0 Object Libraryand tried to compile code:

我尝试遵循如何在 C#教程中打开 Excel 文件,即在Com选项卡上添加了一个引用Microsoft Office 14.0 Object Library并尝试编译代码:

using Excel = Microsoft.Office.Interop.Excel;
//...
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;

xlApp = new Excel.ApplicationClass();//error here
//...

and faced a compile-time error, saying

并面临编译时错误,说

There is no constructor defined for Microsoft.Office.Interop.Excel.ApplicationClass type.

没有为 Microsoft.Office.Interop.Excel.ApplicationClass 类型定义构造函数。

What am I missing?

我错过了什么?

采纳答案by Vlad Spreys

Try this:

尝试这个:

Excel._Application xlApp = new Excel.Application();

回答by PeterJ

Use the following to open it:

使用以下命令打开它:

xlApp = CreateObject("Excel.Application");

CreateObject creates and returns a reference to a COM object. Documentation may be found here:

CreateObject 创建并返回对 COM 对象的引用。文档可以在这里找到:

http://msdn.microsoft.com/en-us/library/7t9k08y5%28v=vs.71%29.aspx

http://msdn.microsoft.com/en-us/library/7t9k08y5%28v=vs.71%29.aspx

回答by Mehrad

If you're using C# 4.0 (.NET 4) you can go with much easier syntax

如果您使用的是 C# 4.0 (.NET 4),您可以使用更简单的语法

var app = new Application( );

var workbook = app.Workbooks.Open("test.xls");

In regards to var: it makes you job easier cuz C# decides which type to chose and go for. If interested you can read about dynamicand varstyles.

关于var:它使您的工作更轻松,因为 C# 决定选择和使用哪种类型。如果有兴趣,您可以阅读有关dynamicvar样式的信息

Remember that interop prior to C# 4.0 is totally a different phenomenon and how C# used to handle Microsoft objects.

请记住,C# 4.0 之前的互操作是完全不同的现象,以及 C# 用于处理 Microsoft 对象的方式。

just so you know how different, this is how you should've coded it before C# 4.0 to talk to a Worddocument.

只是为了让您知道有多么不同,这就是您应该在 C# 4.0 之前对其进行编码以与Word文档对话的方式。

object fileName = @"WordFile.docx";
object missing = System.Reflection.Missing.Value;
object readOnly = true;
var doc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);