最近在一個項目中須要用到Excel文件導入數據庫的功能,本人很懶,因此到網上搜了一堆方法,可是經過對比,以爲一下三種是比較好用或者不是很常見的方法,但願對你們有所幫助。c++
方案一: 經過OleDB方式獲取Excel文件的數據,而後經過DataSet中轉到SQL Server,這種方法的優勢是很是的靈活,能夠對Excel表中的各個單元格進行用戶所需的操做。sql
- openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = "Excel files(*.xls)|*.xls";
-
- if(openFileDialog.ShowDialog()==DialogResult.OK)
- {
- FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
- string filePath = fileInfo.FullName;
- string connExcel = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0";
-
- try
- {
- OleDbConnection oleDbConnection = new OleDbConnection(connExcel);
- oleDbConnection.Open();
-
-
- DataTable dataTable = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
-
-
- string tableName = dataTable.Rows[0][2].ToString().Trim();
- tableName = "[" + tableName.Replace("'","") + "]";
-
-
-
- string query = "SELECT 日期,開課城市,講師,課程名稱,持續時間 FROM " + tableName;
- dataSet = new DataSet();
-
-
-
- OleDbDataAdapter oleAdapter = new OleDbDataAdapter(query,connExcel);
- oleAdapter.Fill(dataSet,"gch_Class_Info");
-
- DataTable dataTable1 = new DataTable();
-
- SqlDataAdapter sqlDA1 = new SqlDataAdapter(@"SELECT classID, classDate,
- classPlace, classTeacher, classTitle, durativeDate FROM gch_Class_Info",sqlConnection1);
-
- //SqlCommandBuilder sqlCB1 = new SqlCommandBuilder(sqlDA1);
-
- sqlDA1.Fill(dataTable1);
-
- foreach(DataRow dataRow in dataSet.Tables["gch_Class_Info"].Rows)
- {
- DataRow dataRow1 = dataTable1.NewRow();
-
- dataRow1["classDate"] = dataRow["日期"];
- dataRow1["classPlace"] = dataRow["開課城市"];
- dataRow1["classTeacher"] = dataRow["講師"];
- dataRow1["classTitle"] = dataRow["課程名稱"];
- dataRow1["durativeDate"] = dataRow["持續時間"];
-
- dataTable1.Rows.Add(dataRow1);
- }
-
- Console.WriteLine("新插入 " + dataTable1.Rows.Count.ToString() + " 條記錄");
- sqlDA1.Update(dataTable1);
-
- oleDbConnection.Close();
-
- }
- catch(Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- }
方案二: 直接經過SQL語句執行SQL Server的功能函數將Excel文件轉換到SQL Server數據庫。shell
OpenFileDialog openFileDialog = new OpenFileDialog(); 數據庫
- openFileDialog.Filter = "Excel files(*.xls)|*.xls";
-
- SqlConnection sqlConnection1 = null;
-
- if(openFileDialog.ShowDialog()==DialogResult.OK)
- {
- string filePath = openFileDialog.FileName;
-
- sqlConnection1 = new SqlConnection();
- sqlConnection1.ConnectionString = "server=(local);integrated security=SSPI;initial catalog=Library";
-
-
-
-
-
- string exportSQL = @"EXEC master..xp_cmdshell
- 'bcp Library.dbo.live41 out " + filePath + "-c -q -S" + "\"" + "\"" +
- " -U" + "\"" + "\"" + " -P" + "\"" + "\"" + "\'";
-
- try
- {
- sqlConnection1.Open();
-
-
-
-
-
-
-
- SqlCommand sqlCommand2 = new SqlCommand();
- sqlCommand2.Connection = sqlConnection1;
- sqlCommand2.CommandText = exportSQL;
- sqlCommand2.ExecuteNonQuery();
- MessageBox.Show("export finish!");
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
-
- if(sqlConnection1!=null)
- {
- sqlConnection1.Close();
- sqlConnection1 = null;
- }
方案三: 經過到入Excel的VBA dll,經過VBA接口獲取Excel數據到DataSetide
- OpenFileDialog openFile = new OpenFileDialog();
- openFile.Filter = "Excel files(*.xls)|*.xls";
-
- ExcelIO excelio = new ExcelIO();
-
- if(openFile.ShowDialog()==DialogResult.OK)
- {
- if(excelio!=null)
- excelio.Close();
-
- excelio = new ExcelIO(openFile.FileName);
- object[,] range = excelio.GetRange();
- excelio.Close();
-
-
- DataSet ds = new DataSet("xlsRange");
-
- int x = range.GetLength(0);
- int y = range.GetLength(1);
-
- DataTable dt = new DataTable("xlsTable");
- DataRow dr;
- DataColumn dc;
-
- ds.Tables.Add(dt);
-
- for(int c=1; c<=y; c++)
- {
- dc = new DataColumn();
- dt.Columns.Add(dc);
- }
-
- object[] temp = new object[y];
-
- for(int i=1; i<=x; i++)
- {
- dr = dt.NewRow();
-
- for(int j=1; j<=y; j++)
- {
- temp[j-1] = range[i,j];
- }
-
- dr.ItemArray = temp;
- ds.Tables[0].Rows.Add(dr);
- }
-
- dataGrid1.SetDataBinding(ds,"xlsTable");
-
- if(excelio!=null)
- excelio.Close();
- }
固然還有其餘一些方法,如遍歷Excel文件中的數據而後構造sql語句,直接利用sql操做Excel文件導入數據庫等,這些都是很常見的方法,所以就再也不作收錄了。最後說明下,以上的方法是我從網上找的源碼並作了必定的修改。函數