此代码对于excel文件中datagridview中的显示图像和数据是否正确 [英] Is this code is correct for display image and data in datagridview from excel file

查看:99
本文介绍了此代码对于excel文件中datagridview中的显示图像和数据是否正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public partial class Form1:Form

{

private string Excel03ConString =Provider = Microsoft.Jet.OLEDB.4.0; Data Source = {0}; Extended Properties ='Excel 8.0; HDR = {1}';

私有字符串Excel07ConString =Provider = Microsoft.ACE.OLEDB.12.0;数据源= {0};扩展属性='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;

}

//将数据加载到DataGridView

string cmdText;

OleDbCommand cmd;

OleDbDataReader dr;

DataTable dt = new DataTable(Book1);

cmdText =SELECT * FROM Book1;

OleDbConnection con = new OleDbConnection(Provider = Microsoft.ACE.OLEDB.12.0; Dat a来源= {0};扩展属性='Excel 8.0; HDR = {1}');

cmd =新OleDbCommand(cmdText,con);

con .Open();

dr = cmd.ExecuteReader();

if(dr.HasRows)dt.Load(dr);

dr.Close();

DataGridView1.DataSource = dt;

//初始化DataGridView列

DataGridView1.RowHeadersVisible = false;

foreach(DataGridView1.Columns中的DataGridViewColumn col)

{

col.SortMode = DataGridViewColumnSortMode.NotSortable;

col.ReadOnly = true;

if(col.GetType()。Name ==DataGridViewImageColumn)

{

foreach(DataGridView中的DataGridViewRow行。行)

{

如果(row.IsNewRow)继续;

row.Height = row.Cells [image]。ContentBounds.Height + 6;

}

}

}



}

这个代码显示异常

Microsoft Access数据库引擎找不到对象'Book1'。确保对象存在,并且您正确拼写其名称和路径名称。如果'Book1'不是本地对象,请检查您的网络连接或联系服务器管理员。

我的Excel文件名是Book1.xlsx,它存储在D:

解决方案

错误消息非常明确:

 Microsoft Access数据库引擎找不到对象'Book1'。确保对象存在,并且您正确拼写其名称和路径名称。如果'Book1'不是本地对象,请检查您的网络连接或联系服务器管理员。



我无法感觉您已经随机复制了在互联网的某个地方,并没有太看实际做了什么,或者它是如何做到的。

这一点特别:

 OleDbConnection con =  new  OleDbConnection(  Provider = Microsoft。 ACE.OLEDB.12.0;数据源= {0};扩展属性='Excel 8.0; HDR = {1}'); 

因为连接字符串看起来像是意味着成为string.Format方法调用的一部分,数据源和传递给它的属性。



你不能随便抓取代码并希望它会工作。你必须考虑它以及它如何应用于你的任务。



所以试着把文件的路径放到连接字符串中:

 OleDbConnection con =  new  OleDbConnection( @  Provider = Microsoft.ACE.OLEDB.12.0; Data Source ='D:\Temp \ MyFile.xlsx';扩展属性='Excel 8.0'); 


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;
}
// Load Data into DataGridView
string cmdText;
OleDbCommand cmd;
OleDbDataReader dr;
DataTable dt = new DataTable("Book1");
cmdText = "SELECT * FROM Book1";
OleDbConnection con=new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'");
cmd = new OleDbCommand(cmdText,con);
con.Open();
dr = cmd.ExecuteReader();
if (dr.HasRows) dt.Load(dr);
dr.Close();
DataGridView1.DataSource = dt;
// Initialize DataGridView Columns
DataGridView1.RowHeadersVisible = false;
foreach (DataGridViewColumn col in DataGridView1.Columns)
{
col.SortMode = DataGridViewColumnSortMode.NotSortable;
col.ReadOnly = true;
if (col.GetType().Name == "DataGridViewImageColumn")
{
foreach (DataGridViewRow row in DataGridView1.Rows)
{
if (row.IsNewRow) continue;
row.Height = row.Cells["image"].ContentBounds.Height + 6;
}
}
}

}
actuly this code show an exception
The Microsoft Access database engine could not find the object 'Book1'. Make sure the object exists and that you spell its name and the path name correctly. If 'Book1' is not a local object, check your network connection or contact the server administrator.
my excel file name is Book1.xlsxand it store in D:

解决方案

The error message is pretty explicit:

The Microsoft Access database engine could not find the object 'Book1'. Make sure the object exists and that you spell its name and the path name correctly. If 'Book1' is not a local object, check your network connection or contact the server administrator.


I can't help getting the feeling that you have copied that randomly from somewhere on the internet, and not looked too hard at what it actually did, or how it did it.
This bit in particular:

OleDbConnection con=new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'");

Because the connection string looks like it is meant to be part of a string.Format method call, with the datasource and the properties passed in to that.

You can't just grab code at random and hope it will work. You have to think about it and how it can be applied to your task.

So try putting the path to the file into the connections string:

OleDbConnection con=new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='D:\Temp\MyFile.xlsx';Extended Properties='Excel 8.0'");


这篇关于此代码对于excel文件中datagridview中的显示图像和数据是否正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆