将图像从excel导入datagridview时代码出错 [英] Error in code while importing image from excel into datagridview
问题描述
任何正文都可以帮我找到我的代码中的删除错误
我有一张excel表,它是商店数据和图片excel表专栏是Roll,Name,Address,Photo。现在我将这个excel表导入datagridview,但只有数据即将到来,图像不会出现。所以我写了这段代码,但这段代码有一些错误。
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click( object sender,EventArgs e)
{
OpenFileDialog1.ShowDialog();
}
private void OpenFileDialog1_FileOk(< span class =code-keyword> object sender,CancelEventArgs e)
{
DataGridView1.AllowUserToAddRows = false ;
DataGridView1.RowHeadersVisible = false ;
// 从excel获取数据将其添加到数据集
OleDbConnection conn = new OleDbConnection( Provider = Microsoft.ACE.OLEDB。 12.0;数据源= {0};扩展属性='Excel 8.0; HDR = {1}');
OleDbDataAdapter comm = new OleDbDataAdapter( select *来自[mySheet $],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。表格( 0 ) .Columns( 3 ).ColumnName;
imgColumn.HeaderText = dset。表格( 0 ) .Columns( 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。行(xlsShape.BottomRightCell.Row - 2 )。Cells(xlsShape.BottomRightCell.Column - 1 )。Value = img;
}
}
}
}
错误位置标记为粗体,错误为:
错误1'System.Data.DataSet'不包含'tables'的定义,并且没有可以找到接受类型'System.Data.DataSet'的第一个参数的扩展方法'tables'(是你错过了using指令或程序集引用?)
错误2不可调用的成员'System.Data.DataSet.Tables'不能像方法一样使用。
错误3非可调用成员'System.Windows.Forms.DataGridView.Rows'不能像方法一样使用。
任何人都可以帮助我
< blockquote>,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。表格( 0 ) .Columns( 3 ).ColumnName;
imgColumn.HeaderText = dset。表格( 0 ) .Columns( 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。行(xlsShape.BottomRightCell.Row - 2 )。Cells(xlsShape.BottomRightCell.Column - 1 )。Value = img;
}
}
}
}
错误位置标记为粗体,错误为:
错误1'System.Data.DataSet'不包含'tables'的定义,并且没有可以找到接受类型'System.Data.DataSet'的第一个参数的扩展方法'tables'(是你错过了using指令或程序集引用?)
错误2不可调用的成员'System.Data.DataSet.Tables'不能像方法一样使用。
错误3非可调用成员'System.Windows.Forms.DataGridView.Rows'不能像方法一样使用。
任何人都可以帮助我
从查看他们报告错误的代码行:
&错误1'System.Data.DataSet'不包含'tables'的定义,没有可以找到扩展方法'tables'接受类型'System.Data.DataSet'的第一个参数(你是否错过了直接使用ive或汇编参考?)
imgColumn.Name = dset.tables( 0 ).Columns( 3 )。ColumnName;C#区分大小写:tables与Tables不同
错误2不可调用的成员'System.Data.DataSet.Tables'不能像方法一样使用。
imgColumn.HeaderText = dset.Tables( 0 )。列( 3 )。ColumnName;
表是一个集合,而不是一个方法 - 所以你使用squarebrackets进行索引,而不是舍入。专栏也是!尝试:
imgColumn.HeaderText = dset.Tables [ 0 ]。列[ 3 ]。ColumnName;
错误3不可调用的成员'System.Windows.Forms。 DataGridView.Rows'不能像方法一样使用。
DataGridView1.Rows(xlsShape.BottomRightCell.Row - 2 )。单元格(xlsShape.BottomRightCell.Column - 1 )。Value = img;
见上一个错误!
更改
dset.tables( 0 )
to
dset.Tables [ 0 ]
注意c#中的人均T和括号,
,你需要在访问索引数据时使用[]。
can any body help me to find remove error in my code
i have a excel sheet which are store data and image excel sheet column are Roll,Name,Address,Photo. now i import this excel sheet into datagridview but only data is coming,image are not coming .so i have write this code but this code having some error.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog1.ShowDialog();
}
private void OpenFileDialog1_FileOk(object sender, CancelEventArgs e)
{
DataGridView1.AllowUserToAddRows = false;
DataGridView1.RowHeadersVisible = false;
//get the data from excel add it to dataset
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'");
OleDbDataAdapter comm = new OleDbDataAdapter("select * from [mySheet$]", 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;
}
}
}
}
error position are marked as bold and error are:
Error 1 'System.Data.DataSet' does not contain a definition for 'tables' and no extension method 'tables' accepting a first argument of type 'System.Data.DataSet' could be found (are you missing a using directive or an assembly reference?)
Error 2 Non-invocable member 'System.Data.DataSet.Tables' cannot be used like a method.
Error 3 Non-invocable member 'System.Windows.Forms.DataGridView.Rows' cannot be used like a method.
can anybody help me
", 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; } } } }
error position are marked as bold and error are:
Error 1 'System.Data.DataSet' does not contain a definition for 'tables' and no extension method 'tables' accepting a first argument of type 'System.Data.DataSet' could be found (are you missing a using directive or an assembly reference?)
Error 2 Non-invocable member 'System.Data.DataSet.Tables' cannot be used like a method.
Error 3 Non-invocable member 'System.Windows.Forms.DataGridView.Rows' cannot be used like a method.
can anybody help me
Start by looking at the lines of code that they are reporting the error on:
&Error 1 'System.Data.DataSet' does not contain a definition for 'tables' and no extension method 'tables' accepting a first argument of type 'System.Data.DataSet' could be found (are you missing a using directive or an assembly reference?)
imgColumn.Name = dset.tables(0).Columns(3).ColumnName;C# is case sensitive: "tables" is not the same as "Tables"
Error 2 Non-invocable member 'System.Data.DataSet.Tables' cannot be used like a method.
imgColumn.HeaderText = dset.Tables(0).Columns(3).ColumnName;
Tables is a Collection, not a method - so you use squarebrackets for indexing, not round. So is Columns! Try:
imgColumn.HeaderText = dset.Tables[0].Columns[3].ColumnName;
Error 3 Non-invocable member 'System.Windows.Forms.DataGridView.Rows' cannot be used like a method.
DataGridView1.Rows(xlsShape.BottomRightCell.Row - 2).Cells(xlsShape.BottomRightCell.Column - 1).Value = img;
See the previous error!
change
dset.tables(0)
to
dset.Tables[0]
note that capita T and brackets,
in c# you need to use [] when access index data.
这篇关于将图像从excel导入datagridview时代码出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!