当引用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;
}
}
}