C#Microsoft Excel 16.0 Object Library automatically releases resources after using Excel, and closes the background running process method

Because of the need to read Excel in xls format, use Microsoft Excel 16.0 Object Library to solve the method of automatically releasing resources after using Excel and closing the background running process (it will be automatically released when the program needs to be closed, or you can use the handle to close the process method)

using Excel = Microsoft.Office.Interop.Excel;

public class ExcelHelper
{
    public void ProcessExcelFile(string filePath)
    {
        Excel.Application excelApp = null;
        Excel.Workbook workbook = null;
        Excel.Worksheet worksheet = null;

        try
        {
            // 创建Excel应用程序对象
            excelApp = new Excel.Application();

            // 打开Excel文件
            workbook = excelApp.Workbooks.Open(filePath);

            // 获取第一个工作表
            worksheet = workbook.Worksheets[1];

            // 在这里进行Excel操作,例如读取或写入数据

            // 保存并关闭Excel文件
            workbook.Save();
            workbook.Close();

            // 释放Excel对象
            Marshal.ReleaseComObject(worksheet);
            Marshal.ReleaseComObject(workbook);
            Marshal.ReleaseComObject(excelApp);
        }
        catch (Exception ex)
        {
            // 处理异常
        }
        finally
        {
            // 确保释放资源
            if (worksheet != null)
            {
                Marshal.FinalReleaseComObject(worksheet);
            }
            if (workbook != null)
            {
                Marshal.FinalReleaseComObject(workbook);
            }
           
        }
    }
}

Note: This method can only be used once to open Excel and close the process. It may not work multiple times. You need to completely close the Microsoft Excel process. Please use the method used to kill the Excel process:

using System;
using System.Diagnostics;
public static void KillExcelProcesses()
    {
        Process[] processes = Process.GetProcessesByName("EXCEL");

        foreach (Process process in processes)
        {
            try
            {
                process.Kill();
                process.WaitForExit();
                process.Close();
            }
            catch (Exception ex)
            {
                // 处理异常情况
                Console.WriteLine("无法杀死Excel进程:" + ex.Message);
            }
        }
    }

For other methods, please refer to: C# completely release EXCEL_c# release excel_lainY7mail's blog-CSDN blog

The above content is used to record your own learning and sharing, and sharing makes technology go further!

Technology comes from the innovation of freedom of thought, independent thinking, dialectical analysis, logical thinking and human freedom, to serve the various conveniences of human life, not to use technology to create ideological divides and siege to create stupidity; technology is good and bad, It is better for good people to use technology, and it is worse for bad people to use technology. Recommended books: George Orwell's "1984", Zamyatin's "Us", Aldous Huxley's "Brave New World", there are multiple translations, just choose the one you like and the good one; There are Plato's "Utopia", [France] Montesquieu's "On the Spirit of Law", Hayek's "The Road to Serfdom" (Hong Kong version) (the Hong Kong version is better translated from the mainland version, and the one with good English is the best. Good-looking original English version), and now the online store is selling it! Reading good books, reading books that most people have never read and not in classrooms, and reading knowledge that is not available in this land can increase a kind of speculative thinking and wisdom, and only then can we get out of the cave of narrow thinking and prejudice!

Plato wrote the cave theory in the seventh volume of his masterpiece "Utopia": a group of people who were imprisoned and lived in the cave since childhood, under the light outside the cave, they saw black shadows when they looked inside, and black shadows when they looked outside. Bright sea and sky, the more you look, the brighter it is!

Well-informed and tolerant people are more confident and diverse in their thinking. A culture of tolerance makes people stronger. The more confident and tolerant the stronger! Tolerance allows you to use the Internet, telephone, various electrical appliances, automobiles, chips, railways, high-speed rail...  

Guess you like

Origin blog.csdn.net/m0_58015531/article/details/132347311