Skip to content

Instantly share code, notes, and snippets.

@jumbojet
Created November 2, 2014 22:13
Show Gist options
  • Save jumbojet/af0e723352e11d42bfde to your computer and use it in GitHub Desktop.
Save jumbojet/af0e723352e11d42bfde to your computer and use it in GitHub Desktop.
Load Excel for Data Analysis through C# Program
using System.Data.OleDb;
using System.Data;
protected void Button1_Click(object sender, EventArgs e)
{
string fileName = "C:\\Enter\\Location\\filename.xls"; // Need to enter the file location - note xls file extension
var conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
fileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
/*In case you have .xlsx file try
conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
fileName + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; */
// This is the name of your worksheet you want to open
var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", conString);
var ds = new DataSet();
adapter.Fill(ds, "TestData");
DataTable data = ds.Tables["TestData"];
// Now we have data in table format ready to be analyzed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment