<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
最新推荐文章于 2024-07-09 10:27:57 发布