ExcelDataReader关闭异常“目录不为空。”

时间:2016-06-23 12:10:05

标签: c# exceldatareader

只有在我将代码部署到服务器上时,才会从代码中获得The directory is not empty异常。

通过异常调试发现excelReader.Close();

存在一些小故障

代码如下

 IExcelDataReader excelReader = null;    
 DeleteFile(path);
 postedFile.SaveAs(path);

 FileStream stream = File.Open(path, FileMode.Open, FileAccess.ReadWrite);
 if (Extension.ToLower() == ".xlsx")
 {
      excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
 }
 else if (Extension.ToLower() == ".xls")
 {
      excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
 }
 excelReader.IsFirstRowAsColumnNames = true;

 DataSet result = excelReader.AsDataSet();

 if (result.Tables.Count > 0)
 {
      dt = result.Tables[0];
 }
 stream.Close();
 stream.Dispose();
 DeleteFile(path);
 if (excelReader != null)
 {
      File.AppendAllText(
         @"D:\Websites\registry.aan.com\OldAuditLogs\Error.txt",
        "excelReader if");

      excelReader.Close();
      excelReader.Dispose();
 }
 else {
     File.AppendAllText(
        @"D:\Websites\registry.aan.com\OldAuditLogs\Error.txt",
        "excelReader else");
 }

 File.AppendAllText(
   @"D:\Websites\registry.aan.com\OldAuditLogs\Error.txt",
   "excelReader out");

 result.Dispose();

1 个答案:

答案 0 :(得分:0)

将ExcelDataReader升级到版本3.4.2