I am using ExcelDataReader to read the first sheet of the an Excel file. However, ExcelDataReader is telling me that multiple sheets exists in an Excel file even though I only see one sheet when I open the excel file in Excel.
This is the first time I have encountered this. ExcelDataReader always showed the exact number of sheets there were present in an excel file. No more, no less
This is the code I'm using to import and read the excel file
public static bool CheckForHeader(string filePath)
{
DataSet ds;
try
{
var extension = Path.GetExtension(filePath).ToLower();
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
IExcelDataReader reader = null;
if (extension == ".xls")
{
reader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else if (extension == ".xlsx")
{
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
else if (extension == ".csv")
{
reader = ExcelReaderFactory.CreateCsvReader(stream);
}
if (reader == null)
throw new Exception();
// reader.IsFirstRowAsColumnNames = firstRowNamesCheckBox.Checked;
using (reader)
{
ds = reader.AsDataSet(new ExcelDataSetConfiguration()
{
UseColumnDataType = false,
ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
{
UseHeaderRow = false
}
});
}
}
}
catch
{ throw new Exception("Error while loading the Excel Spreadsheet. Please make sure that the file is not being used by another program."); }
System.Data.DataTable workSheet = ds.Tables[0];
string FRowFCol;
FRowFCol = workSheet.Rows[0].ItemArray[0].ToString();
if (FRowFCol.Contains('\\'))
return false;
return true;
}
The ds
should only have 1 table inside it, as the excel file only contains 1 sheet. But instead, it is showing that it contains 5 tables.