【问题标题】:OleDbConnection locks Excel xls file if the file is corrupted如果文件损坏,OleDbConnection 会锁定 Excel xls 文件
【发布时间】:2015-03-26 17:39:26
【问题描述】:

我有一个遗留代码将 Excel (*.xls) 导入我们的数据库,然后在处理后将文件移动到特定目录。

代码工作正常,除了在一种情况下,当文件损坏时(即使是 MS Excel 也无法打开它)!在这种情况下会发生System.AccessViolationException 在打开连接时抛出!

代码如下所示:

        string connectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""", filePath);
        OleDbConnection connection = new OleDbConnection(connectionString);
        try
        {
            connection.ConnectionString = connectionString;
            connection.Open(); //<<<--- exception throws here
            //file processing
        }
        catch (Exception e)
        {
            //exception handling
        }
        finally
        {
            connection.Close();
            connection.Dispose();
            connection = null;
            GC.Collect();
        }

这里是异常详情...

System.AccessViolationException was caught
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=System.Data
  StackTrace:
       at System.Data.Common.UnsafeNativeMethods.IDBInitializeInitialize.Invoke(IntPtr pThis)
       at System.Data.OleDb.DataSourceWrapper.InitializeAndCreateSession(OleDbConnectionString constr, SessionWrapper& sessionWrapper)
       at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
       at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
       at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
       at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
       at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
       at System.Data.OleDb.OleDbConnection.Open()

如您所见,我正在捕获此异常并对其进行处理,然后当代码尝试将文件移动到另一个目录时,出现以下异常:

System.IO.IOException occurred
  Message=The process cannot access the file because it is being used by another process.
  Source=mscorlib
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.__Error.WinIOError()
       at System.IO.File.Move(String sourceFileName, String destFileName)

我尝试使用另一个库,例如LinqToExcel,但发现它在内部使用与我相同的实现,那么它有同样的问题!

我也尝试在连接关闭后运行垃圾收集器(如您在上面的代码中看到的),但遇到了同样的问题!

有什么想法吗?

【问题讨论】:

    标签: c# .net excel oledbconnection filehandle


    【解决方案1】:

    我试图解决问题中的主要解决方案,但没有结果:(

    我什至检查了 .NET Framework 代码,可以在代码中的某处看到文件句柄,但遗憾的是未能调试代码:(

    我尝试反编译 .NET Framework 代码,但也失败了 :(

    最后。最后我应该使用另一种解决方案,因为取决于生产机器中 MS Office 的存在不是一个选项,我去了ExcelDataReader,将 *.xls 文件作为二进制流读取的开源库,这里是最终代码的样子:

    using (FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
    {
        using (IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream, true))
        {
            excelReader.IsFirstRowAsColumnNames = true;
            var excelFileDataSet = excelReader.AsDataSet();
            var sheetDataTable = excelFileDataSet.Tables["sheetName"];
            //other file processing code...
        }
    }    
    

    这个解决方案对我有用!

    【讨论】:

      【解决方案2】:

      我现在有同样的问题,我唯一的解决方案是使用 Microsoft.Office.Interop.Excel 读取 excel 文件并设置 MsoFileValidationMode = msoFileValidationSkip;

      Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkbook;

              System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
              System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
      
              **xlApp.FileValidation = MsoFileValidationMode.msoFileValidationSkip;** 
      
              xlWorkbook = xlApp.Workbooks.Open(@"C:\my file.xls");
              Excel.Sheets xlWorksheet = xlWorkbook.Worksheets;
      
              Excel.Worksheet worksheet = (Excel.Worksheet)xlWorksheet.get_Item(3);
              for (int i = 1; i <= 10; i++)
              {
      
                  Excel.Range range = worksheet.get_Range("A" + i.ToString(), "B" + i.ToString()); ; //UsedRange;
                  System.Array myvalues = (System.Array)range.Cells.Value2;
      
      
                  string[] strArray = ConvertToStringArray(myvalues);
      
                  foreach (string item in strArray)
                  {   
                      MessageBox.Show(item);
                  }
      
              }
      

      ...效果很好

      【讨论】:

      • 看来应该在生产机器上安装Excel!另外,此解决方案是否对某些 Office 版本有严格限制?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多