如何打开,读取和关闭excel互操作流程(C#)

时间:2018-02-01 10:15:01

标签: c# excel process interop

我必须执行一个程序,该程序读取excel xlsx文件并将数据存储到数据库中。这是我的实际简单代码,我甚至没有从excel文件中获取值,但我无法杀死该进程。我已经google了很多东西,我尝试了很多东西,但excel过程仍然存在。

public void readFile(path)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application xlApp = null;
                Microsoft.Office.Interop.Excel.Workbooks workbooks = null;
                Microsoft.Office.Interop.Excel.Workbook xlWorkbook = null;
                Microsoft.Office.Interop.Excel.Worksheet xlWorksheet = null;
                Microsoft.Office.Interop.Excel.Range xlRange = null;

                xlApp = new Microsoft.Office.Interop.Excel.Application();
                workbooks = xlApp.Workbooks;
                xlWorkbook = workbooks.Open(path);
                xlWorksheet = xlWorkbook.Sheets[1];
                xlRange = xlWorksheet.UsedRange;

                //------Here is where I will read the data

                xlWorkbook.Close();
                workbooks.Close();
                xlApp.Quit();

                Marshal.FinalReleaseComObject(xlRange);
                Marshal.FinalReleaseComObject(xlWorksheet);
                Marshal.FinalReleaseComObject(xlWorkbook);
                Marshal.FinalReleaseComObject(workbooks);
                Marshal.FinalReleaseComObject(xlApp);

                xlRange = null;
                xlWorksheet = null;
                xlWorkbook = null;
                workbooks = null;
                xlApp = null;
            }
            catch (Exception e)
            {

            }
        }

这个过程还活着。我希望你能帮助我。

3 个答案:

答案 0 :(得分:1)

我尝试了很多东西......下面的代码似乎有效。

public void readFile(path)
            {
                try
                {
                    Microsoft.Office.Interop.Excel.Application xlApp = null;
                Microsoft.Office.Interop.Excel.Workbooks workbooks = null;
                Microsoft.Office.Interop.Excel.Workbook xlWorkbook = null;
                Microsoft.Office.Interop.Excel.Sheets xlsheets = null;
                Microsoft.Office.Interop.Excel.Worksheet xlWorksheet = null;
                Microsoft.Office.Interop.Excel.Range xlRange = null;

                xlApp = new Microsoft.Office.Interop.Excel.Application();
                workbooks = xlApp.Workbooks;
                xlWorkbook = workbooks.Open(path);
                xlsheets = xlWorkbook.Sheets;
                xlWorksheet = xlsheets[1];
                xlRange = xlWorksheet.UsedRange;

                //--------------------------------------------------------------------

                xlWorkbook.Close();
                workbooks.Close();
                xlApp.Quit();

                Marshal.FinalReleaseComObject(xlRangeColumns);
                Marshal.FinalReleaseComObject(xlRangeRows);
                Marshal.FinalReleaseComObject(xlRange);
                Marshal.FinalReleaseComObject(xlWorksheet);
                Marshal.FinalReleaseComObject(xlsheets);
                Marshal.FinalReleaseComObject(xlWorkbook);
                Marshal.FinalReleaseComObject(workbooks);
                Marshal.FinalReleaseComObject(xlApp);

                xlRangeColumns = null;
                xlRangeRows = null;
                xlRange = null;
                xlWorksheet = null;
                xlsheets = null;
                xlWorkbook = null;
                workbooks = null;
                xlApp = null;
                }
                catch (Exception e)
                {

                }
            }

差异很小。一个月前我会告诉你两个代码都是一样的。有人可以告诉我为什么这段代码有效,而第一段代码却没有?

答案 1 :(得分:0)

尝试将其放在代码的末尾,它会杀死你的excel

foreach (var process in Process.GetProcessesByName("myExcelFilename"))
{
    process.Kill();
}

答案 2 :(得分:-2)

您需要将对象放入使用块,或将结束语句放入finally中。

每当您的应用程序抛出异常时,它都会打开互操作。您应该做的是将关闭代码放在finally块中,这意味着代码将始终运行。像这样:

    public void readFile(string path)
    {
        Microsoft.Office.Interop.Excel.Application xlApp = null;
        Microsoft.Office.Interop.Excel.Workbooks workbooks = null;
        Microsoft.Office.Interop.Excel.Workbook xlWorkbook = null;
        Microsoft.Office.Interop.Excel.Worksheet xlWorksheet = null;
        Microsoft.Office.Interop.Excel.Range xlRange = null;

        try
        {


            xlApp = new Microsoft.Office.Interop.Excel.Application();
            workbooks = xlApp.Workbooks;
            xlWorkbook = workbooks.Open(path);
            xlWorksheet = xlWorkbook.Sheets[1];
            xlRange = xlWorksheet.UsedRange;

            //------Here is where I will read the data




        }
        catch (Exception e)
        {

        }
        finally
        {
            xlWorkbook?.Close();
            workbooks?.Close();
            xlApp?.Quit();

            Marshal.FinalReleaseComObject(xlRange);
            Marshal.FinalReleaseComObject(xlWorksheet);
            Marshal.FinalReleaseComObject(xlWorkbook);
            Marshal.FinalReleaseComObject(workbooks);
            Marshal.FinalReleaseComObject(xlApp);                

            xlRange = null;
            xlWorksheet = null;
            xlWorkbook = null;
            workbooks = null;
            xlApp = null;
        }
    }

另一个选择是不要使用互操作并使用另一个库,例如EPPlus(在NuGet上可用)来处理excel文件的读取,这将在程序关闭时关闭。