有多种方式这里介绍第一种: 这里是完整的例子、很简单就不仔细写了
protected void BuploadTeacher() { //OpenFileDialog openFileDialog = new OpenFileDialog(); // openFileDialog.Filter = "Excel files(*.xls)|*.xls"; SqlConnection sqlConnection1 = null; string filePath = this.FImportStudent.FileName; sqlConnection1 = new SqlConnection(); sqlConnection1.ConnectionString=ConfigurationManager.ConnectionStrings["ThesisConnectionString"].ConnectionString; //export SQL Server 2000 into excel /*string exportSQL = @"EXEC master..xp_cmdshell 'bcp Library.dbo.live41 out " + filePath + "-c -q -S" + "/"" + "/"" + " -U" + "/"" + "/"" + " -P" + "/"" + "/"" + "/'";*/ string importSQL = "SELECT * into test1 FROM OpenDataSource" + "('Microsoft.Jet.OLEDB.4.0','Data Source=" + "/"" + "E://a.xls" + "/"" + "; User ID=;Password=; Extended properties=Excel 5.0')...[Sheet1$]"; try { sqlConnection1.Open(); SqlCommand sqlCommand2 = new SqlCommand(); sqlCommand2.Connection = sqlConnection1; sqlCommand2.CommandText = importSQL; sqlCommand2.ExecuteNonQuery(); } catch (Exception ex) { } if (sqlConnection1 != null) { sqlConnection1.Close(); sqlConnection1 = null; }
}