Exemplo n.º 1
0
        /// <summary>
        /// 释放内存
        /// </summary>
        public void Dispose(Microsoft.Office.Interop.Excel._Worksheet CurSheet, Microsoft.Office.Interop.Excel._Workbook CurBook, Microsoft.Office.Interop.Excel._Application CurExcel)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(CurSheet);
                CurSheet = null;
                CurBook.Close(false, mValue, mValue);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(CurBook);
                CurBook = null;

                CurExcel.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(CurExcel);
                CurExcel = null;

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            catch (System.Exception ex)
            {
                // HttpContext.Current.Response.Write("在释放Excel内存空间时发生了一个错误:" + ex);
            }
            finally
            {
                foreach (System.Diagnostics.Process pro in System.Diagnostics.Process.GetProcessesByName("Excel"))
                    //if (pro.StartTime < DateTime.Now)
                    pro.Kill();
            }
            System.GC.SuppressFinalize(this);
        }
Exemplo n.º 2
0
        public Microsoft.Office.Interop.Word.Application fullTextCheck(string filePath, Boolean isPathChanged, string keyWord, string oldKeyWord, Boolean isKeyWordChanged, Microsoft.Office.Interop.Word.Application word)
        {
            Boolean extraSituation = false;
            try
            {
                if (word.Documents.Count == 0)
                {
                    word = openDocument(filePath, word);
                    extraSituation = true;
                }
                else if (isPathChanged)//路径改变则关掉之前文档,打开新的文档
                {
                    word.Documents.Close(WdSaveOptions.wdDoNotSaveChanges, Type.Missing, Type.Missing);
                    word.Quit();
                    word = openDocument(filePath, word);
                }

            }
            catch
            {
                word = new Microsoft.Office.Interop.Word.Application();
                word = openDocument(filePath, word);
                extraSituation = true;
            }

            Selection currentSelect = word.Selection;
            if (isKeyWordChanged || isPathChanged || extraSituation)  //关键字改变的话需要还原文档
            {
                if (oldKeyWord != "")
                {
                    restoreDocument(oldKeyWord, currentSelect);
                }
                replace(keyWord, currentSelect);//替换关键字为高亮显示
            }
            return word;

        }
Exemplo n.º 3
0
        /// <summary>
        /// Kill process excel
        /// </summary>
        /// <param name="application"></param>        
        public static void KillProcessExcel(Microsoft.Office.Interop.Excel.Application application)
        {
            application.Quit();

            int hWnd = application.Application.Hwnd;
            uint processID;

            System.Runtime.InteropServices.Marshal.ReleaseComObject(application);

            GetWindowThreadProcessId((IntPtr)hWnd, out processID);
            Process[] procs = Process.GetProcessesByName("EXCEL");
            foreach (Process p in procs)
            {
                if (p.Id == processID)
                    p.Kill();
            }
        }
Exemplo n.º 4
0
 public bool KillAllExcel(Microsoft.Office.Interop.Excel.Application excelApp)
 {
     try
     {
         if (excelApp != null)
         {
             excelApp.Quit();
             System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
             //释放COM组件,其实就是将其引用计数减1     
             //System.Diagnostics.Process theProc;     
             foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
             {
                 //先关闭图形窗口。如果关闭失败.有的时候在状态里看不到图形窗口的excel了,     
                 //但是在进程里仍然有EXCEL.EXE的进程存在,那么就需要释放它     
                 if (theProc.CloseMainWindow() == false)
                 {
                     theProc.Kill();
                 }
             }
             excelApp = null;
             return true;
         }
     }
     catch
     {
         return false;
     }
     return true;
 }  
Exemplo n.º 5
0
 private static void DocumentPrintOut(Microsoft.Office.Interop.Word.Application oWordApplic, Microsoft.Office.Interop.Word.Document doc)
 {
     object append = Missing.Value;
     object background = true;
     object wdPrintAllDocument = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument;
     object copies = 1;
     object wdPrintAllPages = Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;
     object printToFile = false;
     object collate = false;
     object obj9 = append;
     object manualDuplexPrint = false;
     object printZoomColumn = 1;
     object printZoomRow = 1;
     doc.PrintOut(ref background, ref append, ref wdPrintAllDocument, ref append, ref append, ref append, ref append, ref copies, ref append, ref wdPrintAllPages, ref printToFile, ref collate, ref append, ref manualDuplexPrint, ref printZoomColumn, ref printZoomRow, ref append, ref append);
     object wdDoNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
     object wdOriginalDocumentFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdOriginalDocumentFormat;
     oWordApplic.Quit(ref wdDoNotSaveChanges, ref wdOriginalDocumentFormat, ref append);
     Marshal.ReleaseComObject(doc);
     Marshal.ReleaseComObject(oWordApplic);
     doc = null;
     oWordApplic = null;
 }
Exemplo n.º 6
0
 private void SaveDocument(Microsoft.Office.Interop.Word.Document wordDocument, Microsoft.Office.Interop.Word.ApplicationClass wordApp, string filePath)
 {
     object Visible = false;
     object missing = System.Reflection.Missing.Value;
     Object Nothing = System.Reflection.Missing.Value;
     object Save_FileName = filePath;
     //保存模板文件
     wordDocument.SaveAs(ref Save_FileName, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref Visible,
        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing);
     //关闭wordDoc文档对象
     wordDocument.Close(ref Nothing, ref Nothing, ref Nothing);
     wordDocument = null;
     //关闭wordApp组件对象
     wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
 }