开发工作中经常遇到跟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
}