Excel实例赢了;如果将单元格内容复制到struct,则会死亡 [英] Excel Instance won;t die if coping cell content to struct

查看:53
本文介绍了Excel实例赢了;如果将单元格内容复制到struct,则会死亡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


出于某种奇怪的原因,excel实例赢了;如果我从4行设置值中删除评论

到struct 。


这里是一个剪辑代码


public System.Collections.Generic.List< frmMain.sDBTest>

LoadTestSet(string TestSetFile,

System.Collections.Generic.List< frmMain.sDBTestDBv iewList)

{

int rowIndex = 2;

Excel.ApplicationClass app;

Excel._Workbook oWB;

Excel._Worksheet oSheet;


if (TestSetFile.ToString()=="")

返回null;


Application.DoEvents();


if(!File.Exists(TestSetFile))

{

MessageBox.Show(" File" + TestSetFile +" doesn''t

存在...","找不到文件!");

返回null;

}


app = new Excel.ApplicationClass();

if(app == null)

{
MessageBox.Show(Excel无法启动。,

"错误");

返回null;

}


oWB = app.Workbooks.Open(TestSetFile,0,true,5,"","",

true,

Excel.XlPlatform.xlWindows," \t",false,false,0,

true,1,0);


oSheet =(Excel.Worksheet)oWB.ActiveSheet;


while(((Excel.Range)oSheet.Cells [rowIndex,1])。Value2 !=

null)

rowIndex ++;


progressBar1.Maximum = rowIndex-2;


rowIndex = 2;


尝试

{

while(((Excel.Range)oSheet.Cells [ rowIndex,1])。Value2

!= null)

{

progressBar1.Value = rowIndex-2;

frmMain.sDBTest frmmainsDBTestobj = new

frmMain.sDBTest();

frmmainsDBTestobj.TestNumber =

Int32.Parse(((Excel。范围)oSheet.Cells [rowIndex,

1])。Value2.ToStri ng());

frmmainsDBTestobj.Test =

((Excel.Range)oSheet.Cells [rowIndex,2])。Value2.ToString();

// 1.1 frmmainsDBTestobj.Teststatus =

((Excel.Range)oSheet.Cells [rowIndex,3])。Value2.ToString();

// 1.2 frmmainsDBTestobj.TestComment =

((Excel.Range)oSheet.Cells [rowIndex,4])。Value2.ToString();

//frmmainsDBTestobj.TST =

((Excel.Range)oSheet.Cells [rowIndex,5])。Value2.ToString();

// 1.3 frmmainsDBTestobj.iscomment =

bool.Parse(((Excel.Range)oSheet.Cells [rowIndex,6])。Value2.ToString());

DBviewList.Add(frmmainsDBTestobj);

rowIndex ++;

}

}

catch(例外情况)

{


MessageBox.Show(ex.Message.ToString());

//app.Workbooks.Close();

// app .Quit();

}


app.Workbooks.Close();

app.Application.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComO bject(app);


System.Runtime.InteropServices.Marshal.ReleaseComO bject(oWB);

app.Application.Quit();


System.Runtime.InteropServices.Marshal.ReleaseComO bject(oSheet);

app.Workbooks .Close();

app = null;

oWB = null;

oSheet = null;

系统.GC.Collect();

System.GC.WaitForPendingFinalizers();

返回DBviewList;


}

在这种情况下,excel实例正在死亡。如果我删除行1.1,1.2

和1.3,那么excel实例不会死。


sDBTest是一个公共类,其中所有成员都是字符串除了

iscomment(bool)。


感谢您的帮助。

解决方案

Alexia,


您将在任何地方打开引用。最值得注意的是,Cells属性返回的对象

,以及当您访问Cells对象上的Item属性时返回的每个Range对象。


您需要仔细计算这些并在这些上发布所有参考资料

以确保实例死亡。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


< al ******** @ gmail .comwrote in message

news:11 ********************** @ j72g2000cwa.googlegr oups.com ...


大家好,


出于某种奇怪的原因,excel实例赢了;如果我删除评论就会死掉

从4行设置值到struct。


这里是一个剪辑代码


public System.Collections.Generic.List< frmMain。 sDBTest>

LoadTestSet(字符串TestSetF ile,

System.Collections.Generic.List< frmMain.sDBTestDBv iewList)

{

int rowIndex = 2;

Excel.ApplicationClass app;

Excel._Workbook oWB;

Excel._Worksheet oSheet;


if(TestSetFile。 ToString()=="")

返回null;


Application.DoEvents();


if(!File.Exists(TestSetFile))

{

MessageBox.Show(" File" + TestSetFile +"不存在
存在...",找不到文件!;

返回null;

}


app = new Excel.ApplicationClass();

if(app == null)

{

MessageBox.Show(Excel无法启动。,

"错误");

返回null;

}


oWB = app.Workbooks.Open(TestSetFile,0,true,5,"","",

true,

Excel.XlPlatform.xlWindows," \t",false,false,0,

true,1,0);


oSheet =(Excel.Worksheet)oWB.ActiveSheet;


while(((Excel.Range)oSheet.Cells [rowIndex,1])。Value2!=

null)

rowIndex ++;


progressBar1.Maximum = rowIndex-2;


rowIndex = 2;

尝试

{

while(((Excel.Range)oSheet.Cells [rowIndex,1])。Value2

!= null)

{

progressBar1.Value = rowIndex-2;

frmMain.sDBTest frmmainsDBTestobj = new

frmMain.sDBTest();

frmmainsDBTestobj.TestNumber =

Int32.Parse(((Excel.Range)oSheet.Cells [rowIndex,

1])。Value2.ToString());

frmmainsDBTestobj.Test =

((Excel.Range)oSheet.Cells [rowIndex,2])。 Value2.ToString();

// 1.1 frmmainsDBTestobj.Teststatus =

((Excel.Range)oSheet.Cells [rowIndex,3])。Value2.ToString() ;

// 1.2 frmmainsDBTestobj.TestComment =

((Excel.Range)oSheet.Cells [rowIndex,4])。Value2.ToString();

//frmmainsDBTestobj.TST =

((Excel.Range)oSheet.Cells [rowIndex ,5。)。Value2.ToString();

// 1.3 frmmainsDBTestobj.iscomment =

bool.Parse(((Excel.Range)oSheet.Cells [rowIndex, 6])。Value2.ToString());

DBviewList.Add(frmmainsDBTestobj);

rowIndex ++;

}

}

catch(例外情况)

{


MessageBox.Show(ex.Message.ToString()) ;

//app.Workbooks.Close();

//app.Quit();

}


app.Workbooks.Close();

app.Application.Quit();


System.Runtime.InteropServices.Marshal。 ReleaseComO bject(app);


System.Runtime.InteropServices.Marshal.ReleaseComO bject(oWB);

app.Application.Quit();


System.Runtime.InteropServices.Marshal.ReleaseComO bject(oSheet);

ap p.Workbooks.Close();

app = null;

oWB = null;

oSheet = null;

System.GC.Collect();

System.GC.WaitForPendingFinalizers();


返回DBviewList;


}

在这种情况下,excel实例正在死亡。如果我删除行1.1,1.2

和1.3,那么excel实例不会死。


sDBTest是一个公共类,其中所有成员都是字符串除了

iscomment(bool)。


感谢您的帮助。




Nicholas Paldino [.NET / C#MVP]写道:


Alexia,


您将在任何地方打开引用。最值得注意的是,Cells属性返回的对象

,以及当您访问Cells对象上的Item属性时返回的每个Range对象。


您需要仔细计算这些并在这些上发布所有参考资料

以确保实例死亡。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


< al ****** **@gmail.com在留言中写道

新闻:11 ********************** @ j72g2000cwa.googlegr psps.com .. 。


大家好,


有些奇怪的原因,excel实例赢了;如果我删除评论就会死/>
从4行设置值到struct。


这里是一个剪辑代码


public System.Collections.Generic。列表与LT; frmMain.sDBTest>

LoadTestSet(字符串TestSetFile,

System.Collections.Generic.List< frmMain.sDBTestDBv iewList)

{

int rowIndex = 2;

Excel.ApplicationClass app;

Excel._Workbook oWB;

Excel._Worksheet oSheet;


if(TestSetFile.ToString()=="")

返回null;


Application.DoEvents();


if(!File.Exists(TestSetFile))

{

MessageBox.Show(" File" + TestSetFile +"不存在
存在...",找不到文件!;

返回null;

}


app = new Excel.ApplicationClass();

if(app == null)

{

MessageBox.Show(Excel无法启动。,

"错误");

返回null;

}


oWB = app.Workbooks.Open(TestSetFile,0,true,5,"","",

true,

Excel.XlPlatform.xlWindows," \t",false,false,0,

true,1,0);


oSheet =(Excel.Worksheet)oWB.ActiveSheet;


while(((Excel.Range)oSheet.Cells [rowIndex,1])。Value2!=

null)

rowIndex ++;


progressBar1.Maximum = rowIndex-2;


rowIndex = 2;


尝试

{

while(((Excel.Range)oSheet.Cells [rowIndex,1] ).Value2

!= null)

{

progressBar1.Value = rowIndex-2;

frmMain。 sDBTest frmmainsDBTestobj = new

frmMain.sDBTest();

frmmainsDBTestobj.TestNumber =

Int32.Parse(((Excel.Range)oSheet。单元格[rowIndex,

1])。Value2.ToString());

frmmainsDBTestobj.Test =

((Excel.Range)oSheet .Cells [rowIndex,2])。Value2.ToString();

// 1.1 frmmainsDBTestobj.Teststatus =

((Excel.Range)oSheet.Cells [rowIndex, 3])。Value2.ToString();

// 1.2 frmmainsDBTestobj.TestComment =

((Excel.Range)oSheet.Cells [rowIndex,4])。Value2 .ToString();

//frmmainsDBTestobj.TST =
((Excel.Range)oSheet.Cells [rowIndex,5])。Value2.ToString();

// 1.3 frmmainsDBTestobj.iscomment =

bool.Parse(((Excel.Range)oSheet.Cells [rowIndex,6])。Value2.ToString());

DBviewList.Add(frmmainsDBTestobj);

rowIndex ++;

}

}

catch(例外情况)

{


MessageBox.Show(ex.Message.ToString());

//app.Workbooks.Close();

//app.Quit ();

}


app.Workbooks.Close();

app.Application.Quit();


System.Runtime.InteropServices.Marshal.ReleaseComO bject(app);


System.Runtime.InteropServices.Marshal.ReleaseComO bject(oWB);

app.Application.Quit();


System.Runtime.Interop Services.Marshal.ReleaseComO bject(oSheet);

app.Workbooks.Close();

app = null;

oWB = null;

oSheet = null;

System.GC.Collect();

System.GC.WaitForPendingFinalizers();

返回DBviewList;


}

在这种情况下,excel实例正在死亡。如果我删除行1.1,1.2

和1.3,那么excel实例不会死。


sDBTest是一个公共类,其中所有成员都是字符串除了

iscomment(bool)。


感谢您的帮助。



HI Nicholas并感谢您的回复,


我不知道我在哪里留下参考文献。如你所见,我关闭所有

引用。另外,在这种格式中,excel实例已经死了。但是,当我从$ 1.1,b和1.3行中删除commens时,excel参考

不会被杀死。




Nicholas Paldino [.NET / C#MVP]写道:


Alexia,


您将在任何地方打开引用。最值得注意的是,Cells属性返回的对象

,以及当您访问Cells对象上的Item属性时返回的每个Range对象。


您需要仔细计算这些并在这些上发布所有参考资料

以确保实例死亡。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


< al ****** **@gmail.com在留言中写道

新闻:11 ********************** @ j72g2000cwa.googlegr psps.com .. 。


大家好,


有些奇怪的原因,excel实例赢了;如果我删除评论就会死/>
从4行设置值到struct。


这里是一个剪辑代码


public System.Collections.Generic。列表与LT; frmMain.sDBTest>

LoadTestSet(字符串TestSetFile,

System.Collections.Generic.List< frmMain.sDBTestDBv iewList)

{

int rowIndex = 2;

Excel.ApplicationClass app;

Excel._Workbook oWB;

Excel._Worksheet oSheet;


if(TestSetFile.ToString()=="")

返回null;


Application.DoEvents();


if(!File.Exists(TestSetFile))

{

MessageBox.Show(" File" + TestSetFile +"不存在
存在...",找不到文件!;

返回null;

}


app = new Excel.ApplicationClass();

if(app == null)

{

MessageBox.Show(Excel无法启动。,

"错误");

返回null;

}


oWB = app.Workbooks.Open(TestSetFile,0,true,5,"","",

true,

Excel.XlPlatform.xlWindows," \t",false,false,0,

true,1,0);


oSheet =(Excel.Worksheet)oWB.ActiveSheet;


while(((Excel.Range)oSheet.Cells [rowIndex,1])。Value2!=

null)

rowIndex ++;


progressBar1.Maximum = rowIndex-2;


rowIndex = 2;


尝试

{

while(((Excel.Range)oSheet.Cells [rowIndex,1] ).Value2

!= null)

{

progressBar1.Value = rowIndex-2;

frmMain。 sDBTest frmmainsDBTestobj = new

frmMain.sDBTest();

frmmainsDBTestobj.TestNumber =

Int32.Parse(((Excel.Range)oSheet。单元格[rowIndex,

1])。Value2.ToString());

frmmainsDBTestobj.Test =

((Excel.Range)oSheet .Cells [rowIndex,2])。Value2.ToString();

// 1.1 frmmainsDBTestobj.Teststatus =

((Excel.Range)oSheet.Cells [rowIndex, 3])。Value2.ToString();

// 1.2 frmmainsDBTestobj.TestComment =

((Excel.Range)oSheet.Cells [rowIndex,4])。Value2 .ToString();

//frmmainsDBTestobj.TST =
((Excel.Range)oSheet.Cells [rowIndex,5])。Value2.ToString();

// 1.3 frmmainsDBTestobj.iscomment =

bool.Parse(((Excel.Range)oSheet.Cells [rowIndex,6])。Value2.ToString());

DBviewList.Add(frmmainsDBTestobj);

rowIndex ++;

}

}

catch(例外情况)

{


MessageBox.Show(ex.Message.ToString());

//app.Workbooks.Close();

//app.Quit ();

}


app.Workbooks.Close();

app.Application.Quit();


System.Runtime.InteropServices.Marshal.ReleaseComO bject(app);


System.Runtime.InteropServices.Marshal.ReleaseComO bject(oWB);

app.Application.Quit();


System.Runtime.Interop Services.Marshal.ReleaseComO bject(oSheet);

app.Workbooks.Close();

app = null;

oWB = null;

oSheet = null;

System.GC.Collect();

System.GC.WaitForPendingFinalizers();

返回DBviewList;


}

在这种情况下,excel实例正在死亡。如果我删除行1.1,1.2

和1.3,那么excel实例不会死。


sDBTest是一个公共类,其中所有成员都是字符串除了

iscomment(bool)。


感谢您的帮助。



尼古拉斯我没有看到她忘记释放任何物体的地方。至于

级别,在她打电话给GC后它正在被摧毁。


Hi all,

In some weird reason, excel instance won;t die if i remove the comment
from 4 lines of setting values into struct.

here is a snipcode

public System.Collections.Generic.List<frmMain.sDBTest>
LoadTestSet(string TestSetFile,
System.Collections.Generic.List<frmMain.sDBTestDBv iewList)
{
int rowIndex = 2;
Excel.ApplicationClass app;
Excel._Workbook oWB;
Excel._Worksheet oSheet;

if (TestSetFile.ToString() == "")
return null;

Application.DoEvents();

if (!File.Exists(TestSetFile))
{
MessageBox.Show("File " + TestSetFile + " Doesn''t
exist...", "File not found!");
return null;
}

app = new Excel.ApplicationClass();
if (app == null)
{
MessageBox.Show("Excel could not be started.",
"Error");
return null;
}

oWB = app.Workbooks.Open(TestSetFile, 0, true, 5, "", "",
true,
Excel.XlPlatform.xlWindows, "\t", false, false, 0,
true, 1, 0);

oSheet = (Excel.Worksheet)oWB.ActiveSheet;

while (((Excel.Range)oSheet.Cells[rowIndex, 1]).Value2 !=
null)
rowIndex++;

progressBar1.Maximum = rowIndex-2;

rowIndex = 2;

try
{
while (((Excel.Range)oSheet.Cells[rowIndex, 1]).Value2
!= null)
{
progressBar1.Value = rowIndex-2;
frmMain.sDBTest frmmainsDBTestobj = new
frmMain.sDBTest();
frmmainsDBTestobj.TestNumber =
Int32.Parse(((Excel.Range)oSheet.Cells[rowIndex,
1]).Value2.ToString());
frmmainsDBTestobj.Test =
((Excel.Range)oSheet.Cells[rowIndex, 2]).Value2.ToString();
// 1.1 frmmainsDBTestobj.Teststatus =
((Excel.Range)oSheet.Cells[rowIndex, 3]).Value2.ToString();
// 1.2 frmmainsDBTestobj.TestComment =
((Excel.Range)oSheet.Cells[rowIndex, 4]).Value2.ToString();
//frmmainsDBTestobj.TST =
((Excel.Range)oSheet.Cells[rowIndex, 5]).Value2.ToString();
// 1.3 frmmainsDBTestobj.iscomment =
bool.Parse(((Excel.Range)oSheet.Cells[rowIndex, 6]).Value2.ToString());
DBviewList.Add(frmmainsDBTestobj);
rowIndex++;
}
}
catch (Exception ex)
{

MessageBox.Show(ex.Message.ToString());
//app.Workbooks.Close();
//app.Quit();
}

app.Workbooks.Close();
app.Application.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComO bject(app);

System.Runtime.InteropServices.Marshal.ReleaseComO bject(oWB);
app.Application.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComO bject(oSheet);
app.Workbooks.Close();
app = null;
oWB = null;
oSheet = null;
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
return DBviewList;

}
In this case the excel instance is dying. if i remove lines 1.1,1.2
and1.3, then excel instance won''t die.

sDBTest is a public class where all members are string besides
iscomment(bool).

Thanks for the help.

解决方案

Alexia,

You are leaving references open everywhere. Most notably, the object
returned by the Cells property, as well as each Range object returned when
you access the Item property on the Cells object.

You need to keep careful count of these and release all the references
on these in order to make sure the instance dies.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<al********@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...

Hi all,

In some weird reason, excel instance won;t die if i remove the comment
from 4 lines of setting values into struct.

here is a snipcode

public System.Collections.Generic.List<frmMain.sDBTest>
LoadTestSet(string TestSetFile,
System.Collections.Generic.List<frmMain.sDBTestDBv iewList)
{
int rowIndex = 2;
Excel.ApplicationClass app;
Excel._Workbook oWB;
Excel._Worksheet oSheet;

if (TestSetFile.ToString() == "")
return null;

Application.DoEvents();

if (!File.Exists(TestSetFile))
{
MessageBox.Show("File " + TestSetFile + " Doesn''t
exist...", "File not found!");
return null;
}

app = new Excel.ApplicationClass();
if (app == null)
{
MessageBox.Show("Excel could not be started.",
"Error");
return null;
}

oWB = app.Workbooks.Open(TestSetFile, 0, true, 5, "", "",
true,
Excel.XlPlatform.xlWindows, "\t", false, false, 0,
true, 1, 0);

oSheet = (Excel.Worksheet)oWB.ActiveSheet;

while (((Excel.Range)oSheet.Cells[rowIndex, 1]).Value2 !=
null)
rowIndex++;

progressBar1.Maximum = rowIndex-2;

rowIndex = 2;

try
{
while (((Excel.Range)oSheet.Cells[rowIndex, 1]).Value2
!= null)
{
progressBar1.Value = rowIndex-2;
frmMain.sDBTest frmmainsDBTestobj = new
frmMain.sDBTest();
frmmainsDBTestobj.TestNumber =
Int32.Parse(((Excel.Range)oSheet.Cells[rowIndex,
1]).Value2.ToString());
frmmainsDBTestobj.Test =
((Excel.Range)oSheet.Cells[rowIndex, 2]).Value2.ToString();
// 1.1 frmmainsDBTestobj.Teststatus =
((Excel.Range)oSheet.Cells[rowIndex, 3]).Value2.ToString();
// 1.2 frmmainsDBTestobj.TestComment =
((Excel.Range)oSheet.Cells[rowIndex, 4]).Value2.ToString();
//frmmainsDBTestobj.TST =
((Excel.Range)oSheet.Cells[rowIndex, 5]).Value2.ToString();
// 1.3 frmmainsDBTestobj.iscomment =
bool.Parse(((Excel.Range)oSheet.Cells[rowIndex, 6]).Value2.ToString());
DBviewList.Add(frmmainsDBTestobj);
rowIndex++;
}
}
catch (Exception ex)
{

MessageBox.Show(ex.Message.ToString());
//app.Workbooks.Close();
//app.Quit();
}

app.Workbooks.Close();
app.Application.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComO bject(app);

System.Runtime.InteropServices.Marshal.ReleaseComO bject(oWB);
app.Application.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComO bject(oSheet);
app.Workbooks.Close();
app = null;
oWB = null;
oSheet = null;
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
return DBviewList;

}
In this case the excel instance is dying. if i remove lines 1.1,1.2
and1.3, then excel instance won''t die.

sDBTest is a public class where all members are string besides
iscomment(bool).

Thanks for the help.




Nicholas Paldino [.NET/C# MVP] wrote:

Alexia,

You are leaving references open everywhere. Most notably, the object
returned by the Cells property, as well as each Range object returned when
you access the Item property on the Cells object.

You need to keep careful count of these and release all the references
on these in order to make sure the instance dies.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<al********@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...

Hi all,

In some weird reason, excel instance won;t die if i remove the comment
from 4 lines of setting values into struct.

here is a snipcode

public System.Collections.Generic.List<frmMain.sDBTest>
LoadTestSet(string TestSetFile,
System.Collections.Generic.List<frmMain.sDBTestDBv iewList)
{
int rowIndex = 2;
Excel.ApplicationClass app;
Excel._Workbook oWB;
Excel._Worksheet oSheet;

if (TestSetFile.ToString() == "")
return null;

Application.DoEvents();

if (!File.Exists(TestSetFile))
{
MessageBox.Show("File " + TestSetFile + " Doesn''t
exist...", "File not found!");
return null;
}

app = new Excel.ApplicationClass();
if (app == null)
{
MessageBox.Show("Excel could not be started.",
"Error");
return null;
}

oWB = app.Workbooks.Open(TestSetFile, 0, true, 5, "", "",
true,
Excel.XlPlatform.xlWindows, "\t", false, false, 0,
true, 1, 0);

oSheet = (Excel.Worksheet)oWB.ActiveSheet;

while (((Excel.Range)oSheet.Cells[rowIndex, 1]).Value2 !=
null)
rowIndex++;

progressBar1.Maximum = rowIndex-2;

rowIndex = 2;

try
{
while (((Excel.Range)oSheet.Cells[rowIndex, 1]).Value2
!= null)
{
progressBar1.Value = rowIndex-2;
frmMain.sDBTest frmmainsDBTestobj = new
frmMain.sDBTest();
frmmainsDBTestobj.TestNumber =
Int32.Parse(((Excel.Range)oSheet.Cells[rowIndex,
1]).Value2.ToString());
frmmainsDBTestobj.Test =
((Excel.Range)oSheet.Cells[rowIndex, 2]).Value2.ToString();
// 1.1 frmmainsDBTestobj.Teststatus =
((Excel.Range)oSheet.Cells[rowIndex, 3]).Value2.ToString();
// 1.2 frmmainsDBTestobj.TestComment =
((Excel.Range)oSheet.Cells[rowIndex, 4]).Value2.ToString();
//frmmainsDBTestobj.TST =
((Excel.Range)oSheet.Cells[rowIndex, 5]).Value2.ToString();
// 1.3 frmmainsDBTestobj.iscomment =
bool.Parse(((Excel.Range)oSheet.Cells[rowIndex, 6]).Value2.ToString());
DBviewList.Add(frmmainsDBTestobj);
rowIndex++;
}
}
catch (Exception ex)
{

MessageBox.Show(ex.Message.ToString());
//app.Workbooks.Close();
//app.Quit();
}

app.Workbooks.Close();
app.Application.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComO bject(app);

System.Runtime.InteropServices.Marshal.ReleaseComO bject(oWB);
app.Application.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComO bject(oSheet);
app.Workbooks.Close();
app = null;
oWB = null;
oSheet = null;
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
return DBviewList;

}
In this case the excel instance is dying. if i remove lines 1.1,1.2
and1.3, then excel instance won''t die.

sDBTest is a public class where all members are string besides
iscomment(bool).

Thanks for the help.

HI Nicholas and thanks for the reply,

I have no idea where i leave references open. as you see i close all
references. also, in this format the excel instance is dead. but when i
remove the commetns from lines 1.1,1.2 and 1.3 the excel reference
won''t be killed.



Nicholas Paldino [.NET/C# MVP] wrote:

Alexia,

You are leaving references open everywhere. Most notably, the object
returned by the Cells property, as well as each Range object returned when
you access the Item property on the Cells object.

You need to keep careful count of these and release all the references
on these in order to make sure the instance dies.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<al********@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...

Hi all,

In some weird reason, excel instance won;t die if i remove the comment
from 4 lines of setting values into struct.

here is a snipcode

public System.Collections.Generic.List<frmMain.sDBTest>
LoadTestSet(string TestSetFile,
System.Collections.Generic.List<frmMain.sDBTestDBv iewList)
{
int rowIndex = 2;
Excel.ApplicationClass app;
Excel._Workbook oWB;
Excel._Worksheet oSheet;

if (TestSetFile.ToString() == "")
return null;

Application.DoEvents();

if (!File.Exists(TestSetFile))
{
MessageBox.Show("File " + TestSetFile + " Doesn''t
exist...", "File not found!");
return null;
}

app = new Excel.ApplicationClass();
if (app == null)
{
MessageBox.Show("Excel could not be started.",
"Error");
return null;
}

oWB = app.Workbooks.Open(TestSetFile, 0, true, 5, "", "",
true,
Excel.XlPlatform.xlWindows, "\t", false, false, 0,
true, 1, 0);

oSheet = (Excel.Worksheet)oWB.ActiveSheet;

while (((Excel.Range)oSheet.Cells[rowIndex, 1]).Value2 !=
null)
rowIndex++;

progressBar1.Maximum = rowIndex-2;

rowIndex = 2;

try
{
while (((Excel.Range)oSheet.Cells[rowIndex, 1]).Value2
!= null)
{
progressBar1.Value = rowIndex-2;
frmMain.sDBTest frmmainsDBTestobj = new
frmMain.sDBTest();
frmmainsDBTestobj.TestNumber =
Int32.Parse(((Excel.Range)oSheet.Cells[rowIndex,
1]).Value2.ToString());
frmmainsDBTestobj.Test =
((Excel.Range)oSheet.Cells[rowIndex, 2]).Value2.ToString();
// 1.1 frmmainsDBTestobj.Teststatus =
((Excel.Range)oSheet.Cells[rowIndex, 3]).Value2.ToString();
// 1.2 frmmainsDBTestobj.TestComment =
((Excel.Range)oSheet.Cells[rowIndex, 4]).Value2.ToString();
//frmmainsDBTestobj.TST =
((Excel.Range)oSheet.Cells[rowIndex, 5]).Value2.ToString();
// 1.3 frmmainsDBTestobj.iscomment =
bool.Parse(((Excel.Range)oSheet.Cells[rowIndex, 6]).Value2.ToString());
DBviewList.Add(frmmainsDBTestobj);
rowIndex++;
}
}
catch (Exception ex)
{

MessageBox.Show(ex.Message.ToString());
//app.Workbooks.Close();
//app.Quit();
}

app.Workbooks.Close();
app.Application.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComO bject(app);

System.Runtime.InteropServices.Marshal.ReleaseComO bject(oWB);
app.Application.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComO bject(oSheet);
app.Workbooks.Close();
app = null;
oWB = null;
oSheet = null;
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
return DBviewList;

}
In this case the excel instance is dying. if i remove lines 1.1,1.2
and1.3, then excel instance won''t die.

sDBTest is a public class where all members are string besides
iscomment(bool).

Thanks for the help.

Nicholas I don''t see where she forgot to release any object. as for the
class, it is being destroied after she calls GC.


这篇关于Excel实例赢了;如果将单元格内容复制到struct,则会死亡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆