How to read the xls and xlsx files using c#

user1537319 picture user1537319 · Oct 8, 2012 · Viewed 33.4k times · Source

How to read the xls and xlsx files using c# with OpenXML format Without using the OLEDB connection. I am looking for Open XML format procedure.

Below is the code in which I used the OLEDB preocedure. But I am looking for OpenXML format.

public static DataTable ConvretExcelToDataTable(string FilePath)
{
    string strConn = string.Empty;

     if (FilePath.Trim().EndsWith(".xlsx"))
     {
         strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", FilePath);
     }
     else if (FilePath.Trim().EndsWith(".xls"))
     {
         strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", FilePath);
     }

    OleDbConnection conn = null;
    OleDbCommand cmd = null;
    OleDbDataAdapter da = null;
    DataTable dt = new DataTable();
    try
    {
        conn = new OleDbConnection(strConn);
        conn.Open();
        cmd = new OleDbCommand(@"SELECT * FROM [Sheet1$]", conn);
        cmd.CommandType = CommandType.Text;
        da = new OleDbDataAdapter(cmd);
        da.Fill(dt);
    }
    catch (Exception exc)
    {
        Console.WriteLine(exc.ToString());
        Console.ReadLine();
    }
    finally
    {
        if (conn.State == ConnectionState.Open)
            conn.Close();
        conn.Dispose();
        cmd.Dispose();
        da.Dispose();
    }
    return dt;
}

Requirement is to implement the above conversion in OpenXML format. Thanks.

Answer

Paul Grimshaw picture Paul Grimshaw · Oct 8, 2012

You'll want the OpenXml SDK for the xlsx:

http://www.microsoft.com/en-gb/download/details.aspx?id=30425

But for the XLS, you won't be able to use this the XLS format is not based on xml.

I use the NPOI library for accessing older files:

http://npoi.codeplex.com/

The NPOI library also supports xlsx, so this would give you a consistent way of accessing them. Downside is you'll have to loop through sheets/rows/columns manually, and build up the dataset which will probably affect performance if you have large workbooks. If you want to use queries to access the data, OLEDB is the only method I've found.