从C#关闭excel工作簿 [英] Closing excel workbook from C#

查看:73
本文介绍了从C#关闭excel工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试使用Excel对象来创建工作簿。除非由于某种原因程序崩溃,然后工作簿在后台保持打开,否则此工作文件。下次运行代码时,它会抛出一个工作簿已经打开的错误。我怎么关闭它?我不想杀死EXCEL进程,因为这会关闭所有其他打开的工作簿。



这是我的代码:



Hi

I am trying to use Excel object to create a workbook. This works file unless the program crashes for some reason and next the workbook keeps open in the background. Next time when the code is run, it throws an error that workbook is already open. How do I close it ? I donot want to kill EXCEL process as that would close all other open workbooks.

Here is my code :

using System;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;

namespace ConsoleApplication1
{
    class Program
    {
        private static Excel.Application excelApp = null;
        private static Excel.Workbook workbook = null;
        private static Excel.Sheets sheets = null;
        private static Excel.Worksheet newSheet = null;

        static void Main(string[] args)
        {
            excelApp = StartExcel();
            //excelApp.Visible = true;
             
            workbook = excelApp.Workbooks.Add(1);                
            
            string filePath = "C:\\test.xls";
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            excelApp.DisplayAlerts = false;

            workbook = excelApp.Workbooks.Add(1);
            workbook.SaveAs(filePath, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            
            workbook = excelApp.Workbooks.Open(filePath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);

            sheets = workbook.Sheets;
            newSheet = (Excel.Worksheet)sheets.Add(sheets[1], Type.Missing, Type.Missing, Type.Missing);
            newSheet.Name = "My sheet";

            //excelApp.Application.ActiveWorkbook.Close(false, Type.Missing, Type.Missing); 
            workbook.Close(false, Type.Missing, Type.Missing);
        }

        public static Excel.Application StartExcel()
        {
            Excel.Application instance = null;
            try
            {
                instance = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                instance = new Excel.Application();
            }

            return instance;
        }
    }
}







谢谢,

Monica




Thanks,
Monica

推荐答案

你还没有发布Excel使用的com引用。以下链接解释了如何释放com对象。



如何正确清理excel-interop-objects [ ^ ]



从上面的参考文献替换这一行

you havent released the com reference used by the Excel. The following link explains it well how to release the com objects.

how-to-properly-clean-up-excel-interop-objects[^]

From the above reference replace this line
workbook = excelApp.Workbooks.Add(1);




to

Excel.Workbooks workbooks = excelApp.Workbooks;
workbook = workbooks.Add(1);



并添加以下代码以释放你的com参考。


and add the following code for releasing your com references.

workbook.Close(false, Type.Missing, Type.Missing);

excelApp.Quit();
GC.Collect();

Marshal.FinalReleaseComObject(sheets);
Marshal.FinalReleaseComObject(newSheet);

Marshal.FinalReleaseComObject(workbook);
Marshal.FinalReleaseComObject(workbooks);

Marshal.FinalReleaseComObject(excelApp);





我不明白你为什么在紧接着之后调用Open语句另外,为什么不使用之前保存的相同工作簿?你可以删除该行。



I dont understand why you call the Open statement just after the SaveAs, why dont you use the same workbook just saved before? you can remove that line.


这篇关于从C#关闭excel工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆