zoukankan      html  css  js  c++  java
  • Excel c#Excel工作进程的创建写 与Excel文件的保存[原创] (20100205 11:09)

    Excel工作进程的创建写结束

    创建:

         Microsoft.Office.Interop.Excel.ApplicationClass oExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
               Microsoft.Office.Interop.Excel.Workbook obook = null;
               Microsoft.Office.Interop.Excel.Worksheet oSheet = null;
               Microsoft.Office.Interop.Excel.Range range = null;

    结束:

    方法一:用托管方法

     System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
                   System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
                   System.Runtime.InteropServices.Marshal.ReleaseComObject(obook);

                   oExcel.Quit();
                   System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel);

                   GC.Collect();

    方法二:

     excelApp.Workbooks.Close();
                        oExcel.Quit();
                        int generation = System.GC.GetGeneration(oExcel);
                        System.GC.Collect(generation);

    方法三:

       /// <summary>
            /// 销毁进程
            /// </summary>
            /// <param name="datetime">销毁进程的时间</param>
            /// <returns></returns>
            private bool KillExcelProcess(DateTime datetime)
            {
                try
                {
                    System.Diagnostics.Process[] excelProc = System.Diagnostics.Process.GetProcessesByName("EXCEL");
                    for (int m = 0; m < excelProc.Length; m++)
                    {
                        if (datetime < excelProc[m].StartTime)
                        {
                            excelProc[m].Kill();
                        }

                    }
                    return true;
                }
                catch (Exception ex)
                {
                    strError = "销毁进程出错:" + ex.Message;
                    return false ;
                }
            }

     

    保存并替换原来单页Excel文件

     Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                    excelApp.DisplayAlerts = true;
                    excelApp.SheetsInNewWorkbook = 1;
                    Workbook excelBook = excelApp.Workbooks.Add(Type.Missing );
                    Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.ActiveSheet;

     excelBook.Saved = true;
                    excelBook.SaveCopyAs(strExcelFileName);

  • 相关阅读:
    HTML有2种路径的写法:绝对路径和相对路径
    ZB本地设置
    java main函数
    java static 关键字
    03013_动态页面技术-JSP
    Oracle数据库的文件以及Oracle体系架构
    记录一次mybatis缓存和事务传播行为导致ut挂的排查过程
    rtmp规范1.0全面指南
    程序员小哥教你秋招拿大厂offer
    ubuntu配置samba解决linux的svn使用舒适问题
  • 原文地址:https://www.cnblogs.com/heling/p/1664401.html
Copyright © 2011-2022 走看看