Skip to content

Instantly share code, notes, and snippets.

@Microsofttechies
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save Microsofttechies/3286110b0c0989a1f918 to your computer and use it in GitHub Desktop.

Select an option

Save Microsofttechies/3286110b0c0989a1f918 to your computer and use it in GitHub Desktop.
C# read excel OleDbConnection connection for .xls and .xlsx
using System.Threading.Tasks;
using System.Data.OleDb;
using System.Data;
using System.IO;
//Code
OleDbConnection oledbConn;
string path = "C:\\MyExcel.xlsx";
if (Path.GetExtension(path) == ".xls")
{
oledbConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
}
else if (Path.GetExtension(path) == ".xlsx")
{
oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
}
oledbConn.Open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment