80040154类未注册(从HRESULT异常(80040154 Class not registe

2019-10-19 04:56发布

编辑:该程序试图使用C#,然后将数据添加到表中的SQL开发人员在一个Excel文件中读取。

我已经遇到了,我似乎无法找出错误。 当我尝试和负载的Excel数据到一个消息框,我得到这样的:

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();

我已经做此特定错误的研究,并已尝试了一切我已经找到。 我曾试图改变Platform Target在性能x86像一些人取消选中一起说Prefer 32-bit和离开Platform TargetAny CPU 。 我想我可能会丢失一个重要的参考,但发现我已经有.NET Microsoft.Office.Interop.Excel ,当我试图添加COM相当于它并没有帮助。 仅供参考我使用的是32位Windows操作系统。

为什么这个错误发生的任何帮助,将不胜感激。 谢谢。

下面是在类的完整代码:

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
{ 
}

}

Answer 1:

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

这是你的代码中的bug。 您不能创建工作簿的新情况就是这样,你必须使用Application.WorkBooks.Add()方法来代替。

这是一个工厂方法是等效的,在软件工程和Office互操作的通用模式一般。 没有得到一个编译时错误不可用的构造是一个COM互操作的责任。 你可以看到摩擦当你意识到工作簿是一个接口,而不是一个类。 这使得对COM接口类型新的关键字工作的映射是不完善的。



文章来源: 80040154 Class not registered (Exception from HRESULT