C#:如何打开和closuresExcel工作簿?

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

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

我猜我需要引用Microsoft.Office.Interop.Excel程序集。


*原因:我已经使用第三方库(Aspose)configuration了数据透视表信息。 现在我需要读取生成的数据透视表。

不幸的是,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); } 

一个快速的Google给我这个代码项目:

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

考虑使用System.Diagnostics.Process来启动,监视和closuresExcel。 这个网站给出了一个很好的介绍: http : //www.thescarms.com/dotnet/Process.aspx ,包括运行一个不可见的窗口和发送input。