大体上有两种方式:
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;
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"));
}
}
*/