Hello Team,
Am new to spire.xls, is it possible to read all the column names (c#) of excel (i.e i need to read first row which contains column names alone) ?
public static void Test()
{
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"sample.xlsx");
Worksheet sheet = workbook.Worksheets[0];
int columnCount = sheet.Columns.Length;
List<string> ColumnNames = new List<string>();
for (int i = 1; i <= columnCount; i++)
{
string name = GetExcelLetter(i);
ColumnNames.Add(name);
}
}
private static string GetExcelLetter(int columnNum)
{
int num = columnNum;
int mod = 0;
string result = String.Empty;
while (num > 0)
{
mod = (num - 1) % 26;
result = (char)(65 + mod) + result;
num = (int)((num - mod) / 26);
}
return result;
}
Please refer to following code:
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"Sample.xlsx");
Worksheet sheet = workbook.Worksheets[0];
CellRange[] crs = sheet.Rows[0].Cells;
List<string> cells = new List<string>();
foreach (CellRange cr in crs)
{
cells.Add(cr.Value);
}