c#如何把excel表里的数据倒入sqlserver2000数据库?

bbbb010 2003-11-03 02:11:59
我现在需要用程序把excel表里的数据倒入sqlserver2000数据库中该怎么做?,请高手指点。
...全文
53 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
iamclb 2003-12-11
  • 打赏
  • 举报
回复
using Excel;

visual stiudio里好象添加引用没有excel啊
xfc2003 2003-11-08
  • 打赏
  • 举报
回复
大体上有两种方式:
A 。 使用OleAdo.net或是ADO.net 将Excel看成数据源,然后操纵ADO.Net对象进行数据的检索,然后将检索的数据向SQl Insert就行了。
B 。 在程序中使用Excel COM对象,这样相当有直接操纵Excel,对Excel中数据的读取请找一下(论坛上很多)。
一段小的代码如下:
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel;

namespace excel
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>




class excel
{
public static int Main()
{
Application exc = new Application();
if (exc == null)
{
Console.WriteLine("ERROR: EXCEL couldnt be started!");
return 0;
}

try
{
exc.Visible=false;
Workbooks wbs=exc.Workbooks;
Console.WriteLine("Current workbook counts:"+wbs.Count);
Object mis=System.Reflection.Missing.Value;
wbs.Add(mis);
Console.WriteLine("Current workbook counts:"+wbs.Count);
Workbook wbk=wbs.get_Item(1);
if(wbk==null)
{
Console.WriteLine("Can not get the workbook!");
}
else
{
Sheets wsts=exc.Worksheets;
Console.WriteLine("Total sheets:"+wsts.Count);
wsts.Add(mis,mis,1,mis);
Worksheet wst=(Worksheet)wsts.get_Item(4);
if(wst==null)
{
Console.WriteLine("Can not get worksheet");
}
else
{
///
/// mode a input html
///
String url="URL;file:///e://项目开发//球赛分析//a.html.html";
QueryTables qtbs=wst.QueryTables;
qtbs.Add(url,wst.get_Range("A1","A1"),mis);
QueryTable qtb=(QueryTable)qtbs.Item(1);
qtb.Refresh(false);//=Excel

/*
Object obj=wst.get_Range("C41",mis).Value;
if(obj==null)
{
Console.WriteLine("Obj==Null");
}
else
{
Console.WriteLine("Obj!=Null");
String tmp=(String)obj;
String stmp=(String)tmp.Clone();
Console.WriteLine(stmp);
if(stmp.Length==0)
{
Console.WriteLine("Length==0");
}
else
{
Console.WriteLine("Length<>0");
Console.WriteLine("length of range is:"+wst.get_Range("C41",mis).Count);
Console.WriteLine(stmp+"==Select all - Remove all:"+(stmp=="Select all - Remove all"));
}
}
*/

String tmp="";int check=0;

for(int i=15;i<=60;i++)
{
char ch='C';check=0;
for(;ch<='N';ch++)
{
tmp=ch.ToString()+i.ToString();
Object obj=wst.get_Range(tmp,tmp).Value;
if(obj!=null)
{
Console.Write(obj+"\t");
check++;
}
}
if(check==0)break;
Console.WriteLine();
}
Console.WriteLine("-----------Another tables---------------");

///
///next changed
///
/*
wst=(Worksheet)wsts.get_Item(2);
url="URL;file:///e://项目开发//球赛分析//x.html.html";
wst.QueryTables.Add(url,wst.get_Range("A1","A1"),mis);
qtb=(QueryTable)wst.QueryTables.Item(1);
qtb.Refresh(false);
for(int i=15;i<=50;i++)
{
char ch='F';
Console.WriteLine(i.ToString());
for(;ch<='N';ch++)
{
tmp=ch.ToString()+i.ToString();
tmp=(String)wst.get_Range(tmp,tmp).Value;
//Console.WriteLine(tmp);
Console.Write(tmp+"\t");
}
if(tmp.Length==0) break;
Console.WriteLine();
}
*/

}
wbk.Close(false,"c:\\tmp.xls",mis);
exc.Workbooks.Close();
Console.WriteLine("Finish");
}


/*
Workbook wb=(Workbook)wbs.get_Item(1);
wb.OpenLinks("E:\\项目开发\\球赛分析\\test.xls",mis,mis);
Worksheets wst=(Worksheets)exc.Worksheets;
QueryTables qtb=((Worksheet)wst.get_Item(1)).QueryTables;
qtb.Item(1).Refresh(false);
*/
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
Console.Read();
exc.Quit();
Console.WriteLine("Exiting....");

}


/*
exc.Visible=true;
Workbooks workbooks = exc.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Sheets sheets = workbook.Worksheets;



_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null)
{
Console.WriteLine ("ERROR: worksheet == null");
}

worksheet.QueryTables.Item(1).Refresh(False);
*/
//Console.Read();Console.Read();Console.Read();
return 0;
}
}


}

bbbb010 2003-11-03
  • 打赏
  • 举报
回复
我是新手啊,有没有例子啊。再up
ByWangler 2003-11-03
  • 打赏
  • 举报
回复
一个excel表也是一个对象。你将excel对象作为一个控件导入到项目中,然后查查excel的
方法和属性,慢慢摸索就做的出来了。范例没带,但我记得做的时候看看几个属性和方法(有一个sheet属性..)就可以读取数据了,不难
bbbb010 2003-11-03
  • 打赏
  • 举报
回复
没有高手来看吗?自己up。我不是要读入excel而是要读入数据库
烤火的鱼 2003-11-03
  • 打赏
  • 举报
回复
读入Excel有很多办法,在贴子里搜索一下就可以了。
jxyaolp 2003-11-03
  • 打赏
  • 举报
回复
up
bbbb010 2003-11-03
  • 打赏
  • 举报
回复
我的意思是不用工具用程序实现,读出excel每行里的数据然后插入数据库里面。山风思路正确,但是该怎么写啊
nanker 2003-11-03
  • 打赏
  • 举报
回复
SQL Server2000中有工具,我只导入过Access 没试过Excel的。
实在不行就先用Access导入Excel,再用Sql Server导入Access.
SimonZou 2003-11-03
  • 打赏
  • 举报
回复
思路:把excel表作为数据源,编程读取其中数据,进行整理,再存入SQL Server。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧