工作之余,把做工程过程中重要的代码段做个记录,如下的资料是关于C#使用Ado.net读取Excel表的代码,应该能对各位有所帮助。

using System;
using System.Data.OleDb;
namespace ConsoleApplication1
{
  class Program
  {
    static void Main()
    {
      string connString = "Provider=Microsoft.Jet.OleDb.4.0; data source=c:\sample.xls; Extended Properties=Excel 8.0;";

      OleDbConnection conn = new OleDbConnection(connString);
      OleDbCommand cmd = new OleDbCommand(selectqry,con);

      try
      {
        con.Open();
        OleDbDataReader theDatardr = cmd.ExecuteReader();
        while (theDatardr.Read())
        {
          Console.WriteLine("{0}: {1} ({2}) – {3} ({4})", theDatardr.GetString(0),theDatardr.GetString(1),theDatardr.GetString(2),theDatardr.GetString(3),theDatardr.GetString(4));
        }
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
      }
      finally
      {
        con.Dispose();
      }
    }
  }
}