Created
November 2, 2014 22:13
-
-
Save jumbojet/af0e723352e11d42bfde to your computer and use it in GitHub Desktop.
Load Excel for Data Analysis through C# Program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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