private void initFile(string path) { Microsoft.Office.Interop.Excel.Application excelApplication = null; Workbook excelWorkbook = null; try { excelApplication = new Microsoft.Office.Interop.Excel.Application(); excelWorkbook = excelApplication.Workbooks.Open(path); for (int i = 1; i <= excelWorkbook.Worksheets.Count; i++) { dynamic dynamicWorksheet = excelWorkbook.Worksheets[i]; Worksheet worksheet = excelWorkbook.Worksheets[i]; ExcelWorksheet excelWorksheet = new ExcelWorksheet(dynamicWorksheet.Name); Range lastRow = dynamicWorksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing); Range workingRange = worksheet.get_Range("A1", lastRow); int numberOfRows = workingRange.Rows.Count; for (int j = 2; j <= numberOfRows; j++) { dynamic currentRow = workingRange.Rows[j]; dynamic cell1 = currentRow.Cells[1]; dynamic val1 = cell1.Value; dynamic cell2 = currentRow.Cells[2]; dynamic val2 = cell2.Value; dynamic cell5 = currentRow.Cells[5]; dynamic val5 = cell5.Value; dynamic cell8 = currentRow.Cells[8]; dynamic val8 = cell8.Value; try { int noCrt = Convert.ChangeType(val1, typeof(int)); DateTime date = val2 == null ? DateTime.MinValue : this.getDateTime(val2); double factorDePutere = val5 == null ? 0 : Convert.ChangeType(val5, typeof(double)); double putereReactiva = val8 == null ? 0 : Convert.ChangeType(val8, typeof(double)); if (date >= this.startInterval && date <= this.endInterval) { ExcelLine excelLine = new ExcelLine(noCrt, date, factorDePutere, putereReactiva); excelWorksheet.AddLine(excelLine); this.appendLine(excelLine); this.sum(excelLine); } } catch (Exception ex) { Console.WriteLine(ex); } processWorker.ReportProgress(j * 100 / numberOfRows); } myWorkbook.AddWorkshets(excelWorksheet); if (i == 1) { break; } } } catch (Exception ex) { Console.WriteLine(ex); } finally { try { excelWorkbook.Close(SaveChanges: true); } catch { } excelApplication.Quit(); if (excelWorkbook != null) { Marshal.ReleaseComObject(excelWorkbook); } if (excelApplication != null) { Marshal.ReleaseComObject(excelApplication); } excelWorkbook = null; excelApplication = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } }