//读Excel到DataGridView
private void RedExcel()
{
string sPath = "F://excel名称.xls";
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sPath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
string strSQL = "SELECT * FROM [sheet1$]";
OleDbConnection excelConnection = new OleDbConnection(connectionString);
excelConnection.Open();
OleDbCommand dbCommand = new OleDbCommand(strSQL, excelConnection);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand);
DataTable dTable = new DataTable();
dataAdapter.Fill(dTable);
dataGridView1.DataSource = dTable;
dTable.Dispose();
dataAdapter.Dispose();
dbCommand.Dispose();
excelConnection.Close();
excelConnection.Dispose();
}
读Excel到DataTable
最新推荐文章于 2025-12-02 10:10:41 发布
本文介绍了一种使用C#从指定路径的Excel文件中读取数据,并将其展示在DataGridView控件上的方法。通过OLE DB连接并执行SQL查询来获取Excel表格数据(sheet1),接着填充到DataTable对象中,最后设置DataGridView的数据源。
524

被折叠的 条评论
为什么被折叠?



