0
我开发了从excel读取并将其转换为文本文件的winform应用程序。 我使用Microsoft.Office.Interop.Excel库为了使用Excel。禁用Winform应用程序缓存
这里是我的代码:
private Excel.Application excel = null;
private Excel.Sheets sheets = null;
private Excel.Workbook excelWorkbook = null;
private Excel.Workbooks excelWorkbooks = null;
private Excel._Worksheet worksheet = null;
private Excel.Range usedRange = null;
public ExcelFacade() { }
public ExcelFacade(string fileName)
{
excel = new Excel.Application();
excelWorkbooks = excel.Workbooks;
excelWorkbook = excelWorkbooks.Open(fileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
sheets = excelWorkbook.Sheets;
}
后,我完成的工作与Excel我下面的方法(从here):
public void Dispose()
{
foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in sheets)
{
while (Marshal.ReleaseComObject(sheet) != 0) { }
}
excelWorkbook.Close();
excelWorkbooks.Close();
excel.Quit();
var chromeDriverProcesses = Process.GetProcesses().
Where(pr => pr.ProcessName.ToLower().Contains("excel"));
foreach (var process in chromeDriverProcesses)
{
process.Kill();
}
//while (Marshal.ReleaseComObject(usedRange) != 0) { }
//while (Marshal.ReleaseComObject(worksheet) != 0) { }
while (Marshal.ReleaseComObject(sheets) != 0) { }
while (Marshal.ReleaseComObject(excelWorkbook) != 0) { }
while (Marshal.ReleaseComObject(excelWorkbooks) != 0) { }
//while (Marshal.ReleaseComObject(excel.Application) != 0)
//{ }
while (Marshal.ReleaseComObject(excel) != 0) { }
usedRange = null;
worksheet = null;
excelWorkbook = null;
excelWorkbooks = null;
sheets = null;
excel = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
但是,如果我改变脱颖而出,并重新运行我的应用程序中,更新的Excel不是由应用程序采取的,它看起来像从旧的Excel中读取。这种行为看起来像缓存,我不知道如何禁用它。
上述情况由于我改变了我的代码中的某些内容(如空白,并重建应用程序,它工作辉煌,采取正确的Excel文件。 有什么建议吗?
我还没有使用互操作多所有的问题,所以我不知道我可以直接回答你的问题 - 但它有可能为你使用一个库像ClosedXML呢?或者,如果这是XLS文件,你可以使用OLE吗?这些都可以完全避免你的问题。 – vbnet3d