c#读取excel

<span style="white-space:pre">	</span>    System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
            if (fd.ShowDialog() == DialogResult.OK)
            {
                
                string strConn = "Provider=Microsoft.Ace.OLEDB.12.0;" + "Data Source=" + fd.FileName + ";" + "Extended Properties=Excel 8.0;";
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                string strExcel = "";
                OleDbDataAdapter myCommand = null;
                strExcel = string.Format("select * from [{0}$]", "Sheet1"); //Sheet1为excel中工作薄
                myCommand = new OleDbDataAdapter(strExcel, strConn);
                myCommand.Fill(xds, "Sheet1");
                this.dataGridView2.DataSource = xds.Tables[0];//显示到界面上
                MessageBox.Show("导入完成");
            }

你可能感兴趣的:(c#读取excel)