在这里不讨论那种声称html格式的数据然后更改扩展名的方式。
您给出的办法一定要在发布到IIS的站点上测试过在发上来。谢谢
另外:不管是讨论杀进程的方式还是其他方式,一定要发布到测试站点上进行,不能在VS项目中直接运行Web(关于DCOM组件权限的问题已经解决了),因为在VS项目中直接运行web程序和IIS方式的测试站点来运行对组件的权限解释方式不一样(具体的不清楚,但是肯定有很大的区别,因为发布到测试站点上取进程的Process.StartTime会抛出异常
Excel是通过Microsoft.Office.Interop.Excel.ApplicationClass方式new出来的。
简单说,下面的代码不起作用 finally
{
((MicroExcel._Application)tempApp).Workbooks.Close();
Int32 tempGeneration = GC.GetGeneration(tempApp);
((MicroExcel._Application)tempApp).Quit();
GC.Collect(tempGeneration);
//上面部分在Winform中就已经干掉EXCEL进程了,但是在asp.net中还不行
Process[] myProcesses;
myProcesses = Process.GetProcessesByName("Excel"); //得不到Excel进程ID,暂时只能判断进程启动时间
foreach (Process myProcess in myProcesses)
{
try
{
DateTime startTime = myProcess.StartTime; if (startTime >= tempStart && startTime <= tempEnd)
{
myProcess.Kill();
}
}//直接在VS中运行网页就不会有取不到StartTime的问题
catch//有一些核心进程是不允许访问的,因此而引起的异常可以捕获并放弃处理
{ }
}
}
您给出的办法一定要在发布到IIS的站点上测试过在发上来。谢谢
另外:不管是讨论杀进程的方式还是其他方式,一定要发布到测试站点上进行,不能在VS项目中直接运行Web(关于DCOM组件权限的问题已经解决了),因为在VS项目中直接运行web程序和IIS方式的测试站点来运行对组件的权限解释方式不一样(具体的不清楚,但是肯定有很大的区别,因为发布到测试站点上取进程的Process.StartTime会抛出异常
Excel是通过Microsoft.Office.Interop.Excel.ApplicationClass方式new出来的。
简单说,下面的代码不起作用 finally
{
((MicroExcel._Application)tempApp).Workbooks.Close();
Int32 tempGeneration = GC.GetGeneration(tempApp);
((MicroExcel._Application)tempApp).Quit();
GC.Collect(tempGeneration);
//上面部分在Winform中就已经干掉EXCEL进程了,但是在asp.net中还不行
Process[] myProcesses;
myProcesses = Process.GetProcessesByName("Excel"); //得不到Excel进程ID,暂时只能判断进程启动时间
foreach (Process myProcess in myProcesses)
{
try
{
DateTime startTime = myProcess.StartTime; if (startTime >= tempStart && startTime <= tempEnd)
{
myProcess.Kill();
}
}//直接在VS中运行网页就不会有取不到StartTime的问题
catch//有一些核心进程是不允许访问的,因此而引起的异常可以捕获并放弃处理
{ }
}
}
程序开始时执行次
GC.Collect();
ApplicationClass excel;
_Workbook xBk;
_Worksheet xSt;
excel = new ApplicationClass();
xBk = excel.Workbooks.Add(true);
xSt = (_Worksheet)xBk.ActiveSheet;
//自定义代码...
//结束时
xBk.Close(false, null, null);
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
接上在加上GC.Collect();
程序开始时执行次
GC.Collect();
ApplicationClass excel;
_Workbook xBk;
_Worksheet xSt;
excel = new ApplicationClass();
xBk = excel.Workbooks.Add(true);
xSt = (_Worksheet)xBk.ActiveSheet;
//自定义代码...
//结束时
xBk.Close(false, null, null);
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();怎么可能,我做了半年多excel报表操作,从没有杀不掉的excel进程
string sTimes = DateTime.Now.ToString("yyyyMMddHHmmss");
string strFileName = "";
System.IO.FileInfo file;
strFileName = Server.MapPath("Uploads/qafiles/") + sTimes + ".xls";
xBk.SaveCopyAs(strFileName);
xBk.Close(false, null, null);
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
//听说你是***中最好的一种,可为什么如此善良的你,却无情的闯了进来
GC.Collect();
file = new System.IO.FileInfo(strFileName);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/ms-excel";
Response.WriteFile(file.FullName);
Response.End();
xlApp.Workbooks.Close();
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
wb = null;
ws = null;
System.Diagnostics.Process myproc= new System.Diagnostics.Process();
foreach (Process thisproc in Process.GetProcessesByName(processName)) {
if(!thisproc.CloseMainWindow()){
thisproc.Kill();
}}
到目前为止,在我的源代码上,这个问题还没解决(注意新帖中写的测试环境,一定要发布到IIS上面再验证)
http://topic.csdn.net/u/20100728/16/1d4b7067-77b7-4c03-bfe3-1a6086fc5582.html
对于Office 2007以及之后的版本来讲,推荐的方法就是Open XML的方式,参考:http://support.microsoft.com/kb/931866