| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1638 人关注过本帖
标题:[求助]如何导入导出数据库中的内容呢?
只看楼主 加入收藏
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 
四楼和五楼的兄弟!!

海鸽 is My Lover!!
2007-02-02 08:55
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
收藏
得分:0 
你首先要引用EXCEL类库,不同的OFFICE类库有不同的版本,我的是9.0的,引用类库吧先。

XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-02-02 09:04
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 
下面的就是我引用的类库,应该够了吧,可还有那个错误啊!!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using Microsoft.Office ;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Tools;
using Microsoft.Office.Tools.Outlook;

海鸽 is My Lover!!
2007-02-02 09:19
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 
我明白了,你是让我添加下面的东西:
Microsoft.Office.Tools.Excel
Microsoft.Office.Tools.Common
Microsoft.Office.Tools.Outlook
Microsoft.Office.Object.Library

我都添加了,还是有同样的问题
是不是还少点什么?

海鸽 is My Lover!!
2007-02-02 09:27
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
收藏
得分:0 
天啊,吓人,

XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-02-02 09:27
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
收藏
得分:0 

用我这个方法吧。
//方案一: 通过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();
}


XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-02-02 09:28
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 
怎么??
我把该有的都放在上面了
请指教啊!!

海鸽 is My Lover!!
2007-02-02 09:32
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 
好的,先谢谢你啊
我看看程序

海鸽 is My Lover!!
2007-02-02 09:33
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 
请问jacklee,我怎么用你的方法不能导出啊?
是不是首先要做一个Excel模版啊?还是要把Excel放在指定盘里?

海鸽 is My Lover!!
2007-02-02 13:04
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 
人呢??

海鸽 is My Lover!!
2007-02-02 13:59
快速回复:[求助]如何导入导出数据库中的内容呢?
数据加载中...
 
   



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

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