![]() Var table = new DataTable("CustomTable") Private static DataTable ConvertListToDataTable(IReadOnlyList list) If (dr != null & ((string)dr = "None" || (string)dr = ""))Ĭonsole.WriteLine("Total number of unique solution number in Excel : " + ) Remove Empty Rows or any specify rows as per your requirementįor (var i = - 1 i >= 0 i-) Var uniqueCols = (true, "Solution Number") To get unique column values, to avoid duplication Var dataTable = ConvertListToDataTable(dataList) Select(tableRow => tableRow.Field("Date").GetString()) Select(tableRow => tableRow.Field("Name").GetString()) ![]() Specify what are all the Columns you need to get from Excel Treat the range as a table (to be able to use the column names) Var range = workSheet.Range(firstPossibleAddress, lastPossibleAddress).AsRange() //.RangeUsed() Get a range with the remainder of the worksheet data (the range used) Var lastPossibleAddress = workSheet.LastCellUsed().Address Var firstPossibleAddress = workSheet.Row(firstRowUsed.RowNumber()).FirstCell().Address Var firstRowUsed = workSheet.FirstRowUsed() Using (var workBook = new XLWorkbook(xmlFile)) ![]() Var xmlFile = Path.Combine(Environment.CurrentDirectory, "Data\TestData.xlsx") Initially we are getting data from Excel and querying the data based on column name and pass it to a List then we are converting it to DataTable using a function ConvertListToDataTable based on your requirement. In this article we will see how to easily fetch data from Excel by column names.Īdd ClosedXML reference to your project, by going to Manage NuGet Package, as shown below.Īs you can see in the below screenshot, we have TestData Excel file in Data folder. ClosedXML is one of the useful dll which is used for accessing Excel, XML files and manipulate those files, to know more about ClosedXML – please refer this github. We utilized ClosedXML to achieve this functionality. We had a requirement to read data from Excel and get it in your code. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |