I want to read excel file using gembox like first row as header to check weather required column are present or not.
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
.