excel表如何导入到数据库中
excel表如何导入到数据库中,希望做过这种程序的朋友能给我些代码,实例更好,帮帮忙……
程序代码:
/*读到DataTable里 用oledb方式读取excel到datatable,然后再写入数据库。 */ 1.private DataTable GetData(string strPath) 2.{ 3. DataTable dtbl = new DataTable(); 4. try 5. { 6. string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strPath + ";" + "Extended Properties=Excel 8.0;"; 7. string strSheetName = ""; 8. using (OleDbConnection con = new OleDbConnection(strCon)) 9. { 10. con.Open(); 11. DataTable dtbl1 = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 12. //dataGridView2.DataSource = dtbl1; 13. strSheetName = dtbl1.Rows[0][2].ToString().Trim(); 14. } 15. String strCmd = "select * from [" + strSheetName + "]"; 16. OleDbDataAdapter cmd = new OleDbDataAdapter(strCmd, strCon); 17. cmd.Fill(dtbl); 18. } 19. catch (Exception ex) { MessageBox.Show(ex.Message); } 20. return dtbl; 21.}