Re: Reading an Excel file in C#

Alan Baljeu <[email protected]> Fri, 13 Apr 2007 17:07:27 -0400
Newsgroups gmane.comp.windows.devel.dotnet.cx
Message-ID <03fb01c77e0f$be1e8970$1dc80ac6@CTI>
Try this:

using System.Data.OleDb;
using System.Data;

namespace ee
{
  public class ExcelDataReader
  {
    public readonly DataSet excelData ;

    public ExcelDataReader(string fileName)
    {
      excelData = new DataSet();
      Connect(fileName);
    }
    private void Connect(string fileName)
    {
      string sConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source={0};"+
            "Extended Properties=Excel 8.0;", fileName);

      OleDbConnection objConn = new OleDbConnection(sConnectionString);
      objConn.Open();

      try
      {
        OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM [VMM$]", objConn);
        OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
        objAdapter1.SelectCommand = objCmdSelect;
        objAdapter1.Fill(excelData, "XLData");
      }
      finally
      {
        objConn.Close();
      }
    }
  }
}

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com