有谁知道如何简单地打开和关闭Excel工作簿?

我不需要从文件中读取任何数据,我只需要打开和关闭。 (*)

我猜测,我会需要引用的Microsoft.Office.Interop.Excel组件。


*原因是:我已经与第三方库(阅读Aspose),被配置枢轴表信息。现在我需要读取所生成的数据透视表。

不幸的是,阅读Aspose库不能在运行时生成透视表。它需要有人来打开用Excel 文件以便Excel可以生成枢轴表值。

有帮助吗?

解决方案

引用的Microsoft.Office.Interop.Excel也确保在最后清理后。

using Excel = Microsoft.Office.Interop.Excel;

        Excel.ApplicationClass _Excel;
        Excel.Workbook WB;
        Excel.Worksheet WS;

    try
        {

        _Excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
        WB = _Excel.Workbooks.Open("FILENAME",
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing);

            //do something

        }
        catch (Exception ex)
        {
            WB.Close(false, Type.Missing, Type.Missing);

            throw;
        }
        finally
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();

            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WB);

            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_Excel);


        }

其他提示

一个快速谷歌给了我这个代码项目:

http://www.codeproject.com/KB/office/csharp_excel.aspx

考虑使用的System.Diagnostics.Process启动,监视和关闭Excel。这个网站提供了一个很好的介绍: http://www.thescarms.com/dotnet/Process.aspx ,包括一个不可见的窗口中运行并发送输入给它。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top