System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Почему после завершения работы с Excel из C# Excel остаётся в памяти?
Код:
public void ExportToExcel(string fileName, object[,] data, uint startPosition, uint count)
{
lock (this)
{
if (string.IsNullOrEmpty(fileName))
{
throw new ApplicationException("File name can't be null or empty.");
}
if (data == null)
{
throw new NullReferenceException("Data can't be null.");
}
else if (data.Length == 0)
{
throw new ApplicationException("No data.");
}
if (startPosition + count > data.GetLength(0))
{
throw new ApplicationException(string.Format("You try to use {0} rows whereas data array contains {1} rows.", (startPosition + count), data.GetLength(0)));
}
// This lines requires Excel application for own start.
CultureInfo oldCI = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Application excelApplication = new Application();
if (excelApplication == null)
{
QuitFromExcel(fileName, oldCI, excelApplication, null, null);
throw new ApplicationException("EXCEL couldn't be started!");
}
Workbooks workbooks = excelApplication.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
if (worksheet == null)
{
QuitFromExcel(fileName, oldCI, excelApplication, workbook, worksheet);
throw new ApplicationException("Worksheet == null.");
}
try
{
AddToExcelColumnsTitles(worksheet);
FillExcelTable(data, startPosition, count, worksheet);
}
catch (ApplicationException exception)
{
QuitFromExcel(fileName, oldCI, excelApplication, workbook, worksheet);
throw new ApplicationException(exception.Message);
}
QuitFromExcel(fileName, oldCI, excelApplication, workbook, worksheet);
}
}
private void QuitFromExcel(string fileName, CultureInfo oldCI, Application excelApplication, _Workbook workbook, _Worksheet worksheet)
{
if (workbook != null)
{
workbook.SaveCopyAs(fileName);
workbook.Close(false, "", Missing.Value);
}
if (excelApplication != null)
{
excelApplication.Quit();
Marshal.ReleaseComObject(excelApplication);
}
workbook = null;
worksheet = null;
excelApplication = null;
// This line requires Excel application. Such as CurrentCulture property was changed.
Thread.CurrentThread.CurrentCulture = oldCI;
GC.GetTotalMemory(true);
}
{
lock (this)
{
if (string.IsNullOrEmpty(fileName))
{
throw new ApplicationException("File name can't be null or empty.");
}
if (data == null)
{
throw new NullReferenceException("Data can't be null.");
}
else if (data.Length == 0)
{
throw new ApplicationException("No data.");
}
if (startPosition + count > data.GetLength(0))
{
throw new ApplicationException(string.Format("You try to use {0} rows whereas data array contains {1} rows.", (startPosition + count), data.GetLength(0)));
}
// This lines requires Excel application for own start.
CultureInfo oldCI = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Application excelApplication = new Application();
if (excelApplication == null)
{
QuitFromExcel(fileName, oldCI, excelApplication, null, null);
throw new ApplicationException("EXCEL couldn't be started!");
}
Workbooks workbooks = excelApplication.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
if (worksheet == null)
{
QuitFromExcel(fileName, oldCI, excelApplication, workbook, worksheet);
throw new ApplicationException("Worksheet == null.");
}
try
{
AddToExcelColumnsTitles(worksheet);
FillExcelTable(data, startPosition, count, worksheet);
}
catch (ApplicationException exception)
{
QuitFromExcel(fileName, oldCI, excelApplication, workbook, worksheet);
throw new ApplicationException(exception.Message);
}
QuitFromExcel(fileName, oldCI, excelApplication, workbook, worksheet);
}
}
private void QuitFromExcel(string fileName, CultureInfo oldCI, Application excelApplication, _Workbook workbook, _Worksheet worksheet)
{
if (workbook != null)
{
workbook.SaveCopyAs(fileName);
workbook.Close(false, "", Missing.Value);
}
if (excelApplication != null)
{
excelApplication.Quit();
Marshal.ReleaseComObject(excelApplication);
}
workbook = null;
worksheet = null;
excelApplication = null;
// This line requires Excel application. Such as CurrentCulture property was changed.
Thread.CurrentThread.CurrentCulture = oldCI;
GC.GetTotalMemory(true);
}
Код:
Попробуйте:
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant covFalse((short)FALSE);
workbook.Close(covFalse, covOptional, covOptional);
workbook.ReleaseDispatch();
workbooks.Close();
workbooks.ReleaseDispatch();
excelApplication.Quit();
excelApplication.ReleaseDispatch();
Тогда сообщение о сохранении не должно вас беспокоить