ADO.Net连接Excel的方法代码

2007-12-06  来源:   浏览次数 26
    首先创建一个Excle表,把Sheet1重命名为:student,在此sheet中创建两个列,一列名为stuid,另一列为stuname,最后在里面输入几行测试数据.保存为student.xls关闭.

    创建一个C#控制台项目,然后在Debug目录下建立另外一个目录,Excel,将student.xls文件放置在其中.然后在代码文件中键入如下代码,中国自学编程网,www.zxbc.cn :

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Odbc;

namespace ConnectExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            OdbcConnection con = new OdbcConnection(@"Driver={Microsoft Excel Driver (*.xls)};DBQ=Excel/student.xls");
        //    OdbcDataAdapter da=new OdbcDataAdapter();

            OdbcCommand com = new OdbcCommand("select stuid,stuname from [student$]", con);
            con.Open();
              OdbcDataReader dr = com.ExecuteReader();
            while (dr.Read())
            {
               Console.WriteLine(dr[0]);
               Console.WriteLine(dr[1]);
            }
            dr.Close();
            con.Close();

        }
    }
}

上一篇:ADODB.Stream 错..    下一篇:VS.Net2005连接Or..

相关主题:

网友评论