static void Main(string[] args) { // AutomateExcel 通过Microsoft Excel 主要的互用组件(PIA)自动化 // Excel应用程序,并且将每个COM访问对象分配给一个新的变量,使用户最终 // 可以通过调用Marshal.FinalReleaseComObject方法释放这些变量。 Solution1.AutomateExcel(); // 输出空行 Console.WriteLine(); // AutomateExcel 通过Microsoft Excel 主要的互用组件(PIA)自动化 // Excel 应用程序,在自动化方法退出堆栈后执行垃圾收集器 //(此时RCW对象不再被引用),从而清除RCW并释放COM对象。 Solution2.AutomateExcel(); }
static void Main(string[] args) { // Solution1.AutomateExcel demonstrates automating Microsoft // Excel application by using Microsoft Excel PIA and explicitly // assigning each COM accessor object to a new varaible that you // would explicitly call Marshal.FinalReleaseComObject to release // it at the end. Solution1.AutomateExcel(); Console.WriteLine(); // Solution2.AutomateExcel demonstrates automating Microsoft // Excel application by using Microsoft Excel PIA and forcing a // garbage collection as soon as the automation function is off // the stack (at which point the RCW objects are no longer rooted) // to clean up RCWs and release COM objects. Solution2.AutomateExcel(); }