我正在使用 Codeplex Excel 数据阅读器来读取 excel。我面临的问题是它可以毫无困难地读取 Excel 97-2003 文档,但是当使用 ExcelReaderFactory.CreateOpenXmlReader(stream) 读取 Excel 207-2010 文档时,它输出的是一个空数据集。有没有人遇到过这个问题。有没有人对此有任何解决方案?
读取方法如下
private DataSet ReadExcel(string fileName, string extention)
{
DataSet dsData = null;
FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader = null;
try
{
if (extention.Equals("xls"))
{
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else
{
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
// excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
}
excelReader.IsFirstRowAsColumnNames = false;
dsData = excelReader.AsDataSet();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (excelReader != null)
{
excelReader.Close();
}
}
return dsData;
}