有人知道如何简单地打开和关闭Excel工作簿吗?
我不需要从文件中读取任何数据,只需要打开和关闭它。 (*)
我猜我需要引用Microsoft.Office.Interop.Excel程序集。
*原因: 我已经使用第三方库(Aspose)配置了数据透视表信息。现在我需要读取生成的数据透视表。
不幸的是,Aspose库无法在运行时生成数据透视表。需要有人用Excel打开文件,这样Excel才能生成数据透视表数值。
有人知道如何简单地打开和关闭Excel工作簿吗?
我不需要从文件中读取任何数据,只需要打开和关闭它。 (*)
我猜我需要引用Microsoft.Office.Interop.Excel程序集。
*原因: 我已经使用第三方库(Aspose)配置了数据透视表信息。现在我需要读取生成的数据透视表。
不幸的是,Aspose库无法在运行时生成数据透视表。需要有人用Excel打开文件,这样Excel才能生成数据透视表数值。
在引用Microsoft.Office.Interop.Excel之后,还要确保在finally中进行清理。
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);
}
_Excel.Quit()
来关闭 Excel 应用程序。 - arthur