将 excel 文件读入 datagridview c#



我正在尝试将一个特定的 excel 文件读取到名为 stats_table 的 datagridview 中,但大多数时候整个系统崩溃或根本没有做任何事情。我在要导入的特定按钮下使用的代码如下:

private void Predict_Click(object sender, EventArgs e)
{
string path = @"C:UsersepifaDesktopMaster2nd semesteresportsall_cards.xlsx";
string constr = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = "+ path + " Extended Properties="Excel 12.0 Macro; HDR = YES";
OleDbConnection con = new OleDbConnection(constr);
DataTable dt = new DataTable();
Stats_table.DataSource = dt;
}

你能指教吗?

经过多次尝试,我终于明白了,

OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source =  C:UsersepifaDesktopMaster2nd semesteresportsall_cards.xlsx" + @"; Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", conn);
DataSet stats = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter.Fill(stats);
Stats_table.DataSource = stats.Tables[0];

无论如何,谢谢你:)

你需要找出发生了什么。

显然,异常正在发生。

您可以捕获异常以了解发生了什么:

private void Predict_Click(object sender, EventArgs e)
{
try 
{
string path = @"C:UsersepifaDesktopMaster2nd semesteresportsall_cards.xlsx";
string constr = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = "+ path + " Extended Properties="Excel 12.0 Macro; HDR = YES";
OleDbConnection con = new OleDbConnection(constr);
DataTable dt = new DataTable();
Stats_table.DataSource = dt;
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}

在你知道问题是什么之后,你可以继续修复它。

正如你评论的那样..."然后因为我知道连接正在工作">......这是值得商榷的。但是,假设您有一个有效的连接...该代码似乎没有"与"该连接执行任何操作......例如,在线...

OleDbConnection con = new OleDbConnection(constr);

这行代码很可能成功,但你没有对连接做任何事情......,比如用 excel 工作簿中的数据"填充"数据表。

在这行代码之后,将创建一个新的空DataTable,然后将此"空"表设置为网格的DataSource。我不确定除了"空"网格之外,您还会期待什么。

最后,连接字符串似乎缺少一些语法,并且可能会因此而失败。下面是两个将打开 XLS 和 XLSX 文件的示例。请记住,您需要知道要用来填充表格的工作表的名称。

要打开带有名为"sheet1"的工作表的 XLSX 文件...

private void btn_OpenXLSX_Click(object sender, EventArgs e) {
string path = @"path_to_the_XLSX_file";
string worksheetName = "sheet1";
string constr = "Provider = Microsoft.Ace.OLEDB.12.0; Data Source = " + path + "; Extended Properties="Excel 12.0; HDR = YES;Imex=1;";";
OleDbConnection con = new OleDbConnection(constr);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + worksheetName + "$]", con);
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
Stats_table.DataSource = dt;
}

要打开带有名为"sheet1"的工作表的 XLS 文件...

private void btn_OpenXLS_Click(object sender, EventArgs e) {
string path = @"path_to_the_XLS_file";
string worksheetName = "sheet1";
string constr = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + path + "; Extended Properties="Excel 8.0; HDR = YES;Imex=1;";";
OleDbConnection con = new OleDbConnection(constr);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + worksheetName + "$]", con);
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
Stats_table.DataSource = dt;
}

我希望这是有道理的,也有帮助。

最新更新