Tutorial: Read Excel file content from C# (*.xls, *.xlsx)

Use the following code/connection string in your application to read both xls and xlsx file types:

private void ImportExcelButton_Click(object sender, EventArgs e)
    FileDialog dialog = GetOpenFileDialog(".xls,.xlsx", "Excel Files (*.xls, *.xlsx)|*.xls,*.xlsx");
    DialogResult result = dialog.ShowDialog();
    string xlConn = string.Empty;

    if (Path.GetExtension(dialog.FileName).Equals("xls"))
        xlConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + dialog.FileName + ";Extended Properties=Excel 8.0;";
        xlConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dialog.FileName + ";Extended Properties="Excel 12.0;";";

    using (OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", xlConn))
        using (DataSet myDataSet = new DataSet())

            foreach (var row in myDataSet.Tables[0].Rows)

One thought on “Tutorial: Read Excel file content from C# (*.xls, *.xlsx)

  1. Good post. I learn something new and challenging on websites I stumbleupon
    everyday. It will always be interesting to read through
    articles from other authors and use something from other websites.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s