//读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
最新推荐文章于 2024-10-09 16:02:44 发布