Last active
March 29, 2016 13:06
-
-
Save cguldogan/4a652a58455cd4fdc12b to your computer and use it in GitHub Desktop.
Read Excel in C#
This file contains hidden or 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
public class ExcelOperations | |
{ | |
public List<List<string>> Read(string filePath) | |
{ | |
OleDbConnection conn = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + filePath + "; Extended Properties = 'Excel 8.0; HDR=NO'"); | |
OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", conn); | |
conn.Open(); | |
OleDbDataReader dr = cmd.ExecuteReader(); | |
List<List<string>> data = new List<List<string>>(); | |
while (dr.Read()) | |
{ | |
var obj = dr.GetValue(0); | |
if (obj == DBNull.Value) continue; | |
var shortList = new List<string>(); | |
for (int i = 0; i < dr.FieldCount; i++) | |
{ | |
shortList.Add(dr[i].ToString()); | |
} | |
data.Add(shortList); | |
} | |
conn.Close(); | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment