sqlServer连接Excel代码
?????????????????? private void button1_click()?????????????????? { ??????????????????????????? try ??????????????????????????? { ???????????????????????????????????? if(this.dataSet1.Tables.Count>0) ???????????????????????????????????? { ?????????????????????????????????????????????? this.dataSet1.Tables[0].Rows.Clear(); ???????????????????????????????????? } ???????????????????????????????????? string filename ="";??????????????????????????? ???????????????????????????????????? DialogResult result =openFileDialog1.ShowDialog(); ???????????????????????????????????? if( result == DialogResult.OK ) ???????????????????????????????????? { ?????????????????????????????????????????????? filename=openFileDialog1.FileName; ??????????????????????????? ?????????????????? lbl_path.Text=filename; ??????????????????????????? ?????????????????????????? ??????????????????????????? ?????????????????????????????????????????????? string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; " + "Data Source =" + filename + ";Extended Properties=Excel 8.0"; ?????????????????????????????????????????????? OleDbConnection myConn = new OleDbConnection(strCon); ?????????????????????????????????????????????? myConn.Open(); ?????????????????????????????????????????????? string strCom = "Select * FROM [Sheet1$] "; ?????????????????????????????????????????????? OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom,myConn); ??????????????????????????? ?????????????????????????????????????????????? myCommand.Fill(dataSet1,"[Sheet1$]"); ?????????????????????????????????????????????? myConn.Close();
?????????????????????????????????????????????? mygrid.SetDataBinding(dataSet1,"[Sheet1$]");
?
?????????????????????????????????????????????? this.BT_ScreenOut.Enabled = true; ?????????????????????????????????????????????? this.btn_Insert.Enabled = false; ???????????????????????????????????? } ??????????????????????????? } ??????????????????????????? catch(System.Exception e) ??????????????????????????? { ???????????????????????????????????? MessageBox.Show(e.Message); ??????????????????????????? } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |