Exemplo n.º 1
0
 // closes Excel
 public static void CloseExcel(Excel.Application xlApp, Excel.Workbook wb)
 {
     // Close Excel
     object misValue = System.Reflection.Missing.Value;
     wb.Close(false, misValue, misValue);
     xlApp.Quit();
 }
Exemplo n.º 2
0
Arquivo: Core.cs Projeto: guigui43/TDV
        private void QuitExcel()
        {
            //WriteLog("Quit and clean up");

            if (Book != null)
            {
                Book.Close(false, _missing, _missing);
                Marshal.ReleaseComObject(Book);
                Book = null;
            }

            if (Books != null)
            {
                Marshal.ReleaseComObject(Books);
                Books = null;
            }

            if (Excel != null)
            {
                Excel.Quit();
                Marshal.ReleaseComObject(Excel);
                Excel = null;
            }

            //Garbage collection
            GC.Collect();
        }
Exemplo n.º 3
0
        public int Run()
        {
            bool result = InitPara();

            if (!result)
            {
                return(-999);
            }

            int failCount = 0;

            result = FailCount(ref failCount);
            if (!result)
            {
                Wb.Close(false, Type.Missing, Type.Missing);
                Excel.Quit();
                return(-998);
            }

            Wb.Close(false, Type.Missing, Type.Missing);
            Excel.Quit();

            if (failCount < 0)
            {
                failCount = failCount * -1;
            }

            return(0);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 释放内存
        /// </summary>
        public void Dispose(Excel._Worksheet CurSheet, Excel._Workbook CurBook, 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)
            {
                // 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.º 5
0
        protected void ReleaseExcel(ref MSOffice.Application excelApp, ref MSOffice.Workbook excelBook, ref MSOffice.Worksheet excelSheet)
        {
            try
            {
                excelBook.Close(false, null, null);
                excelApp.Quit();
                GC.Collect();

                IntPtr ptr = new IntPtr(excelApp.Hwnd);
                int pid = 0;
                GetWindowThreadProcessId(ptr, out pid);
                System.Diagnostics.Process proc = System.Diagnostics.Process.GetProcessById(pid);

                System.Runtime.InteropServices.Marshal.ReleaseComObject((object)excelApp);
                System.Runtime.InteropServices.Marshal.ReleaseComObject((object)excelBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject((object)excelSheet);
                excelApp = null;
                excelBook = null;
                excelSheet = null;

                //最后尝试结束进程,出错表示已销毁
                try
                { proc.Kill(); }
                catch (Exception) { }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 private static void CloseAndReleaseExcelObjects(Excel.Application xlApp, Excel.Workbook xlworkBook, Excel.Worksheet xlWorksheet)
 {
     xlworkBook.Close(false, null, null);
     xlApp.Quit();
     ReleaseObject(xlWorksheet);
     ReleaseObject(xlworkBook);
     ReleaseObject(xlApp);
 }
Exemplo n.º 7
0
        /// <summary>
        /// 저장및 메모리 해제
        /// </summary>
        /// <param name="excelApp"></param>
        /// <param name="wb"></param>
        /// <param name="workSheet"></param>
        public static void ExcelDispose(Excel.Application excelApp, Excel.Workbook wb, Excel._Worksheet workSheet)
        {
            wb.SaveAs(@"C:\users\user\Desktop\수강신청시간표", Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            wb.Close(Type.Missing, Type.Missing, Type.Missing);
            excelApp.Quit();
            releaseObject(excelApp);
            releaseObject(workSheet);
            releaseObject(wb);
        }
Exemplo n.º 8
0
        private bool SaveReport()
        {
            Console.WriteLine("Save Report...");
            try
            {
                bool   allPass      = IsAllPass();
                string resultStr    = allPass ? "(Pass)" : "(Fail)";
                string savePathFile = ExcelFileDirectory + @"\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + resultStr + ".xlsx";
                Wb.SaveAs(savePathFile);

                Wb.Close(false, Type.Missing, Type.Missing);
                Excel.Quit();

                MoveReportToPreFolder(savePathFile);
            }
            catch (Exception ex)
            {
                Console.WriteLine("儲存檔案出錯,檔案可能正在使用" + Environment.NewLine + ex.Message);
                return(false);
            }

            return(true);
        }
Exemplo n.º 9
0
 private void release_excel_app(ref MSExcel.Application excelApp)
 {
     excelApp.Workbooks[1].Close();
     excelApp.Quit();
     if (excelApp != null)
     {
         int lpdwProcessID;
         GetWindowThreadProcessId(new IntPtr(excelApp.Hwnd), out lpdwProcessID);
         System.Diagnostics.Process.GetProcessById(lpdwProcessID).Kill();
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// Close Excel workbook, quit excel and wait until finished
        /// </summary>
        /// <param name="app">Excel handle to quit</param>
        /// <param name="wb">Excel workbook to close</param>
        private static void ExcelQuit(Excel.Application app, Excel.Workbook wb)
        {
            wb.Close(SaveChanges: false);
            app.Quit();
            while (Marshal.ReleaseComObject(app) != 0)
            {
            }

            app = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
Exemplo n.º 11
0
        private void SaveExcel(string file, MSExcel.Application excel, MSExcel.Workbook wbook)
        {
            try
            {
                File.Delete(file);
            }
            catch //(Exception ex)
            {
                wbook.Close(false);

                excel.Quit();

                return;
            }

            try
            {
                wbook.SaveAs(file, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, MSExcel.XlSaveAsAccessMode.xlExclusive);
            }
            catch//(Exception ex)
            {

            }

            wbook.Close(false);

            excel.Quit();
        }
Exemplo n.º 12
0
        public static string ConvertToXlsx(string filePath, Excel.Application excel = null)
        {
            excel = excel ?? CreateExcel();

            var workbook = excel.Workbooks.Open(filePath, UpdateLinks: false, ReadOnly: true);
        	
        	var path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
     
        	workbook.SaveAs(path, FileFormat: Excel.XlFileFormat.xlOpenXMLWorkbook, 
        	                AccessMode: Excel.XlSaveAsAccessMode.xlNoChange);
        	
        	workbook.Close(false);
        	excel.Quit();
        	
        	return path;
        }