乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 怎样将EXCEL数据表导入到SQL中-sql从excel导入,将excel导入sql数据库

怎样将EXCEL数据表导入到SQL中-sql从excel导入,将excel导入sql数据库

作者:乔山办公网日期:

返回目录:excel表格制作


第一步:登录到 SQL Server Management Studio
第二步:在 “对象资源管理器 ”中右键单击 “管理 ”,在弹出列表中单击 “导入数据 ”
第三步:在 “导入向导 ”对话框中单击 “下一步 ”,进入到 “选择数据源 ”对话框,在 “数据源 ”列表中选择 “Microsoft Excel ”,同时选择相应的 Excel 文档,完成后单击 “下一步 ”(一定要勾选该对话框中的 “首行包含列名称 ”,因此它是将 Excel文档中的列标题为数据库表中的列项标题)
第四步:指定目标数据库服务,依次单击 “下一步 ”。。。。至到 “完成 ”
第五步:重新打到 SQL Server Management Studio,进入到导入的数据库表,可以发现所导入的 Excel文档数据。


最近在一个项目中需要用到Excel文件导入数据库的功能,本人很懒,所以到网上搜了一堆方法,但是通过对比,觉得一下三种是比较好用或者不是很常见的方法,希望对大家有所帮助。

方案一: 通过OleDB方式获取Excel文件的数据,然后通过DataSet中转到SQL Server,这种方法的优点是非常的灵活,可以对Excel表中的各个单元格进行用户所需的操作。

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();  
          
        //获取excel表  
        DataTable dataTable = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);  
 
        //获取sheet名,其中[0][1]...[N]: 按名称排列的表单元素  
        string tableName = dataTable.Rows[0][2].ToString().Trim();  
        tableName = "[" + tableName.Replace("'","") + "]";  
 
        //利用SQL语句从Excel文件里获取数据  
        //string query = "SELECT classDate,classPlace,classTeacher,classTitle,classID FROM " + tableName;  
        string query = "SELECT 日期,开课城市,讲师,课程名称,持续时间 FROM " + tableName;  
        dataSet = new DataSet();  
 
        //OleDbCommand oleCommand = new OleDbCommand(query, oleDbConnection);  
        //OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);  
        OleDbDataAdapter oleAdapter = new OleDbDataAdapter(query,connExcel);  
        oleAdapter.Fill(dataSet,"gch_Class_Info");  
        //从excel文件获得数据后,插入记录到SQL Server的数据表
        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的功能函数e69da5e6ba90e799bee5baa6e79fa5e98193339将Excel文件转换到SQL Server数据库。

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";  
 
    //import excel into SQL Server 2000  
    /*string importSQL = "SELECT * into live41 FROM OpenDataSource" + 
        "('Microsoft.Jet.OLEDB.4.0','Data Source=" + "\"" + "E:\\022n.xls" + "\"" +   
        "; User ID=;Password=; Extended properties=Excel 5.0')...[Sheet1$]";*/ 
 
    //export SQL Server 2000 into excel  
    string exportSQL = @"EXEC master..xp_cmdshell  
'bcp Library.dbo.live41 out " + filePath + "-c -q -S" + "\"" + "\"" +  
        " -U" + "\"" + "\"" + " -P" + "\"" + "\"" + "\'";  
      
    try 
    {  
        sqlConnection1.Open();  
          
        //SqlCommand sqlCommand1 = new SqlCommand();  
        //sqlCommand1.Connection = sqlConnection1;  
        //sqlCommand1.CommandText = importSQL;  
        //sqlCommand1.ExecuteNonQuery();  
        //MessageBox.Show("import finish!");  
          
        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数据到DataSet

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文件导入数据库等,这些都是很常见的方法,因此就不再做收录了。最后说明下,以上的方法是我从网上找的源码并做了一定的修改。



DB:MsSQLServer2005
dbFile:2003 Excel
** 注意事项:
1. 以管理员身份登录SQL Server 外围应用配置器,然后点击功能外围应用配置器选中开启
既席远程调用和ole自动化服务
2. 重新e68a84e799bee5baa6e997aee7ad94364启动mssqlserver 服务
3. 确保dbfile文件处于非活动状态(不要打开或有程序在读)
4.确保dbfile的列数与表结构一致
exec sp_configure 'show advanced options',1

reconfigure

exec sp_configure 'Ad Hoc Distributed Queries',1

reconfigure

EXEC sp_configure 'show advanced options', 1

GO

RECONFIGURE

GO

EXEC sp_configure 'Ad Hoc Distributed Queries', 1

GO

RECONFIGURE

GO

SELECT * from OPENROWSET('MICROSOFT.JET.OLEDB.4.0' , 'Excel5.0;HDR=YES;DATABASE=e:\\testdata.xls',sheet$) ;

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网