80040154类未注册(HRESULT的异常 [英] 80040154 Class not registered (Exception from HRESULT

查看:117
本文介绍了80040154类未注册(HRESULT的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:该程序试图使用C#读取excel文件,然后将数据添加到sql developer中的表中。

This program is trying to read in an excel file using C# then adding the data to a table in sql developer.

我遇到了一个错误,我似乎不知道。当我尝试将excel数据加载到消息框中时,我得到以下消息:

I have ran into an error that I cant seem to figure out. When I try and load excel data into a Message Box I get this:

Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

此错误出现在:

Microsoft.Office.Interop.Excel.Workbook workbook = new Microsoft.Office.Interop.Excel.Workbook();

我已经针对此特定错误进行了研究,并尝试了所有发现的错误。我曾尝试将属性中的平台目标更改为 x86 ,就像某些人所说的那样,并且取消选中首选32位,将平台目标保留在任何CPU 处。我以为我可能会缺少重要的参考资料,但是注意到我已经有了.NET Microsoft.Office.Interop.Excel ,当我尝试添加等效的COM时没有帮助。作为参考,我使用的是32位Windows操作系统。

I have done research on this specific error and have tried everything I have found. I have tried changing the Platform Target in the properties to x86 like some people said along with unchecking Prefer 32-bit and leaving Platform Target at Any CPU. I figured I might be missing an important reference but noticed I already had the .NETMicrosoft.Office.Interop.Excel and when I tried adding the COM equivalent it did not help. For reference I am using a 32-bit Windows OS.

对于为什么出现此错误的任何帮助,将不胜感激。谢谢。

Any help on why this error is occurring would be greatly appreciated. Thanks.

这是课程中的完整代码:

Here is the full code in the class:

namespace ReadExcel
{
    public partial class Form1 : Form
    {
    public Microsoft.Office.Interop.Excel._Application excelApp = new Microsoft.Office.Interop.Excel.Application() { DisplayAlerts = false, Visible = false };

    public List<Attorney> listOfAttys = Helpers.getAttorneys();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        setAttyList();
    }


    private void setAttyList()
    {
        foreach (var item in listOfAttys.Select(x => x.Caption))
            cmbAtty.Items.Add(item);


    }


    private void btnRun_Click(object sender, EventArgs e)
    {
        Microsoft.Office.Interop.Excel.Workbook workbook = new Microsoft.Office.Interop.Excel.Workbook();
        //Microsoft.Office.Interop.Excel.Style style = new Microsoft.Office.Interop.Excel.Style();
        //Microsoft.Office.Interop.Excel.Worksheet worksheet = new Microsoft.Office.Interop.Excel.Worksheet();

        try
        {

            workbook = excelApp.Workbooks.Open(txtbxFilename.Text);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1);
            Microsoft.Office.Interop.Excel.Range xlRange = worksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;


            for(int i=1; i <= rowCount; i++)
            {
              for(int j=1; j <= colCount; j++)
              {
                MessageBox.Show(xlRange.Cells[i,j].Value2.ToString());
              }
            }


            if (validateHeader(worksheet))
            {




            }
        }
        catch (Exception ex)
        { 
        }


        excelApp.Quit();
    }

    private void btnLoad_Click(object sender, EventArgs e)
    {
        txtbxFilename.Text = null;

        System.IO.Stream myStream = null;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        openFileDialog1.InitialDirectory = "C:\\";
        openFileDialog1.Filter = "Excel files (*.xlsx)|*.xlsx";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = true;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    txtbxFilename.Text = openFileDialog1.FileName;
                    myStream.Close();
                    myStream.Dispose();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }
    }


    private bool validateHeader(Microsoft.Office.Interop.Excel.Worksheet Worksheet)
    {
        if (Worksheet == null)
            return false;

        bool isValid = false;

        //get header row
        //check all cell values

        return isValid;
    }

    //private int getWorkSheetLength(int startPoint, char column, Microsoft.Office.Interop.Excel.Worksheet sheet)
    //{
    //    int i = startPoint;

    //    while (sheet.Range(column + i).Text.ToString().Replace("/r", String.Empty).Replace("/n", String.Empty).Replace(" ", String.Empty) != String.Empty)
    //        i++;

    //    i--;
    //    return i;
    //}


}

public class Attorney
{
    public string AttorneyID { get; set; }
    public int OrganizationID { get; set; }
    public string AttorneyName { get; set; }
    public string Caption { get; set; }
}

public class LegalTransactionRec
{
    public string AccountNumber { get; set; }
    public decimal CostAmount { get; set; }
    public string SSN { get; set; }
    public int BatchID { get; set; }
    public Attorney Attorney { get; set; }
    public DateTime TransactionDate { get; set; }
    public string Description { get; set; }
    public int TransactionCode { get; set; }
}


public class ReviewOutput
{
}

public class ApprovedOutput
{ 
}

}


推荐答案

  var workbook = new Microsoft.Office.Interop.Excel.Workbook();

这是您代码中的错误。您不能像这样创建Workbook的新实例,必须必须使用 Application.WorkBooks.Add()方法。

This is a bug in your code. You cannot create new instances of Workbook like this, you must use the Application.WorkBooks.Add() method instead.

它等效于工厂方法,这是软件工程和Office互操作性的通用模式。无法获得构造函数的编译时错误是COM互操作性的责任。当您意识到Workbook是一个接口而不是一个类时,您会看到摩擦。使 new 关键字在COM接口类型上起作用的映射是不完善的。

It is the equivalent of a factory method, a common pattern in software engineering and Office interop in general. Not getting a compile-time error for the unavailable constructor is a COM interop liability. You can see the friction when you realize that Workbook is an interface, not a class. The mapping that makes the new keyword work on COM interface types is imperfect.

这篇关于80040154类未注册(HRESULT的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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