English 中文(简体)
为什么此代码导致Excel无法正常关闭?
原标题:Why does this code cause Excel to not close properly?
  • 时间:2011-02-11 01:20:16
  •  标签:
  • c#
  • excel

为什么这行代码导致Excel无法退出?

Excel.Range range = (Excel.Range)ws.Cells[1,1];

如果是因为强制转换,那么这段代码不会导致同样的问题吗?

Excel.Worksheet ws = (Excel.Worksheet)wb.ActiveSheet;

我试过了。但这是有效的。Excel将关闭。

如果我使用这个代码。Excel关闭。

Excel.Range range = ws.get_Range("A1","A1");

那有什么区别呢?是的,我知道有一百万个“如何正确关闭Excel”线程。但由于这是一个问题而不是答案,我决定问一个新的问题,而不是问别人的问题。

这是我的密码。但当然,两者之间还有其他代码。我只是把所有的东西都注释掉,慢慢地试着找出哪些行导致Excel无法关闭。我意识到,即使不使用垃圾收集器,Excel仍然会关闭。我不想用大锤来关闭Excel。

谢谢

Excel.Application objExcel = new Excel.Application();
Excel.Workbooks wbs = objExcel.Workbooks;
Excel.Workbook wb = wbs.Open(saveFileDialog1.FileName, Type.Missing,  Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Excel.Worksheet ws = (Excel.Worksheet)wb.ActiveSheet;
//Excel.Range range = (Excel.Range)ws.Cells[1,1];
Excel.Range range = ws.get_Range("A1","A1");

FinalReleaseAnyComObject(range);
FinalReleaseAnyComObject(ws);
wb.Close(Type.Missing, Type.Missing, Type.Missing);
FinalReleaseAnyComObject(wb);
FinalReleaseAnyComObject(wbs);
objExcel.Quit();
FinalReleaseAnyComObject(objExcel);

目前我已经尝试了objExcel、wbs、wb和ws。这4个对象没有问题。

private static void FinalReleaseAnyComObject(object o)
{
    Marshal.FinalReleaseComObject(o);
    o = null;
}

我意识到你也不能重用这个变量。

Excel.Range range = ws.get_Range("A1","G1");
range = ws.get_Range("A1", "A1");

这也会导致Excel无法正常关闭。相反,使用这个。

Excel.Range range = ws.get_Range("A1","G1");
FinalReleaseAnyComObject(range);
range = ws.get_Range("A1", "A1");
最佳回答

有一个隐藏的Range接口指针,您看不到,Cells属性将返回该指针。然后将索引器表达式应用于该指针,取消引用该Range的默认Item属性。获取另一个范围。

这就是为什么尝试自己管理COM接口指针是一个非常糟糕的主意。相信垃圾收集器总是正确处理。GC.Collect()和GC.WaitForPendingFinalizers(),如果您真的非常想让它按需退出。请务必阅读这个答案来理解为什么在调试程序时这并不总是按预期工作。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签