I want to read excel file using gembox like first row as header to check weather required column are present or not.
Asked
Active
Viewed 591 times
1 Answers
0
You can find the reading example here.
Also, here is another example of how you can read just the first row:
var workbook = ExcelFile.Load("Sample.xlsx");
var worksheet = workbook.Worksheets[0];
var firstRow = worksheet.Rows[0];
foreach (var cell in firstRow.AllocatedCells)
{
Console.WriteLine(cell.Name);
Console.WriteLine(cell.Value);
Console.WriteLine("---");
}
I hope this helps.
UPDATE
Another attempt to guess what exactly is requested here:
string[] expectedColumns = new string[] { "Column 1", "Column 2", "Column 3" };
var workbook = ExcelFile.Load("Sample.xlsx");
var worksheet = workbook.Worksheets[0];
var firstRow = worksheet.Rows[0];
string[] actualColumns = firstRow.AllocatedCells
.Select(cell => cell.Value != null ? cell.Value.ToString() : string.Empty)
.ToArray();
for (int i = 0; i < expectedColumns.Length; i++)
if (expectedColumns[i] != actualColumns[i])
throw new Exception("Unexpected column name detected!");
Note, Select
and ToArray
methods are provided by System.Linq
.

Mario Z
- 4,328
- 2
- 24
- 38
-
thanks but i want to read first row as a header to check weather any clolum is missing or not – Kaushik Nandwana Aug 31 '18 at 06:49
-
Apologize, but I don't know what you mean by "as a header", can you explain that a bit more? Note, all rows are read in the same way, as shown in my answer. Nevertheless, here is an update that I'm guessing could be of use to you. – Mario Z Sep 01 '18 at 07:43
-
as we are reading all rows, but i want to read first row of file as header(as like in data table) to check all the columns are present or not. – Kaushik Nandwana Apr 02 '19 at 06:50