ASP.NET导出Excel并将Excel进程关闭 (根据进程ID)

开发工作中经常遇到跟Excel打交道的地方,但在ASP.NET中使用完Excel后往往不能将其关闭,导致服务器上的Excel进程越来越多.本文介绍一个直接将进程KILL的方法,根据进程ID来KILL,不会影响其他用户的操作.
完整EXCEL导出及结束代码参考如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;

namespace SYJ.Web.Admin.ReportManager
{
    public partial class StatExport : System.Web.UI.Page
    {
        private static System.Reflection.Missing missValue = System.Reflection.Missing.Value;

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public static void DownLoad(System.Web.UI.Page p,string physicalPath,string downFilePre)
        {
            string pPath = physicalPath;
            byte[] bs = System.IO.File.ReadAllBytes(pPath);
            System.IO.File.Delete(pPath);
            p.Response.AddHeader("Content-Disposition", "attachment; filename="+downFilePre + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
            p.Response.OutputStream.Write(bs, 0, bs.Length);
            p.Response.End();
        }
        public static string Export(System.Data.DataTable dt)
        {
            string path = System.Web.HttpContext.Current.Server.MapPath("export");
            string filename = DateTime.Now.Ticks.ToString() + ".xls";
            string file = path + "\\" + filename;
            Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
            app.Visible = false;
            app.DisplayAlerts = false;
            //Excel.Workbooks wbs = app.Workbooks;
            //wbs.Open(file, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
            Microsoft.Office.Interop.Excel._Workbook wb = app.Workbooks.Add(missValue);
            //Excel.Sheets ss = wb.Worksheets;
            Microsoft.Office.Interop.Excel._Worksheet ws = (Microsoft.Office.Interop.Excel._Worksheet)wb.Worksheets.get_Item(1);
            //
            int iCols = dt.Columns.Count;
            int iRows = dt.Rows.Count;
            //
            for (int j = 0; j < iCols; j++)
            {
                ws.Cells[1, j + 1] = dt.Columns[j].ColumnName;
            }
            //
            for (int i = 0; i < iRows; i++)
            {
                for (int j = 0; j < iCols; j++)
                {
                    ws.Cells[i + 2, j + 1] = dt.Rows[i][j];
                }
            }
            //app.Save(file);//" + System.DateTime.Now.Ticks.ToString() + "
            wb.SaveAs(file, missValue, missValue, missValue, missValue, missValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missValue, missValue, missValue, missValue, missValue);
            wb.Close(missValue, missValue, missValue);

            killExcel(app);
            return file;
        }
        private static void killExcel(Microsoft.Office.Interop.Excel._Application excel)
        {
            try
            {
                Process[] ps = Process.GetProcesses();
                IntPtr t = new IntPtr(excel.Hwnd); //得到这个句柄,具体作用是得到这块内存入口
                int ExcelID = 0;
                GetWindowThreadProcessId(t, out ExcelID); //得到本进程唯一标志k
                foreach (Process p in ps)
                {
                    if (p.ProcessName.ToLower().Equals("excel"))
                    {
                        if (p.Id == ExcelID)
                        {
                            p.Kill();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show("ERROR " + ex.Message);
            }
        }
    }//C
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值