gpt4 book ai didi

c# - 将 Excel 工作表导入数据 GridView

转载 作者:太空宇宙 更新时间:2023-11-03 12:12:26 25 4
gpt4 key购买 nike

我正在尝试使用此代码 Used Code将 excel 表导入数据 grideview 但此异常被触发,我不知道主要问题是什么。

Exception

我正在尝试一些解决方案,例如将事件解决方案平台从任何人更改为 x86,但它不起作用

Excel文件版本重要Excel 2010、Excel 2013吗?

更新:
我从这个 Site 安装了 Microsoft Office Access 数据库引擎,最后一个异常消失了,但是这个新的异常被触发了

Exception#2

这是使用的代码:

public void importfromexcel()
{
string SheetPath = @"E:\students.xlsx";
string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + SheetPath + ";Extended Properties=Excel 12.0;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbDataAdapter sda = new OleDbDataAdapter("Select * From [" + SheetPath + "$]", con);
System.Data.DataTable data = new System.Data.DataTable();
sda.Fill(data);
dataGridView1.DataSource = data;
}

这段代码有什么问题吗??
提前致谢。

最佳答案

尝试对下面的代码进行一些小改动……它按预期工作。

string WorkBookPath = @"D:\Test\Book1.xlsx";  //@"E:\students.xlsx";
string SheetName = "Sheet1$"; // <--- this is the name of the WORKSHEET in the workBOOK
string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + WorkBookPath + ";Extended Properties=Excel 12.0;";
using (OleDbConnection con = new OleDbConnection(constr)) {
OleDbDataAdapter sda = new OleDbDataAdapter("Select * From [" + SheetName + "]", con);
DataTable data = new DataTable();
sda.Fill(data);
dataGridView1.DataSource = data;
}

关于c# - 将 Excel 工作表导入数据 GridView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51433791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com