本文介绍了需要帮助来解决异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的编码遇到异常: 检索带有CLSID的组件的COM类工厂{00020819-0000- 0000-C000-000000000046}由于以下错误而失败:80040154未注册类(HRESULT异常:0x80040154(REGDB_E_CLASSNOTREG))。 这是我的代码: 使用系统; 使用 System.Collections.Generic; 使用 System.ComponentModel; 使用 System.Data; 使用 System.Drawing; 使用 System.Linq; 使用 System.Text; 使用 System.Windows.Forms; 使用 System.Data.OleDb; 使用 System.IO; 使用 Microsoft.Office.Interop.Excel; 命名空间 WindowsFormsApplication2 { public partial class Form1:表格 { private string Excel03ConString = Provider = Microsoft.Jet.OLEDB .4.0;数据源= {0};扩展属性='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() { InitializeComponent(); } 私有 void button1_Click( object sender,EventArgs e) { OpenFileDialog1.ShowDialog(); } 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 (扩展名) { 案例 。xls: // Excel 97-03 conStr = string .Format(Excel03ConString,filePath,header); break ; case 。xlsx: // Excel 07 conStr = string .Format(Excel07ConString,filePath,header); break ; } DataGridView1.AllowUserToAddRows = false ; DataGridView1.RowHeadersVisible = false ; // 从excel获取数据将其添加到数据集 OleDbConnection conn = new OleDbConnection(conStr); OleDbDataAdapter comm = new OleDbDataAdapter( select *来自[sheet1 $],conn); DataSet dset = new DataSet(); comm.TableMappings.Add( 表, TempTable); comm.Fill(dset); conn.Close(); 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 工作表(); oBook = oExcel.Workbooks.Open( mySheet); oSheet = oBook.Sheets.Add( Mysheet); DataGridView1.DataSource = dset.Tables; // 删除包含图像的datagridview列 DataGridView1.Columns。 RemoveAt( 3 ); // DataGridView1.Columns.RemoveAt(3); DataGridViewImageColumn imgColumn = new DataGridViewImageColumn(); imgColumn.Name = dset.Tables [ 0 ]。列[ 3 ]。ColumnName; imgColumn.HeaderText = dset.Tables [ 0 ]。列[ 3 ]。ColumnName; // 将DataGridViewImageColumn添加到datagridview // DataGridView1.Columns.Insert(2,imgColumn) DataGridView1.Columns.Insert( 3 ,imgColumn); 图片img; // 将图像复制到剪贴板并将图像添加到单元格 foreach (形状xlsShape oSheet.Shapes) { if (xlsShape.BottomRightCell.Column - 1 < DataGridView1 .Columns.Count&& xlsShape.BottomRightCell.Row - 2 < DataGridView1.Rows.Count ) { xlsShape.Copy(); } if (Clipboard.ContainsImage()) { img = Clipboard.GetImage(); DataGridView1.Rows [xlsShape.BottomRightCell.Row - 2 ]。Cells [xlsShape.BottomRightCell.Column - 1 ]。Value = img; } } } } } 此代码使用Openfilediloag从用户上传excel文件并在datagridview中显示。 这个excel文件内容数据和图像。解决方案 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() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog1.ShowDialog(); } 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); break; case ".xlsx": //Excel 07 conStr = string.Format(Excel07ConString, filePath, header); break; } 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"); comm.Fill(dset); conn.Close(); 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); // 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) { xlsShape.Copy(); } 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. 解决方案 这篇关于需要帮助来解决异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 13:47