将Excel文件数据导入到SqlServer数据库的三种方案
方案一: 通过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的功能函数将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文件导入数据库等,这些都是很常见的方法,因此就不再做收录了。最后说明下,以上的方法是我从网上找的源码并做了一定的修改,希望对大家有帮助。
将Excel文件数据导入到SqlServer数据库的三种方案的更多相关文章
- Excel表格数据导入到SQLServer数据库
转载:http://blog.csdn.net/lishuangzhe7047/article/details/8797416 步骤: 1,选择要插入的数据库--右键--任务--导入数据 2,点击下一 ...
- 怎样把excel的数据导入到sqlserver2000数据库中
在做程序的时候有时需要把excel数据导入到sqlserver2000中,以前没从外部导入过数据,今天刚做了一下导入数据,感觉还是蛮简单的,没做过之前还想着多么的复杂呢,下面就来分享一下我是如何把ex ...
- Excel表数据导入Sql Server数据库中
Excel表数据导入Sql Server数据库的方法很多,这里只是介绍了其中一种: 1.首先,我们要先在test数据库中新建一个my_test表,该表具有三个字段tid int类型, tname nv ...
- java读取excel文件数据导入mysql数据库
这是我来公司的第二周的一个小学习任务,下面是实现过程: 1.建立maven工程(方便管理jar包) 在pom.xml导入 jxl,mysql-connector 依赖 可以在maven仓库搜索 2.建 ...
- Excel文件数据导入到后台保存倒数据库
后台代码数据解析: 方法一: (简单点) import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermo ...
- 使用navicat for sqlserver 把excel中的数据导入到sqlserver数据库
以前记得使用excel向mysql中导入过数据,今天使用excel向sqlserver2005导入了数据,在此把做法记录一下 第一步:准备excel数据,在这个excel中有3个sheet,每个she ...
- 将 excel文件数据导入MySQL数据库中
第一步:先将Excel文件另存为文本文件(制表符分割) 第二步:将生成的txt文件另存,并修改编码格式utf8; 第三步:将文件放到指定位置,或自己想要的位置: G:\city.txt 第四步:避免创 ...
- Excel 数据导入至Sqlserver 数据库中 ltrim() 、rtrim() 、replace() 函数 依次空格无效问题
今天导一些数据从Excel中至Sqlserver 数据库中,在做数据合并去重的时候发现,有两条数据一模一样,竟然没有进行合并: 最后发现有一条后面有个“空格”,正是因为这个“空格”让我抓狂许久,因为它 ...
- pl/sql 如何将Excel文件数据导入oracle的数据表?
1.准备导入数据的excel文件 注意:excel列名和数据表列名必须相同,excel文件sheet2和sheet3可以删除 1)excel文件格式 2)数据表格式 2.打开pl/sql ,找到工具- ...
随机推荐
- linux备份文件脚本
#!/bin/sh #Author: Opal TODAY=`date +%Y%m%d` YESTERDAY=`date -d"-1 day" +%Y%m%d` mkdir -p ...
- 用git从github网站上下载代码的方式
原本单击如下下载按钮即可 但有时候github异常,该按钮无效,可以使用如下方法: 1.复制url,如https://github.com/ulli-kroll/mt7610u 2.进入要存放该代码的 ...
- ocs的沟通平台
Microsoft Office Communications Server 2007 R2 简称:OCS准时准确地联系人员以及管理信息过载根据人员的状态与其联系,然后单击最佳方式与其通信:通过电子邮 ...
- 1.ElasticSearch介绍及基本概念
一.ElasticSearch介绍 一个采用RESTful API标准的高扩展性的和高可用性的实时性分析的全文搜索工具 基于Lucene[开源的搜索引擎框架]构建 ElasticSearch是一个面向 ...
- spring两大核心对象IOC和AOP(新手理解)
现在自己对spring的理解是:spring的主要的作用是用来进行业务的处理和实现类与类之间的解耦. 其中实现解耦功能 是IOC(控制反转)通过sessionfactory工厂来为需要的对象注入对应的 ...
- CSS滤镜效果
使用 filter: blur() 生成毛玻璃效果 使用 filter: drop-shadow() 生成整体阴影效果 使用 filter: opacity() 生成透明度 blur生成阴影 通常我们 ...
- Java集合框架,未完
一.集合类 集合的由来: 面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象的操作,就需要将对象进行存储,集合就是存储对象最常用的一种方式. 集合特点:1,用于存储对象的容器.(容器本身就是 ...
- 吾八哥学Python(三):了解Python基础语法(上)
学习一门开发语言首先当然是要熟悉它的语法了,Python的语法还算是比较简单的,这里从基础的开始了解一下. 标识符1.第一个字符必须是字母表中字母或下划线'_'.2.标识符的其他的部分有字母.数字和下 ...
- Table样式设置
<table class="listTable"> <tr><th width="40px">序号</th>&l ...
- [bzoj2131]免费的馅饼 树状数组优化dp
2131: 免费的馅饼 Time Limit: 10 Sec Memory Limit: 259 MB[Submit][Status][Discuss] Description Input 第一行是 ...