I've been looking to similar questions trying to solve this issue to no avail:
The process EXCEL.EXE is not closing after use, despite closing the Workbook, Workbooks and Application objects. I've also tried using Marshal.ReleaseComObject
to release them and also the Range Sheet and Sheets objects. None of the answers I looked up have worked.
Some of the answers I looked up:
How do I properly clean up Excel interop objects?
Cannot close Excel.exe after Interop process
Here's the code I've been using:
Resultado res = new Resultado();
List<RegistroHH> lista = MantenedoresNegocio.RegistrosHH.Listar(ccosto, 1);
//Excel.Application app = new Application();
Excel.Application app = null;
Excel.Workbooks books = null;
Excel._Workbook wbook = null;
Excel._Worksheet sheet = null;
Excel.Sheets sheets = null;
Excel.Range rang = null;
try
{
//Start Excel and get Application object.
app = new Excel.Application();
app.Visible = true;
app.DisplayAlerts = false;
//Get a new workbook.
books = app.Workbooks;
wbook = books.Add(Missing.Value);
sheets = wbook.Sheets;
//sheet = (Excel._Worksheet)wbook.ActiveSheet;
sheet = sheets.Add();
int idActividad = 0;
List<string> horas = new List<string>();
rang = sheet.Cells[1, 1] as Excel.Range;
rang.Value2 = "ID";
rang.Borders.Weight = Excel.XlBorderWeight.xlThin;
rang.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkBlue);
rang = sheet.Cells[1, 2] as Excel.Range;
rang.Value2 = "Actividad";
rang.Borders.Weight = Excel.XlBorderWeight.xlThin;
Dictionary<int, string> responsables = new Dictionary<int, string>();
List<string> columnasResponsables = new List<string>();
int idColumna = 3;
int idFila = 1;
foreach (RegistroHH reg in lista)
{
if (reg.idActividad != idActividad)
{
idColumna = 3;
rang = sheet.Cells[idFila, 1] as Excel.Range;
rang.NumberFormat = "@";
rang.Value2 = reg.jerarquia;
rang.Borders.Weight = Excel.XlBorderWeight.xlThin;
rang = sheet.Cells[idFila, 2] as Excel.Range;
rang.Value2 = reg.nombreActividad;
rang.Borders.Weight = Excel.XlBorderWeight.xlThin;
idFila++;
}
if (!responsables.ContainsKey(reg.idResponsable))
{
responsables.Add(reg.idResponsable, reg.nombreResponsable);
rang = sheet.Cells[1, idColumna] as Excel.Range;
rang.Value2 = reg.nombreResponsable;
rang.Borders.Weight = Excel.XlBorderWeight.xlThin;
}
rang = sheet.Cells[idFila, idColumna] as Excel.Range;
if (reg.horasAsignadas != 0)
rang.Value2 = reg.horasAsignadas;
/*
if (!dt.Columns.Contains(reg.nombreResponsable.ToString()))
dt.Columns.Add(reg.nombreResponsable.ToString());
horas.Add((reg.horasAsignadas > 0 ? reg.horasAsignadas.ToString() : ""));
*/
idActividad = reg.idActividad;
idColumna++;
}
rang = sheet.Columns[2] as Excel.Range;
rang.AutoFit();
wbook.SaveAs(ruta);
res.CodigoResultado = 0;
res.Mensaje = "Exito";
}
catch (Exception theException)
{
res.CodigoResultado = -1;
res.Mensaje = theException.Message;
}
finally
{
if (rang != null) Marshal.ReleaseComObject(rang);
if (sheet != null) Marshal.ReleaseComObject(sheet);
if (sheets != null) Marshal.ReleaseComObject(sheets);
if (wbook != null)
wbook.Close();
if (wbook != null) Marshal.ReleaseComObject(wbook);
if (books != null)
books.Close();
if (books != null) Marshal.ReleaseComObject(books);
if (app != null)
{
app.Application.Quit();
app.Quit();
}
if (app != null) Marshal.ReleaseComObject(app);
//cleanup
GC.Collect();
GC.WaitForPendingFinalizers();
}