OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "xls|*.xls";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
filePath = ofd.FileName;
using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ofd.FileName + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'"))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter("select * from [Sheet1$]", conn))
{
System.Data.DataTable dt = new System.Data.DataTable();
adapter.Fill(dt);
this.dataGridView1.DataSource = dt;
}
}
}
C#Winform通过连接访问Excel文件
本文转载:CSDN博客