将Excel文件数据库导入SQL Server的三种方案//方案一: 通过OleDB方式获取Excel文件的数据,然后通过DataSet中转到SQL Server
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");
//dataGrid1.DataSource = dataSet;
//dataGrid1.DataMember = tableName;
dataGrid1.SetDataBinding(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导入SQLserver源码 Excel是一种非常灵活的电子表格软件,用它可以存储各种数据,本节将对如何将Excel导入SQL Server2000数据库进行详细介绍。 开发环境:NET2.0 开发工具:vs2005 开发语言:c# 数据库:server...
WInform 程序 将Excel导入SQLSERVER数据库
将Excel数据导入到SQLServer中,可以选择要导入的Excel文件和Sheet名称,表名。通过自动编写存储过程来实现导入功能。实际使用无须这么麻烦,可以作为数据导入和存储过程参考例子。
利用C#编程将Excel中的数据导入到Sqlserver数据库中
在界面上有一个TextField,一个浏览按钮,一个导入按钮。当选择浏览按钮时,弹出文本选择窗口,选择Excel文件之后,将路径显示在TextField中。点击导入,将Excel中的数据保存到SqlServer数据库中。
ASP.NET将EXCEL导入SQL SERVER数据库的代码 ASP.NET将EXCEL导入SQL SERVER数据库的代码
使用Asp.Net开发,用于导入Excel文件到SQLServer,支持配置导入字段。支持主键为Int的外键。 两种方式:1、先导入Excel再配置对应关系 2、先配置对应关系在导入
excel导入sql server数据库源码
将txt与excel文件里的数据导入SQLServer数据库的sql代码,很实用
将DBF文件导入到SQL server 数据库,并且导出到本地的excel
jxl将excel文件导入sqlserver中,很好用
Excel导入SQLserver源码_excel2sql
最近在做个小东西的时候用到的,网上有很多但大多是文档,所以自己照着改了改,开发环境:vs2013 excel版本:office excel 2013,可能写的比较糙,有什么有问题的地方希望能够指出来,共同进步。
excel导入到SqlServer,具体讲:asp.net实现将Excel文件导入到SQL数据库的三种解决方案
可以导入Excel的多个工作表的数据; 无法将相同的表导入数据库多次,也就是说只能导入数据库一次,无法覆盖和添加。
excel数据导入到sql server数据库中,
我是新手一枚,做了个winform导入Excel到数据库的小东西,分享给大家,请大家指点下。 文件里面有我的导入的Excel文件,估计只能导这样的吧。不过改改还是能导别的数据的。
sql server 直接读取excel ,不需要把excel导入数据库。直接进行查询或者插入临时表后操作,包含32位及64位
excel文件导入到SQL server数据库,先选择excel文件,然后选择数据库的ip,默认是本地,填写用户名,密码,然后才可连接自己要导入数据的数据库。下面选择要导入数据的数据库表,然后在对应的datagridview控件的一列...
有关excel文件导入到SQL Server数据库的代码