not releasing excel objects..pl see this simple code.

A

asp newbie

Please help...i tried so many things but I can't get all Excel objects
cleaned up. Here is my whole code...if I comment out everything in
for() loop below excel objects get cleaned up. otherwise I can see them
in Task Manager...its so weird.

Appreciate your help.
thanks

Excel.Application dstExcel = new Excel.ApplicationClass();
Excel._Workbook dstWb = dstExcel.Workbooks.Open(eventdir,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value,
Missing.Value, Missing.Value, Missing.Value);
Excel._Worksheet dstWsh = (Excel._Worksheet)dstWb.Sheets.get_Item(2);

int i = 0;
string vend, perc;

for ( i = 15; i <= 34; i++ )
{
if ( ((Excel.Range)dstMsh.Cells[i,4]).Value2 != null )
{
perc = ((Excel.Range)dstMsh.Cells[i,4]).Value2.ToString();
}
else
{
perc = "";
}

if ( ((Excel.Range)dstMsh.Cells[i,2]).Value2 != null )
{
vend = ((Excel.Range)dstMsh.Cells[i,2]).Value2.ToString();
}
else
{
vend = "";
}

// process 'perc' and 'vend' strings

}

// export excel chart
Excel.Range chartRange = dstMsh.get_Range("B15", "D" +
chartData.ToString());
Excel.ChartObjects chObjs =
(Excel.ChartObjects)dstMsh.ChartObjects(Type.Missing);
Excel.ChartObject chObj = chObjs.Add(100, 200, 300, 200);
Excel.Chart chart = chObj.Chart;

chart.SetSourceData(chartRange, Type.Missing);
chart.ChartArea.AutoScaleFont = false;
chart.ChartType = Excel.XlChartType.xlBarClustered;

string imgDir = "C:\\inetpub\\wwwroot\\apac2\\images\\";
string imgSaveAs = getCurrentTime()+ "_chart.JPG";
chart.Export(imgDir + imgSaveAs, "JPEG", false);

imgChart.Visible = true;
imgChart.ImageUrl = "images\\" + imgSaveAs;

System.Runtime.InteropServices.Marshal.ReleaseComObject (chartRange);
System.Runtime.InteropServices.Marshal.ReleaseComObject (chart);
System.Runtime.InteropServices.Marshal.ReleaseComObject (chObjs);
System.Runtime.InteropServices.Marshal.ReleaseComObject (chObj);
chartRange = null;
chObjs = null;
chObj = null;
chart = null;

System.Runtime.InteropServices.Marshal.ReleaseComObject (dstMsh);
dstMsh = null;

dstWb.Close(false, Missing.Value, Missing.Value);
dstExcel.Workbooks.Close();
dstExcel.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComObject (dstExcel);
System.Runtime.InteropServices.Marshal.ReleaseComObject (dstWb);
System.Runtime.InteropServices.Marshal.ReleaseComObject (dstWsh);

dstWsh = null;
dstWb = null;
dstExcel = null;

GC.Collect();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top