Use Tuple to send Excel Column headers and data row from same function using C#

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 509
public Tuple<DataTable,List<string>> ReadXcel(string fileName, string sheetName, string tblName)
{
var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=Excel 12.0;";
var adapter = new OleDbDataAdapter("SELECT * FROM [" + sheetName + "]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, tblName);

var dt = ds.Tables[tblName];

var excelColumnHeaders= dt.Columns.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToList();

return Tuple.Create(dt, excelColumnHeaders);
}

Comments or Responses

Login to post response