COM类工厂错误

时间:2014-04-24 09:23:22

标签: c#-4.0

我在控制台应用程序中尝试显示,由于以下错误,检索CLSID {00024500-0000-0000-C000-000000000046}组件的COM类工厂失败:80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

这是错误。如何解决这个问题:

static void Main(string[] args)
    {


        StringBuilder query = new StringBuilder();
        query.Append("SELECT deptId ");
        query.Append(",[deptabbr], [deptname ], [deptnameLocal] ");
        query.Append(",[deptabbrLocal ]");
        //    , [UnitPrice], [UnitsInStock] ");
        //query.Append(",[UnitsOnOrder], [ReorderLevel], [Discontinued] ");
        query.Append("FROM [dbo].[mDepartment] ");
        //query.Append("JOIN Categories ON Categories.CategoryID = Products.CategoryID ");
        //query.Append("ORDER BY Categories.CategoryName ");

        SQL.DataTable dtProducts = new SQL.DataTable();
        using (SqlConnection cn = new SqlConnection("server=ADMIN-PC;uid=sa;pwd=microsoftsql;database=HPMS_IntranetApp;Integrated Security=True"))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(query.ToString(), cn))
            {
                da.Fill(dtProducts); 
            }
        }

        Excel.Application oXL;
        Excel._Workbook oWB;
        Excel._Worksheet oSheet;

        oXL = new Excel.Application();
       oXL.Visible = true;

        oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
        oSheet = (Excel._Worksheet)oWB.ActiveSheet;

        try
        {
            SQL.DataTable dtCategories = dtProducts.DefaultView.ToTable(true, "deptId");

            foreach (SQL.DataRow category in dtCategories.Rows)
            {
                oSheet = (Excel._Worksheet)oXL.Worksheets.Add();
                oSheet.Name = category[0].ToString().Replace(" ", "").Replace("  ", "").Replace("/", "").Replace("\\", "").Replace("*", "");

                string[] colNames = new string[dtProducts.Columns.Count];

                int col = 0;

                foreach (SQL.DataColumn dc in dtProducts.Columns)
                    colNames[col++] = dc.ColumnName;

                char lastColumn = (char)(65 + dtProducts.Columns.Count - 1);

                oSheet.get_Range("A1", lastColumn + "1").Value2 = colNames;
                oSheet.get_Range("A1", lastColumn + "1").Font.Bold = true;
                oSheet.get_Range("A1", lastColumn + "1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;

                SQL.DataRow[] dr = dtProducts.Select(string.Format("deptId='{0}'", category[0].ToString()));

                string[,] rowData = new string[dr.Count<SQL.DataRow>(), dtProducts.Columns.Count];

                int rowCnt = 0;
                int redRows = 2;
                foreach (SQL.DataRow row in dr)
                {                         
                    for (col = 0; col < dtProducts.Columns.Count; col++)
                    {
                        rowData[rowCnt, col] = row[col].ToString();
                    }

                    if (int.Parse(row["deptname"].ToString()) < int.Parse(row["deptnameLocal"].ToString()))
                    {
                        Range range = oSheet.get_Range("A" + redRows.ToString(), "J" + redRows.ToString());
                        range.Cells.Interior.Color = System.Drawing.Color.Red; 
                    }
                    redRows++;
                    rowCnt++;
                }
                oSheet.get_Range("A2", lastColumn + rowCnt.ToString()).Value2 = rowData;
            }   

            oXL.Visible = true;
            oXL.UserControl = true;

            oWB.SaveAs("Products.xlsx",
                AccessMode: Excel.XlSaveAsAccessMode.xlShared);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);  
        }
        finally
        {   
            Marshal.ReleaseComObject(oWB);
        }
    }

0 个答案:

没有答案