我使用以下代码通过ExcelDataReader读取xls文件的内容。
List<string> excelFiles = GetExcelFileNamesInDirectory(Application.persistentDataPath);
for (int i = 0; i < excelFiles.Count; i++)
{
using (var stream = File.Open(excelFiles[i], FileMode.Open, FileAccess.Read))
{
// Auto-detect format, supports:
// - Binary Excel files (2.0-2003 format; *.xls)
// - OpenXml Excel files (2007 format; *.xlsx)
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
// Choose one of either 1 or 2:
fileLog.text = reader.Name;
// 1. Use the reader methods
do
{
while (reader.Read())
{
// reader.GetDouble(0);
}
} while (reader.NextResult());
// 2. Use the AsDataSet extension method
var result = reader.AsDataSet();
// The result of each spreadsheet is in result.Tables
}
}
}
有一个Excel文件,因此代码通过了for循环。不幸的是,代码没有跨过using (var stream..
行,并且在使用ExcelReaderFactory创建阅读器之前就结束了。我缺少参考吗?