Reading only the first row of a file
Sometimes you need to process a big number of files depending on their data in the first rows. If the data isn't what you want, you skip the file.
For those cases, it might make sense not to load the full file in memory, just to read the cell A1 and then close it. And FlexCel has the right tool for the job: Virtual Mode
Virtual mode is described in more detail in the Performance Guide, but in this tip we will just give you the code to read a file and stop reading after the first cell.
You can also find this example in the ExcelFile.VirtualMode documentation.
static void ReadFirstRowOfAllSheets(string fileName)
{
XlsFile xls = new XlsFile(); //Create the object but don't open the file yet.
xls.VirtualMode = true; //Set the mode to Virtual.
xls.VirtualCellRead += (sender, e) =>
{
if (e.Cell.Row > 1)
{
if (e.Cell.Sheet < e.SheetNames.Length) e.NextSheet = e.SheetNames[e.Cell.Sheet]; //Stop reading this sheet, move to the next.
else e.NextSheet = null; //Stop reading the file.
}
else
{
Console.WriteLine("Cell: " + new TCellAddress(e.SheetNames[e.Cell.Sheet - 1], e.Cell.Row, e.Cell.Col, false, false).CellRef);
Console.Write("Value: ");
Console.WriteLine(e.Cell.Value);
}
};
xls.Open(fileName);
}