gpt4 book ai didi

c# - c#关闭excel的问题

转载 作者:行者123 更新时间:2023-11-30 16:34:19 26 4
gpt4 key购买 nike

我已经用这段代码进行了单元测试:

  Excel.Application objExcel = new Excel.Application();
Excel.Workbook objWorkbook = (Excel.Workbook)(objExcel.Workbooks._Open(@"D:\Selenium\wszystkieSeba2.xls", true,
false, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value));


Excel.Worksheet ws = (Excel.Worksheet)objWorkbook.Sheets[1];
Excel.Range r = ws.get_Range("A1", "I2575");
DateTime dt = DateTime.Now;
Excel.Range cellData = null;
Excel.Range cellKwota = null;
string cellValueData = null;
string cellValueKwota = null;
double dataTransakcji = 0;
string dzien = null;
string miesiac = null;
int nrOperacji = 1;
int wierszPoczatkowy = 11;
int pozostało = 526;

cellData = r.Cells[wierszPoczatkowy, 1] as Excel.Range;
cellKwota = r.Cells[wierszPoczatkowy, 6] as Excel.Range;


if ((cellData != null) && (cellKwota != null))
{

object valData = cellData.Value2;
object valKwota = cellKwota.Value2;


if ((valData != null) && (valKwota != null))
{
cellValueData = valData.ToString();
dataTransakcji = Convert.ToDouble(cellValueData);
Console.WriteLine("data transakcji to: " + dataTransakcji);
dt = DateTime.FromOADate((double)dataTransakcji);
dzien = dt.Day.ToString();
miesiac = dt.Month.ToString();


cellValueKwota = valKwota.ToString();


}
}

r.Cells[wierszPoczatkowy, 8] = "ok";
objWorkbook.Save();



objWorkbook.Close(true, @"C:\Documents and Settings\Administrator\Pulpit\Selenium\wszystkieSeba2.xls", true);
objExcel.Quit();

为什么在完成测试后我仍然有 excel in process(它没有关闭)

并且:有什么我可以改进以提高性能的吗??

Excel 2007 和 .net 3.5

最佳答案

我使用这样的代码段来强行关闭它:

    public void DisposeExcelInstance()
{
//oXL.DisplayAlerts = false;
//oWB.Close(null, null, null);
//oXL.Quit();


//oWB.Close(null, null, null);
//oXL.Quit();
///KNG - CLEANUP code
oXL.DisplayAlerts = false;
oWB.Close(null, null, null);
oXL.Workbooks.Close();
oXL.Quit();
if (oResizeRange != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oResizeRange);
if (oSheet != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
if (oWB != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
if (oXL != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
oSheet = null;
oWB = null;
oXL = null;
GC.Collect(); // force final cleanup!

}

关于c# - c#关闭excel的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2505949/

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