无法将类型为“microsoft.Office.Interop.Excel.ApplicationClass”的COM对象转换为“microsoft.Office.Interop.Excel.Application”“ [英] unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'"

查看:3737
本文介绍了无法将类型为“microsoft.Office.Interop.Excel.ApplicationClass”的COM对象转换为“microsoft.Office.Interop.Excel.Application”“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试从一个C#控制台应用程序中从excel中捕获一些数据。



我收到错误无法转换类型的COM对象 .Office.Interop.Excel.ApplicationClass'到'microsoft.Office.Interop.Excel.Application'。



此代码使用Microsoft Excel 12.0对象库我引用了Microsoft.Office.Interop.Excel。



尽管如此,我无法克服这个错误 - 我相信它有自己的快速解决方案。 >

我挖了一点这个网站,遇到了这个解决方案:
Interop类型无法嵌入



然而,我无法理解,所以无法实现所建议的解决方案。 / p>

我的.Net版本是4.5。



任何帮助都非常感谢。



提前感谢

  usin g系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;

使用Excel = Microsoft.Office.Interop.Excel;

命名空间deneme
{
类程序
{
static void Main(string [] args)
{

Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true; //< - excel application
xlApp.DisplayAlerts = false;

//打开工作簿。
Excel.Workbook wBook = xlApp.Workbooks.Open(C:\\\FNN\\XLA\\\\
nComTemplate.xlsx,
Type.Missing,Type.Missing,Type .Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
类型。

//获取工作表
Excel.Worksheet wSheet = wBook.Sheets [0];
// foreach(在wBook.Sheets中的Excel.Worksheet表){if(sheet.Name ==templateSheet){wSheet = sheet; }}

Excel.Range rng = wSheet.get_Range(A1);

aux = wSheet.Range [F6]。

Console.WriteLine(interop result:+ aux);
Console.ReadLine();

}

}
}


解决方案

如果这不是与代码相关的错误,请从注册表中删除以下给定的密钥。



步骤:



开始 - >运行 - > regedit - > HKEY_CLASSES_ROOT - > TypeLib - > {00020813-0000-0000-C000-000000000046} - > 1.8 / 1.7(删除这个)



解决方案:
可以通过删除更高版本的Office中留下的无效注册表项来解决此问题。请执行上述步骤。



原因:
如果您将Microsoft Office的版本从Office 2010降级为Office 2007或Office 2013到Office 2010或2007.



如果此解决方案没有得到帮助,请通知我


This is my first attempt to capture some data from excel from within one C# console application.

I get the error "unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'".

This code used the 'Microsoft Excel 12.0 Object Library' , and I gave reference to Microsoft.Office.Interop.Excel.

Still, I could not get over this error - which I believe has its own quick solution.

I digged a little bit this site and came across with this solution: Interop type cannot be embedded

However, I could not understand so could not implement what was suggested as a solution.

My .Net version is 4.5.

Any help is very much appreciated.

Thanks in advance.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Excel = Microsoft.Office.Interop.Excel;

namespace deneme
{
class Program
{
    static void Main(string[] args)
    {

        Excel.Application xlApp = new Excel.Application();
        xlApp.Visible = true; // <-- excel application
        xlApp.DisplayAlerts = false;

        // Open the workbook.
        Excel.Workbook wBook = xlApp.Workbooks.Open("C:\\FNN\\XLA\\fnnComTemplate.xlsx",
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing);

        // get the sheet
        Excel.Worksheet wSheet = wBook.Sheets[0];
        // foreach (Excel.Worksheet sheet in wBook.Sheets) { if (sheet.Name == "templateSheet") { wSheet = sheet; } }

        Excel.Range rng = wSheet.get_Range("A1");

        aux = wSheet.Range["F6"].Value;

        Console.WriteLine("interop result:" + aux);
        Console.ReadLine();

    }

}
}

解决方案

If this is not a code related error than please remove below given key from registry.

Steps:

Start--> Run --> regedit --> HKEY_CLASSES_ROOT-->TypeLib-->{00020813-0000-0000-C000-000000000046} --> 1.8/1.7 (Delete this)

Solution: This issue can be resolved by deleting an invalid registry key left over from the higher version of Office. Follow the above steps.

Cause: This issue is caused by a left over registry key if you have downgraded your version of Microsoft Office from either Office 2010 to Office 2007, or Office 2013 to Office 2010 or 2007.

Let me know if this solution is not helped to you

这篇关于无法将类型为“microsoft.Office.Interop.Excel.ApplicationClass”的COM对象转换为“microsoft.Office.Interop.Excel.Application”“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆