Click here to Skip to main content
15,667,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have facing a exception in my coding:

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)).

this is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using Microsoft.Office.Interop.Excel;
namespace WindowsFormsApplication2
    public partial class Form1 : Form
        private string Excel03ConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";
        private string Excel07ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";
        public Form1()
        private void button1_Click(object sender, EventArgs e)
        private void OpenFileDialog1_FileOk(object sender, CancelEventArgs e)
            string filePath = OpenFileDialog1.FileName;
            string extension = Path.GetExtension(filePath);
            string header = rbHeaderYes.Checked ? "YES" : "NO";
            string conStr; 
            conStr = string.Empty;
            switch (extension)
                case ".xls": //Excel 97-03
                    conStr = string.Format(Excel03ConString, filePath, header);
                case ".xlsx": //Excel 07
                    conStr = string.Format(Excel07ConString, filePath, header);
            DataGridView1.AllowUserToAddRows = false;
            DataGridView1.RowHeadersVisible = false;
            //get the data from excel add it to dataset
            OleDbConnection conn = new OleDbConnection(conStr);
            OleDbDataAdapter comm = new OleDbDataAdapter("select * from [sheet1$]", conn);
            DataSet dset = new DataSet();
            comm.TableMappings.Add("Table", "TempTable");
            Microsoft.Office.Interop.Excel.Application oExcel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook oBook = new Workbook();
            Microsoft.Office.Interop.Excel.Worksheet oSheet = new Worksheet();
            oBook = oExcel.Workbooks.Open("mySheet");
            oSheet = oBook.Sheets.Add("Mysheet");
            DataGridView1.DataSource = dset.Tables;
            //remove the column of datagridview which has images
            // DataGridView1.Columns.RemoveAt(3);
            DataGridViewImageColumn imgColumn = new DataGridViewImageColumn();
            imgColumn.Name = dset.Tables[0].Columns[3].ColumnName;
            imgColumn.HeaderText = dset.Tables[0].Columns[3].ColumnName;
            //add the DataGridViewImageColumn to datagridview
            // DataGridView1.Columns.Insert(2, imgColumn)
            DataGridView1.Columns.Insert(3, imgColumn);
            Image img;
            //copy the image to clipboard and add the image to the cell 
            foreach (Shape xlsShape in oSheet.Shapes)
                if (xlsShape.BottomRightCell.Column - 1 < DataGridView1.Columns.Count && xlsShape.BottomRightCell.Row - 2 < DataGridView1.Rows.Count)
                if (Clipboard.ContainsImage())
                    img = Clipboard.GetImage();
                    DataGridView1.Rows[xlsShape.BottomRightCell.Row - 2].Cells[xlsShape.BottomRightCell.Column - 1].Value = img;

this code upload a excel file from user using Openfilediloag and display in datagridview .
this excel file content data and image.
Updated 24-Jul-15 2:08am
Leo Chapiro 24-Jul-15 8:09am    
Try to use Try/Catch and debug your source code.
[no name] 24-Jul-15 8:16am    
The message means what it says. You are trying to use a COM control that isn't registered. Register it.
Member 10273293 24-Jul-15 8:18am    
not solve my problum
Thanks7872 24-Jul-15 8:22am    
What do you mean? We can not understand issues with such short statements.
Member 10273293 24-Jul-15 10:03am    
dear Rohan
i am not told that to you, i just need to import image from excel file into datagridview,above code not work for that work can you please help me for this i will be very thankful to him

The machine you run this on needs Excel to be installed and working using the currently logged in user.
Share this answer
You have a couple of lines of code:
Microsoft.Office.Interop.Excel.Workbook oBook = new Workbook();
Microsoft.Office.Interop.Excel.Worksheet oSheet = new Worksheet();

that you cannot use. They don't work because you're trying to create an instance of an Interface, which is impossible.

You have to have Excel create these objects for you.
Workbook wb = oExcel.Workbooks.Add(System.Reflection.Missing.Value);
Share this answer
Member 10273293 25-Jul-15 4:53am    
is anybody please help me to slove this exception ??????
Dave Kreskowiak 25-Jul-15 10:52am    
I just told you how to solve it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900