gpt4 book ai didi

C# Excel 工作簿已关闭,但无法移动文件

转载 作者:行者123 更新时间:2023-11-30 20:29:18 24 4
gpt4 key购买 nike

我正在通过

打开 Excel 文件
app = new Microsoft.Office.Interop.Excel.Application();
wb = app.Workbooks.Open(fromFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

然后我正在操纵细胞,效果很好。我将文件保存到另一个文件夹,然后再打开它。

wb.SaveAs(toFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, false, Type.Missing, Type.Missing, true);

我关闭应用程序和工作簿:

wb.Close(true, Type.Missing, Type.Missing);
app.Quit();

当我现在想通过 File.Move() 移动原始文件时,出现错误:

The process cannot access the file because it is being used by another

process.

我错过了什么吗?

编辑:

非常感谢你们。一直是对象的缺失释放。

Marshal.FinalReleaseComObject(object)

编辑 2:

好吧,但现在我可以运行它一次,现在我总是得到错误:

This file is currently not available for use on this computer.

结合 COMException

The object invoked has disconnected from its clients.

最佳答案

你需要使用

Marshal.ReleaseComObject(Object_to_be_released)

用于互操作服务中使用的所有对象。

_xlApp.Quit();
Marshal.ReleaseComObject(_xlApp);

关于C# Excel 工作簿已关闭,但无法移动文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46303946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com