我正在使用ExcelDataReader读取Excel文件的第一张纸。但是,ExcelDataReader告诉我一个Excel文件中存在多个工作表,即使我在Excel中打开excel文件时只看到一个工作表。
这是我第一次遇到此问题。 ExcelDataReader始终显示excel文件中存在的确切图纸数。不多不少
这是我用来导入和读取Excel文件的代码
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;
}
ds
内仅应包含1个表,因为excel文件仅包含1张工作表。而是显示它包含5个表。