2012-01-03 18 views
1

Excelのワークシートを操作するために、Excelのインターフェイスへの参照を作成および解放しています。OleDbConnectionのOpenメソッドを使用すると、Excelが自動的に閉じない

この状況では、Excel自体が正しく閉じます。 OleDbDataAdapter接続を使用してデータを取得すると、Excelはまだメモリに残っています。

私はこのテーマのほとんどを読んでいます。

  1. 適切なリリースリファレンスを作成するためのサブルーチンを作成しました。
  2. 私が使用しています:
GC.Collect(); 
GC.WaitForPendingFinalizers(); 

私は他に何ができますか?

namespace ExcelTestCode 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
    Application excel = null; 
    Workbook workbook = null; 
    Worksheet workSheet = null; 
    object oMissing = Missing.Value; 

    excel = new Application { Visible = false }; 
    workbook = excel.Workbooks.Open(@"c:\temp.xls", 0, false, 5, "", "", 
       true, XlPlatform.xlWindows, "\t", false, false, 0, true, true, oMissing); 
    workSheet = (Worksheet)workbook.Sheets[1]; 

    try 
    { 
     string strError = ""; 
     System.Data.DataTable dtTable = null; 

     //If I remove the following line, everything is allright 
     dtTable = ImportDataTableFromExcelIMEX(@"c:\temp.xls", out strError); 
    } 
    finally 
    { 
     if (workSheet != null) 
     { 
     Marshal.ReleaseComObject(workSheet); 
     workSheet = null; 
     } 
     if (workbook != null) 
     { 
     workbook.Close(false, oMissing, oMissing); 
     Marshal.ReleaseComObject(workbook); 
     workbook = null; 
     } 

     if (excel != null) 
     { 
     excel.Quit(); 
     Marshal.ReleaseComObject(excel); 
     excel = null; 
     } 
     GC.Collect(); 
     GC.WaitForPendingFinalizers(); 
     GC.Collect(); 
    } 
    } 

    public static System.Data.DataTable ImportDataTableFromExcelIMEX(string filename, out string error) 
    { 
    string connstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + @";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""; 
    OleDbConnection upocn = new OleDbConnection(connstring); 
    try 
    { 
     upocn.Open(); 

     System.Data.DataTable dt = null; 
     dt = upocn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 

     using (OleDbDataAdapter upoda = new OleDbDataAdapter("select * from [" + dt.Rows[0]["TABLE_NAME"].ToString() + "]", upocn)) 
     { 
     DataSet upods = new DataSet(); 
     error = string.Empty; 

     upoda.Fill(upods); 

     if (!string.IsNullOrEmpty(error)) 
      return null; 

     return upods.Tables[0]; 
     } 
    } 
    catch (Exception ex) 
    { 
     error = ex.Message; 
    } 
    finally 
    { 
     upocn.Close(); 
     upocn = null; 
    } 
    return null; 
    } 
} 
} 
+0

あなたは接続とコマンドを処理していますか?あなたのコードを共有できますか? –

+0

問題の小さな例を追加しました。 – John

答えて

2

(OleDbConnectionオブジェクトupocn =新しいOleDbConnectionオブジェクト(れたconnectionString))を使用してみてください、またはお電話upocn.Dispose()

これは曖昧な問題のようです。..ここ

コードです

MSDN OleDbConnection.Dispose:System.ComponentModel.Componentで使用されるすべてのリソースを解放します。 OleDbConnection.close:私は上記のコードのようにconnection.closeない場合、私はこの問題を生成することができますを更新されたデータソース

への接続を閉じますが、私はそれが正常に動作処分を呼び出すとき、私はいずれかを参照していけませんExcelのインスタンス。以下は私のために働くコードです。テスト前に実行中のインスタンスをタスクマネージャから削除することを確認してください。

class Program 
{ 
    static void Main(string[] args) 
    { 
     Application excel = null; 
     Workbook workbook = null; 
     Worksheet workSheet = null; 
     object oMissing = Missing.Value; 

     excel = new Application { Visible = false }; 
     workbook = excel.Workbooks.Open(@"c:\Book1.xls", 0, false, 5, "", "", 
        true, XlPlatform.xlWindows, "\t", false, false, 0, true, true, oMissing); 
     workSheet = (Worksheet)workbook.Sheets[1]; 

     try 
     { 
      string strError = ""; 
      System.Data.DataTable dtTable = null; 

      //If I remove the following line, everything is allright 
      dtTable = ImportDataTableFromExcelIMEX(@"c:\Book1.xls", out strError); 
     } 
     finally 
     { 
      if (workSheet != null) 
      { 
       Marshal.ReleaseComObject(workSheet); 
       workSheet = null; 
      } 
      if (workbook != null) 
      { 
       workbook.Close(false, oMissing, oMissing); 
       Marshal.ReleaseComObject(workbook); 
       workbook = null; 
      } 

      if (excel != null) 
      { 
       excel.Quit();     
       Marshal.ReleaseComObject(excel); 
       excel = null; 
      } 
      GC.Collect(); 
      GC.WaitForPendingFinalizers(); 
      GC.Collect(); 
     } 
    } 

    public static System.Data.DataTable ImportDataTableFromExcelIMEX(string filename, out string error) 
    { 
     string connstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + @";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""; 

     try 
     { 
      using (OleDbConnection upocn = new OleDbConnection(connstring)) 
      { 
       upocn.Open(); 
       System.Data.DataTable dt = null; 
       dt = upocn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 

       using (OleDbDataAdapter upoda = new OleDbDataAdapter("select * from [" + dt.Rows[0]["TABLE_NAME"].ToString() + "]", upocn)) 
       { 
        DataSet upods = new DataSet(); 
        error = string.Empty; 

        upoda.Fill(upods); 

        if (!string.IsNullOrEmpty(error)) 
         return null; 

        return upods.Tables[0]; 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      error = ex.Message; 
     } 

     return null; 
    } 
} 
+0

私はこれを試しました。これは例ではありませんが、どちらもうまくいきません。 – John

+0

既存のExcelインスタンスを削除して試してみてください。 –

+0

ソースコードを100%確実にコピーしました。まだメモリ内のExcel。私は2003 v。which verに秀でています。ありますか?私は、comの相互運用性を使用する前にデータを取得することで問題を解決しました。しかし、私は問題がどこにあるか知りたいと思います。 – John

1

私は同じ問題がありました。基本的に私は、OleDbConnectionオブジェクトxlConが閉じるように言った後、新規のOleDbCommand xlCmdと

finally 
{ 
    if (xlCon != null) 
     xlCon.Dispose(); 
} 

を初期化する前に

finally 
{ 
    if (xlCmd != null) 
    { 
     xlCmd.Dispose(); 
     xlCmd = null; 
    } 
} 

を入れていました。あなたはまた、以下のように使用してブロックを使用してExcelの接続を初期化していることを確認してください:あなたがあなたのファイルを開きたい場合は、アプリケーションが実行されているし、あなたがあなたの輸出で行われている間

using (OleDbConnection xlCon = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; " + 
    "Data Source = " + xlFile + ";Mode=ReadWrite;" + 
    "Extended Properties='Excel 12.0;HDR=YES;'")) //Automatically creates a new excel file (Do not include IMEX=1) 
{ 

これらの手動クリーンアップが必要とされています。

+0

あなたのコードを貼り付けることができます、私はここで私の質問をExcelの閉じるの同じ問題を抱えています:http://stackoverflow.com/questions/12735284/excel-file-it-is-already-opened-exclusively-by-別のユーザー –

関連する問題