Office PIA:注意Office App进程的管理
来源:互联网 发布:淘宝比价软件叫什么 编辑:程序博客网 时间:2024/05/12 01:48
在Office PIA应用程序中,对Office App的管理不容忽视。
在正常情况下,程序launch office app以后,通过interop的COM组件进行文档操作,然后保存、关闭office app。但这个过程在出错的情况下,office app可能还没有关闭,并且依然占用着所操作的文件,这当然不是我们期望看到的。
以ExcelApp这个类为例,看看我在里面是怎么考虑的。以下贴出一些相关的程序片断:
using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Threading; using Excel = Microsoft.Office.Interop.Excel; /// <summary> /// Excel application interop class. /// </summary> public class ExcelApp : IDisposable { #region Const definition /// <summary>Microsoft Excel application process name.</summary> private const string ExcelProcessName = "EXCEL"; #endregion #region Private variables /// <summary>Original Excel Application processes before launching new Excel process.</summary> private Process[] originalExcelProcess = null; #endregion #region Initializations and Finalizations /// <summary> /// Initializes a new instance of the ExcelApp class. /// </summary> public ExcelApp() { // ...... this.originalExcelProcess = Process.GetProcessesByName(ExcelProcessName); } /// <summary> /// Finalizes an instance of the ExcelApp class. /// </summary> ~ExcelApp() { this.Dispose(false); } #endregion #region Private methods for IDisposable /// <summary> /// Dispose method /// </summary> /// <param name="disposing">Disposing status</param> protected void Dispose(bool disposing) { if (this.disposed) { return; } if (disposing) { // Cleanup managed resource this.Close(); } // Cleanup un-managed resource // ... this.CleanUnhandledAppInstance(); this.disposed = true; } #endregion #region Private Excel methods - Application /// <summary> /// clean up unhandled Excel application instances. /// </summary> private void CleanUnhandledAppInstance() { Process[] currentExcelProcess = Process.GetProcessesByName(ExcelProcessName); if (currentExcelProcess == null || currentExcelProcess.Length == 0) { return; } List<Process> killList = null; if (this.originalExcelProcess == null || this.originalExcelProcess.Length == 0) { killList = new List<Process>(currentExcelProcess); } else { killList = new List<Process>(); bool isExists; foreach (Process process in currentExcelProcess) { isExists = false; foreach (Process originalProcess in this.originalExcelProcess) { if (originalProcess.Id == process.Id) { isExists = true; break; } } if (!isExists) { killList.Add(process); } } } if (killList != null || killList.Count > 0) { foreach (Process process in killList) { process.Kill(); } } } #endregion }
当考虑到某个操作可能会意外终止时:
#region Public Excel methods - Workbook /// <summary> /// Creates the excel doc. /// </summary> /// <param name="fileName">the excel doc filename</param> public void CreateWorkbook(string fileName) { try { this.CurrentWorkbook = this.Application.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet); this.LatestWorksheet = this.CurrentWorkbook.Sheets[1] as Excel.Worksheet; this.CurrentWorksheet = this.LatestWorksheet; string fullPath = this.GetActualFullPath(fileName); this.LatestWorksheet.SaveAs( fullPath, this.currentFileFormat, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing /* 28591 */ /* ISO 8859-1 Latin 1; Western European (ISO) */, Type.Missing, Type.Missing); } catch (Exception ex) { this.Dispose(); throw ex; } } #endregion
- Office PIA:注意Office App进程的管理
- Office PIA:关于Office PIA应用程序的部署
- Office 2013 PIA 组件的GUID
- 获取 Office 2003 PIA
- Office 2007 PIA Demo
- .net Office PIA 2007
- Office PIA:代码示例:生成Excel文件的ExcelApp类
- 我的Office PIA之路(一) Microsoft.Office.Interop.Excel.dll 的获取
- office的管理寓言
- office 安裝到GAC,通過pia安裝
- ASP.NET Office PIA 编程心得
- 当Office PIA遭遇C# 4.0
- 风靡office的管理寓言
- 风靡office的管理寓言
- 风靡office的管理寓言
- Web服务中调用office pia 操作WORD,EXCEL时需做的设置
- Office 2003 PIA编程的问题:文件保存为XlFileFormat.xlExcel7格式时Range.Merge()失效
- 解决安装不同版本office后造成的PIA COM组件调用失败
- Android之Task Activity
- Lanuch4j为java 程序生成EXE可执行文件
- sscanf
- 老外们汉语考试
- nodejs包结构
- Office PIA:注意Office App进程的管理
- 一个java的小项目
- 前端知识
- 周日 将在 广州软件测试俱乐部 举行一次小规模的持续集成、自动化测试、敏捷测试的研讨会!高手云集!
- 第一章 安装
- 初识Struts(九)-------动态验证框架validator
- Centos5下构建 MySQL服务器和常见问题解决办法
- android 安全设置相关at流程分析
- WinCE系统下基于DirectShow的摄像头应用编程