Ich habe einen Code geschrieben, aber ich kann Application.Cells nicht korrekt freigeben. Kann mir jemand helfen, Excel.exe im Taskmanager freizugeben?Wie man Application.Cells richtig freigibt?
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application app = null;
Excel.Workbooks books = null;
Excel.Workbook book = null;
Excel.Sheets sheets = null;
for (int a = 0; a < dgrviRoute.RowCount; a++)
{
for (int b = 0; b < dgrviRoute.ColumnCount; b++)
{
DataGridViewRow row = dgrviRoute.Rows[a];
DataGridViewCell cell = row.Cells[b];
var value = cell.Value;
app.Cells[a + 2, b + 1] = value;
/*
I still see the Excel.exe process in the Windows Task Managers’
list of background processes.
This strange phenomenon occurs because in the above code,
Iam not releasing any COM objects and we’re also “chaining”
object references by using app.Cells[a + 2, b + 1].
*/
/*
if (app.Cells[a + 2, b + 1] != null) Marshal.FinalReleaseComObject(app.Cells[a + 2, b + 1]);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
*/
}
}
Ich las bei hier einige Tutorial How to properly release Excel COM objects