C# Excel com 对象的释放

本文介绍了一个用于管理Excel应用程序的单例类实现方法,包括如何创建和使用Excel.Workbook对象,以及如何打开、保存和关闭工作簿。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当引用Excel Application和Workbook ,Worksheet 对象的时候不需要用Marchal.ReleaseComObject来释放引用。

用任务管理器观察释放不会较少内存。

只需最后的时候关闭Workbook即可。

下面为管理Excel的单例类

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

namespace SuperMap.DZBSFZ.Common
{
    public class ExcelApplicationManage
    {
        Excel.Application ExcelApp = null;
        static ExcelApplicationManage thisExcelApplicationManage  = new ExcelApplicationManage();
        private object Nothing = System.Reflection.Missing.Value;
        /// <summary>
        /// 私有构造方法
        /// </summary>
        private ExcelApplicationManage() 
        {
        }
        /// <summary>
        /// 获得单例类对象
        /// </summary>
        /// <returns></returns>
        public static ExcelApplicationManage GetInstance()
        {     
            return thisExcelApplicationManage;
        }
        /// <summary>
        /// 初始化Excel.Application对象
        /// </summary>
        private void StartExcel() 
        {
            if(ExcelApp==null)
                ExcelApp = new Excel.Application();
        }
        /// <summary>
        /// 得到一个只有Sheet1的工作簿
        /// </summary>
        /// <returns>Excel.Workbook对象</returns>
        public Excel.Workbook GetExcelBook() 
        {
            if (ExcelApp == null)
                StartExcel();
            Excel.Workbook workbook = ExcelApp.Workbooks.Add(1);
            return workbook;
        }
        /// <summary>
        /// 根据名称打开Excel文件
        /// </summary>
        /// <param name="excelFileName">Excel文件名</param>
        /// <returns>Excel.Workbook对象</returns>
        public Excel.Workbook OpenExcelFile(string excelFileName) 
        {
            if (!System.IO.File.Exists(excelFileName))
                return null;
            if(ExcelApp==null) 
               StartExcel();
            Excel.Workbook Excelbook = null;
            try
            {
                Excelbook = ExcelApp.Workbooks.Open(excelFileName, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
            }
            catch 
            {
                return null;
            }
            return Excelbook;
        }
        /// <summary>
        /// 根据名称得到Excel表单对象
        /// </summary>
        /// <param name="Excelbook">Excel.Workbook对象</param>
        /// <param name="sheetName">Sheet文件名,例如 Sheet1</param>
        /// <returns>Excel.Worksheet对象</returns>
        public Excel.Worksheet GetWorksheet(Excel.Workbook Excelbook, string sheetName) 
        {
            if (Excelbook == null || sheetName == null || sheetName=="")
                return null;
            Excel.Worksheet ExcelSheet = null;
            try
            {
                ExcelSheet = (Excel.Worksheet)Excelbook.Worksheets.get_Item(sheetName);
            }
            catch 
            {
                return null;
            }
            return ExcelSheet;
        }
        /// <summary>
        /// 关闭Excel.Workbook对象
        /// </summary>
        /// <param name="Excelbook">Excel.Workbook对象</param>
        /// <param name="bSave">是否保存</param>
        public void CloseWorkbook(ref Excel.Workbook Excelbook,bool bSave) 
        {
            if (Excelbook == null)
                return;
            Excelbook.Close(bSave, null, null);
            Excelbook = null;
        }
        /// <summary>
        /// 另存Excel.Workbook对象
        /// </summary>
        /// <param name="Excelbook">Excel.Workbook对象</param>
        /// <param name="excelFileName">另存Excel文件名</param>
        /// <returns>是否成功</returns>
        public bool SaveAs(Excel.Workbook Excelbook, string excelFileName) 
        {
            if (Excelbook == null || excelFileName == null || excelFileName == "")
                return false;
            try
            {
                Excelbook.SaveAs(excelFileName, Excel.XlFileFormat.xlWorkbookNormal, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlNoChange, Nothing, Nothing, Nothing, Nothing, Nothing);
            }
            catch 
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 关闭Excel.Worksheet对象
        /// </summary>
        /// <param name="Excelsheet">Excel.Worksheet对象</param>
        public void CloseWorksheet(ref Excel.Worksheet Excelsheet)
        {
            if (Excelsheet == null)
                return;
            Excelsheet = null;
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值