Skip to content

Instantly share code, notes, and snippets.

@cguldogan
Last active March 29, 2016 13:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cguldogan/4a652a58455cd4fdc12b to your computer and use it in GitHub Desktop.
Save cguldogan/4a652a58455cd4fdc12b to your computer and use it in GitHub Desktop.
Read Excel in C#
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