• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

释放内存对象和杀死进程C#Excel

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
    public class ExcelHelper
    {
        #region 成员变量
        private string templetFile = null;
        private string outputFile = null;
        private object missing = Missing.Value;
        private DateTime beforeTime;            //Excel启动之前时间
        private DateTime afterTime;                //Excel启动之后时间
        Microsoft.Office.Interop.Excel.Application app;
        Microsoft.Office.Interop.Excel.Workbook workBook;
        Microsoft.Office.Interop.Excel.Worksheet workSheet;
        Microsoft.Office.Interop.Excel.Range range;
        Microsoft.Office.Interop.Excel.Range range1;
        Microsoft.Office.Interop.Excel.Range range2;
        Microsoft.Office.Interop.Excel.TextBox textBox;
        private int sheetCount = 1;            //WorkSheet数量
        private string sheetPrefixName = "页";
        #endregion

        #region 公共属性
        /// <summary>
        /// WorkSheet前缀名,比如:前缀名为“页”,那么WorkSheet名称依次为“页-1,页-2...”
        /// </summary>
        public string SheetPrefixName
        {
            set { this.sheetPrefixName = value; }
        }

        /// <summary>
        /// WorkSheet数量
        /// </summary>
        public int WorkSheetCount
        {
            get { return workBook.Sheets.Count; }
        }

        /// <summary>
        /// Microsoft.Office.Interop.Excel模板文件路径
        /// </summary>
        public string TempletFilePath
        {
            set { this.templetFile = value; }
        }

        /// <summary>
        /// 输出Excel文件路径
        /// </summary>
        public string OutputFilePath
        {
            set { this.outputFile = value; }
        }
        #endregion

        public static void MakeExcelDocs(string sourcepath, string destinpath, string newname)
        {
            destinpath = destinpath + "\\" + newname + ".xls";
            // Ensure that the target does not exist.
            File.Delete(destinpath);
            // Copy the file.
            File.Copy(sourcepath, destinpath);
            // Try to copy the same file again, which should succeed.
            File.Copy(sourcepath, destinpath, true);

        }

        public static void OpenExcelDocs(string filename,string[] content)
        {

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); //引用Excel对象
            Microsoft.Office.Interop.Excel.Workbook book = excel.Workbooks.Open(filename,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing
                ,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);   //引用Excel工作簿
            Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.Sheets.get_Item(1); ;  //引用Excel工作页面
            excel.Visible = false;
           

            string[] newcontent = StringHelper.Modify(content);

            sheet.Cells[10, 3] = newcontent[0];
            sheet.Cells[11, 3] = newcontent[1];
            sheet.Cells[12, 3] = newcontent[2];
            sheet.Cells[13, 3] = newcontent[3];
            sheet.Cells[14, 3] = newcontent[4];
            sheet.Cells[15, 3] = newcontent[5];
            sheet.Cells[16, 3] = newcontent[6];
            sheet.Cells[17, 3] = newcontent[7];
           

            book.Save();
            book.Close(Type.Missing, Type.Missing, Type.Missing);
            excel.Quit();

            IntPtr t = new IntPtr(excel.Hwnd);
            int k = 0;
            GetWindowThreadProcessId(t, out k);
            System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
            p.Kill();

            //sheet = null;
            //book = null;
            //excel = null

            //System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
        }

        [DllImport("User32.dll",   CharSet   =   CharSet.Auto)]    
        public static extern  int  GetWindowThreadProcessId(IntPtr   hwnd,   out   int   ID);     
        public static void OpenExcelDocs2(string filename, double[] content)
        {

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); //引用Excel对象
            Microsoft.Office.Interop.Excel.Workbook book = excel.Workbooks.Open(filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
                , Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);   //引用Excel工作簿
            Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.Sheets.get_Item(1); ;  //引用Excel工作页面
            excel.Visible = false;

            sheet.Cells[24, 3] = content[1];
            sheet.Cells[25, 3] = content[0];

            book.Save();
            book.Close(Type.Missing, Type.Missing, Type.Missing);
            excel.Quit();  //应用程序推出,但是进程还在运行

            IntPtr t = new IntPtr(excel.Hwnd);          //杀死进程的好方法,很有效
            int k = 0;
            GetWindowThreadProcessId(t, out k);
            System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
            p.Kill();

            //sheet = null;
            //book = null;
            //excel = null;   //不能杀死进程
 
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);  //可以释放对象,但是不能杀死进程
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

             
        }

    }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C/S架构和B/S架构发布时间:2022-07-13
下一篇:
tensorflowc++接口的编译安装与一些问题记录发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap