| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4938 人关注过本帖, 7 人收藏
标题:将Excel文件数据库导入SQL Server的三种方案
只看楼主 加入收藏
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
结帖率:66.67%
收藏(7)
 问题点数:0 回复次数:21 
将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();
}





样例源代码:(注意:此代码与上面的代码略有出入)

hyfIeG9Y.zip (20.5 KB) 将Excel文件数据库导入SQL Server的三种方案



[此贴子已经被作者于2006-12-2 13:56:40编辑过]

搜索更多相关主题的帖子: Excel文件 SQL 数据库 Server Microsoft 
2006-12-02 13:48
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 

其中,方案三中使用到的类方法如下:

using System;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
using Excel;

namespace Excel2SQL
{
public class ExcelIO
{
private Excel.ApplicationClass oExcel;
private Excel.Workbook oBook;
private Excel.Worksheet oSheet;
private Excel.Range oRange;

private object missing = Type.Missing;

public ExcelIO()
{
}

public ExcelIO(string inFile)
{
object fileName = inFile;

oExcel = new Excel.ApplicationClass();
oBook = null;
oSheet = null;
oRange = null;

oExcel.Visible = false;
oExcel.ScreenUpdating = false;
oExcel.DisplayAlerts = false;

oBook = oExcel.Workbooks.Open(inFile, missing, missing,
missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing);
}

public object[,] GetRange()
{
oSheet = (Worksheet)oBook.Worksheets[1];
oRange = oSheet.get_Range("A1", missing);
oRange = oRange.get_End(Excel.XlDirection.xlToRight);
oRange = oRange.get_End(Excel.XlDirection.xlDown);
string downAddress = oRange.get_Address(false, false, XlReferenceStyle.xlA1, missing, missing);
oRange = oSheet.get_Range("A1", downAddress);
object[,] values = (object[,])oRange.Value2;

return values;
}

public void Close()
{
oRange = null;
oSheet = null;
if(oBook != null)
oBook.Close(false, missing, missing);
oBook = null;
if(oExcel != null)
oExcel.Quit();
oExcel = null;
}
}
}

2006-12-02 13:49
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
收藏
得分:0 
                  

2006-12-02 14:19
人妖123
Rank: 1
等 级:新手上路
威 望:2
帖 子:462
专家分:0
注 册:2006-11-8
收藏
得分:0 
gfgd

你自归家我自归,说着如何过,我断不思量,你莫思量我。将你从前予我心,付与他人可。
2006-12-05 09:08
人妖123
Rank: 1
等 级:新手上路
威 望:2
帖 子:462
专家分:0
注 册:2006-11-8
收藏
得分:0 

再次做个记号,不错呀


你自归家我自归,说着如何过,我断不思量,你莫思量我。将你从前予我心,付与他人可。
2006-12-05 20:13
YSKING
Rank: 5Rank: 5
来 自:中国绿城
等 级:贵宾
威 望:16
帖 子:1380
专家分:25
注 册:2006-11-11
收藏
得分:0 
谢谢版主共享,我也不用重新发帖了~

仍然自由自我,永远高唱我歌,走遍千里...
2006-12-06 13:09
klfo
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2006-11-7
收藏
得分:0 

2006-12-07 02:07
qianxiaogang
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2006-12-5
收藏
得分:0 
斑主,能否给我发给例子。让我研究,学习一下。谢谢了
我的邮箱:qianxiaogang@hotmail.com
2006-12-07 11:36
YSKING
Rank: 5Rank: 5
来 自:中国绿城
等 级:贵宾
威 望:16
帖 子:1380
专家分:25
注 册:2006-11-11
收藏
得分:0 

上面不是有下载了吗


仍然自由自我,永远高唱我歌,走遍千里...
2006-12-07 11:45
chjz21
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2006-12-5
收藏
得分:0 
实在是高,啥时候我才能学到这程度呢?
正需这样的转换接口
谢谢!
2006-12-07 12:41
快速回复:将Excel文件数据库导入SQL Server的三种方案
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018800 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved