由于在C#中读写Excel文档,导致资源监控中的内存增长内存泄漏

问题描述

我有一个用c#编写的程序。在程序执行期间,我正在以不同的任务将数据写入Excel文件,运行宏并从这些Excel文件中读取数据。

在资源监视视图之后,我看到由于读写操作导致内存消耗增加

我附加了我写的读写功能和资源监控视图的屏幕截图-我看到内存在不断增长

这是内存泄漏的情况吗? 有谁知道“内存转义”的源头吗?

public string[] GettingValuesFromColumn(string excelSheetName,string excelTableColumnName)
{
    List<string> result = new List<string>();
    int colCount,rowCount,iColIndex = -1;
    string value;
    string[] result_arr = null;

    Microsoft.Office.Interop.Excel.Range xlRange = null;

    try
    { 
        xlRange = (ExcelBook.Worksheets[excelSheetName]).UsedRange;
        //Getting Requested Column:
        rowCount = xlRange.Rows.Count;
        colCount = xlRange.Columns.Count;

        //Getting Index of Requested Column:
        for (int j = 1; j <= colCount; j++)
            if (xlRange.Cells[1,j] != null)
            {
                if (xlRange.Cells[1,j].Value2 != null)
                {
                    value = Convert.ToString(xlRange.Cells[1,j].Value2);
                    if (value == excelTableColumnName)
                        iColIndex = j;
                }
            }

        //Getting Values of Requested Column:
        if (iColIndex == -1)
            //printing error
        else
        {
            for (int i = 2; i <= rowCount; i++)
            {
                if (xlRange.Cells[i,iColIndex] != null)
                {
                    if (xlRange.Cells[i,iColIndex].Value2 != null)
                    {
                        value = string.Empty;
                        value = Convert.ToString(xlRange.Cells[i,iColIndex].Value2);
                        result.Add(value);
                    }
                }
            }
            result_arr = result.ToArray();
        }
    }
    catch (Exception e)
    {
        //printing error
    }
    finally
    {
        if (xlRange != null)
        {
            Marshal.FinalReleaseComObject(xlRange);
            xlRange = null;
        }
        //cleanup
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }

    return result_arr;
}


public string SettingValuesToColumn(string excelSheetName,string excelTableColNameForKey,string excelTableColNameForValue,Dictionary<string,string> dataDict)
{
    int iColKeyIndex = -1,iColValueIndex = -1,colCount,rowCount;
    string value,key,strResult = "";

    Microsoft.Office.Interop.Excel.Range xlRange = null;

    try
    {
        xlRange = (ExcelBook.Worksheets[excelSheetName]).UsedRange;
        //Getting Requested Column:
        rowCount = xlRange.Rows.Count;
        colCount = xlRange.Columns.Count;

        for (int j = 1; j <= colCount; j++)
        {
            if (xlRange.Cells[1,j].Value2);
                    if (xlRange.Cells[1,j].Value2 == excelTableColNameForKey)
                        iColKeyIndex = j;
                    if (xlRange.Cells[1,j].Value2 == excelTableColNameForValue)
                        iColValueIndex = j;
                }
            }
        }
        //Getting Values of Requested Column:
        if (iColKeyIndex == -1 || iColValueIndex == -1)
        {
           //printing error
        }
        else
        {
            for (int i = 2; i <= rowCount; i++)
            {
                if (xlRange.Cells[i,iColKeyIndex] != null)
                {
                    if (xlRange.Cells[i,iColKeyIndex].Value2 != null)
                    {
                        key = Convert.ToString(xlRange.Cells[i,iColKeyIndex].Value2);
                        value = dataDict[key];
                        xlRange.Cells[i,iColValueIndex].Value2 = value;
                    }
                }
            }
        }
    }
    catch (Exception e)
    {
        //printing error
    }
    finally
    {
        if (xlRange != null)
        {
            Marshal.FinalReleaseComObject(xlRange);
            xlRange = null;
        }
        //cleanup
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
    return strResult;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)