首页 新闻 会员 周边

如何把Excel工作表里的数据传到数据库

0
悬赏园豆:50 [已解决问题] 解决于 2013-03-13 20:48

我想把Excel里面的数据导入到sql server 数据库里面去,可以怎么来实现

lgfalmh的主页 lgfalmh | 初学一级 | 园豆:2
提问于:2013-03-11 10:58
< >
分享
最佳答案
0

sql server 自带了数据导入向导,运行 dtswizard 即可快速实现

收获园豆:50
陈希章 | 老鸟四级 |园豆:2538 | 2013-03-11 14:29
其他回答(5)
0

.net里面有oledb的操作函数啊!不会就上网一搜就有了,写一个函数把excel的数据按照规则用sql语句insert到数据库,和excel导出功能也差不多!如果你上网不会搜就+我QQ:635120811。。。。

大肥籽 | 园豆:140 (初学一级) | 2013-03-11 11:14
0

这个应该不是难点,在网上确实有好多的例子,我前段时间用到的就是,先将excel上传到服务器,然后根据路径读取内容到DataTable里 然后在一天一天用事务存到数据库中,

private DataTable GetTableFromTXT(string filePath, string filename)
    {
        DataSet ds;
        string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                        "Extended Properties=Excel 8.0;" +
                        "data source=" + filePath + filename;
        OleDbConnection myConn = new OleDbConnection(strCon);
        string strCom = " SELECT * FROM [Worksheet$]";
        myConn.Open();
        OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
        ds = new DataSet();
        myCommand.Fill(ds);
        myConn.Close();
        File.Delete(filePath + filename);
        return ds.Tables[0];
        
    }

这个可以读出来的,filePath是路径,filename是名称。

 public bool TableInsertSQL(DataTable table)
        {
            bool result = false;
            bool flag = false;
            string str = "";
            TransactionManager tm = DataRepository.Provider.CreateTransaction();
            try
            {
                tm.BeginTransaction();
                for (int j = 3; j < table.Rows.Count; j++)
                {
                    Webkeywordsranking webkeywords = new Webkeywordsranking();

                       webkeywords.Id = Guid.NewGuid();
                        webkeywords.Baiduspider = Convert.ToInt32(table.Rows[j][2]);
                        webkeywords.Googlespider = Convert.ToInt32(table.Rows[j][4]);
                        PublicCookie cookies = new PublicCookie();
                        webkeywords.Editor = cookies.GetCurrentPublicWay().Username;
                        webkeywords.Createman = cookies.GetCurrentPublicWay().Username;
                        webkeywords.Editortime = DateTime.Parse(datenow);
                        webkeywords.Createtime = DateTime.Parse(datenow);
                    }
                    flag = DataRepository.WebkeywordsrankingProvider.Insert(tm, webkeywords);                    
                }
                if (flag)
                {
                    tm.Commit();
                    result = true;
                }
                else
                {
                    tm.Rollback();
                }
            }
            catch
            {
                tm.Rollback();
                return false;
            }
            return result;
        }

这样能加到数据库中,你看看你可以用不。

W宁宁 | 园豆:522 (小虾三级) | 2013-03-11 14:36

这个事务添加我就是意思意思,我们的程序的架构比较简单,向数据库添加你应该会吧

支持(0) 反对(0) W宁宁 | 园豆:522 (小虾三级) | 2013-03-11 14:38

看来一开始我的想法就是误导了,呵呵呵。。。我尝试下

支持(0) 反对(0) lgfalmh | 园豆:2 (初学一级) | 2013-03-13 20:46
0
protected DataTable ExcelToDT(string fileName)
        {
            string str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName +
                         ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";
            OleDbConnection conn = new OleDbConnection(str);
            DataTable exceldt = new DataTable();
            try
            {
                conn.Open();
                DataTable tableschema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);//获取excel表中的标题
                string sheetname = tableschema.Rows[0][2].ToString().Trim();//获取excel表中第一个Sheet的名称
                string oledbstr = "select * from [" + sheetname + "]";
                OleDbCommand com = new OleDbCommand(oledbstr, conn);
                OleDbDataAdapter adp = new OleDbDataAdapter(com);
                adp.Fill(exceldt);
                conn.Close();
                File.Delete(fileName);
                exceltodata = true;
            }
            catch
            {
                //显示错误信息
                //throw(e);
                exceltodata = false;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            return exceldt;
        }

上面的代码实现的是将excel中的内容导入datatable中,然后你只需要遍历将datatable中的数据导入数据库就可以。注意,代码中使用的是ACE接口,支持2003和2007,需要你的电脑上安装有office2007(你自己安装了ACE组件也行)。

allon6318 | 园豆:858 (小虾三级) | 2013-03-11 15:48
0

三个解决办法,

1.NPOI,最简单的办法

下载地址

2003版本  http://download.csdn.net/detail/jm19890727/3551430

2007-2010版本  http://download.csdn.net/detail/jm19890727/4420467

2.oledb,微软出品,网上应该有现成的oledbHelper,

3.直接利用数据库导入导出功能,(手工实现)

只会造轮子 | 园豆:2274 (老鸟四级) | 2013-03-11 18:04
0

用这个Aspose插件,可以读取Excel数据,网上也有很多案例

*^__^* | 园豆:428 (菜鸟二级) | 2013-03-13 11:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册