c# – 将Excel导入Datagridview

c# – 将Excel导入Datagridview,第1张

概述我正在使用此代码打开excel文件并将其保存在DataGridView中: string name = "Items";string constr = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source=" + Dialog_Excel.FileName.ToString() + "; Extented Properties =\"Excel 8.0 我正在使用此代码打开excel文件并将其保存在DataGrIDVIEw中:
string name = "Items";string constr = "ProvIDer = Microsoft.Jet.olEDB.4.0; Data Source=" + Dialog_Excel.filename.ToString() + "; Extented PropertIEs =\"Excel 8.0; HDR=Yes;\";";oleDbConnection con = new oleDbConnection(constr);oleDbDataAdapter sda = new oleDbDataAdapter("Select * From [" + name + "$]",con);Datatable data = new Datatable();sda.Fill(data);grID_items.DataSource = data;

如上所述:
This youtube link

但是我收到了ISAM错误.有什么建议吗?

解决方法 由于您没有回复我上面的评论,我正在为两者发布解决方案.

您在扩展属性中缺少’

对于Excel 2003试试这个(经过试验和测试)

private voID button1_Click(object sender,EventArgs e)    {        String name = "Items";        String constr = "ProvIDer=Microsoft.Jet.olEDB.4.0;Data Source=" +                        "C:\Sample.xls" +                         ";Extended PropertIEs='Excel 8.0;HDR=YES;';";        oleDbConnection con = new oleDbConnection(constr);        oleDbCommand oconn = new oleDbCommand("Select * From [" + name + "$]",con);        con.open();        oleDbDataAdapter sda = new oleDbDataAdapter(oconn);        Datatable data = new Datatable();        sda.Fill(data);        grID_items.DataSource = data;    }

顺便说一下,我很久以前就不再使用Jet了.我现在使用ACE.

private voID button1_Click(object sender,EventArgs e)    {        String name = "Items";        String constr = "ProvIDer=Microsoft.ACE.olEDB.12.0;Data Source=" +                        "C:\Sample.xls" +                         ";Extended PropertIEs='Excel 8.0;HDR=YES;';";        oleDbConnection con = new oleDbConnection(constr);        oleDbCommand oconn = new oleDbCommand("Select * From [" + name + "$]",con);        con.open();        oleDbDataAdapter sda = new oleDbDataAdapter(oconn);        Datatable data = new Datatable();        sda.Fill(data);        grID_items.DataSource = data;    }

对于Excel 2007

private voID button1_Click(object sender,EventArgs e)    {        String name = "Items";        String constr = "ProvIDer=Microsoft.ACE.olEDB.12.0;Data Source=" +                        "C:\Sample.xlsx" +                         ";Extended PropertIEs='Excel 12.0 XML;HDR=YES;';";        oleDbConnection con = new oleDbConnection(constr);        oleDbCommand oconn = new oleDbCommand("Select * From [" + name + "$]",con);        con.open();        oleDbDataAdapter sda = new oleDbDataAdapter(oconn);        Datatable data = new Datatable();        sda.Fill(data);        grID_items.DataSource = data;    }
总结

以上是内存溢出为你收集整理的c# – 将Excel导入Datagridview全部内容,希望文章能够帮你解决c# – 将Excel导入Datagridview所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1240769.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存