A
alexia.bee
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.sDBTest> DBviewList)
{
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.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
app.Application.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(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.
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.sDBTest> DBviewList)
{
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.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
app.Application.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(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.