C#中访问Excel(xls)数据源的例程

来源:互联网 发布:瑞士生活知乎 编辑:程序博客网 时间:2024/05/02 15:55
 
static void connectToXls()        {            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=e:/tmp/123.Xls;Extended Properties=Excel 8.0;";            String strSql = "select * from [sheet1$]";            using (OleDbConnection Conn = new OleDbConnection(strConn))            {                OleDbCommand command = new OleDbCommand(strSql);                command.Connection = Conn;                                try                {                    Conn.Open();                    Console.WriteLine(Conn.ToString());                    Object ob = command.ExecuteScalar();                    Console.WriteLine(ob.GetType());                    Console.WriteLine(ob);                }                catch (Exception e)                {                    Console.WriteLine(e.Message);                }                                Conn.Close();            }        }