ASP.NET調(diào)用Excel不能結(jié)束進(jìn)程的解決方法

2010-08-28 10:48:52來源:西部e網(wǎng)作者:

畢設(shè)是碰到這個(gè)問題,一直是手動(dòng)關(guān)閉Excel進(jìn)程-_-!今天碰巧看到,欣喜轉(zhuǎn)之:

-------------

關(guān)于在asp.net中調(diào)用Excel組件不能結(jié)束進(jìn)程的問題,常見的解決方法用的是下面這段代碼

wb.Close(null,null,null);
app.Workbooks.Close();
app.Quit();

if(rng != null)
{
    System.Runtime.InteropServices.Marshal.ReleaseComObject(rng);
    rng 
= null;
}

if(ws != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
    ws 
= null;
}

if(wb != null)
{  System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
    wb 
= null;
}

if(app != null)
{ System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app 
= null;
}

GC.Collect();

        雖然這段代碼在配置正確的情況下能自動(dòng)結(jié)束Excel進(jìn)程,但是前提是在操作Excel時(shí)沒有引發(fā)異常的情況下,如果有異常發(fā)生,那么Excel進(jìn)程將不能結(jié)束(比如:引用了一個(gè)在Excel文件中不存在的文本框時(shí)就會(huì)出現(xiàn)“HRESULT 中的異常:0x800A03EC。”),這時(shí)就要借助Process類的Kill()方法來結(jié)束,下面是我寫的測(cè)試代碼:

using System;
using System.Diagnostics;
using excel = Microsoft.Office.Interop.Excel;

namespace ExcelTest
{
    
/// <summary>
    
/// Excel的摘要說明。
    
/// </summary>

    public class Excel
    
{
        
private DateTime beforeTime;            //Excel啟動(dòng)之前時(shí)間
        private DateTime afterTime;                //Excel啟動(dòng)之后時(shí)間

        excel.Application app;
        excel.Workbook wb;
        excel.Worksheet ws;
        excel.Range rng;
        excel.TextBox tb;

        
public Excel(string templetPath)
        
{
            
//實(shí)例化一個(gè)Excel Application對(duì)象并使其可見
            beforeTime = DateTime.Now;
            app 
= new excel.ApplicationClass();
            app.Visible 
= true;
            afterTime 
= DateTime.Now;

            wb 
= app.Workbooks.Open(templetPath,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);
            ws 
= (excel.Worksheet)wb.Worksheets.get_Item(1);
        }


        
public void ExcelMethod()
        
{
            rng 
= ws.get_Range("B5","C7");
            rng.Merge(excel.XlAxisCrosses.xlAxisCrossesAutomatic);
            rng.Value2 
= "Excel2003";

            rng 
= ws.get_Range("D8","E11");
            rng.MergeCells 
= true;
            rng.Value2 
= "Excel2003";
            rng.HorizontalAlignment 
= excel.XlHAlign.xlHAlignCenter;
            rng.VerticalAlignment 
= excel.XlVAlign.xlVAlignCenter;

            rng 
= ws.get_Range("A1",Type.Missing);
            rng.Value2 
= 5;

            rng 
= ws.get_Range("A2",Type.Missing);
            rng.Value2 
= 7;

            
for(int i=1;i<100;i++)
            
{
                
string s = string.Concat("G",i.ToString());
                rng 
= ws.get_Range(s,Type.Missing);
                rng.Value2 
= i.ToString();
            }


            tb 
= (excel.TextBox)ws.TextBoxes("文本框 1");
            tb.Text 
= "作 者";

            tb 
= (excel.TextBox)ws.TextBoxes("文本框 2");
            tb.Text 
= "KLY.NET的Blog";

            tb 
= (excel.TextBox)ws.TextBoxes("文本框 3");
            tb.Text 
= "日 期";


            
try
            
{
                tb 
= (excel.TextBox)ws.TextBoxes("文本框 5");
                tb.Text 
= DateTime.Now.ToShortDateString();
            }

            
catch
            
{
                
//這里用Dispose()方法結(jié)束不了Excel進(jìn)程,所有還是要用Process的Kill()方法配合使用
                
//                this.Dispose();
                this.KillExcelProcess();
                
throw new Exception("不存在ID為\"文本框 5\"的文本框!");
            }

            
finally
            
{
                
//如果有異常發(fā)生,Dispose()方法放在這里也結(jié)束不了Excel進(jìn)程
//                this.Dispose();

                
//如果發(fā)生異常,在這里也可以結(jié)束Excel進(jìn)程
//                this.KillExcelProcess();
            }

        }


        
/// <summary>
        
/// 另存為Excel文件
        
/// </summary>
        
/// <param name="savePath">保存路徑</param>

        public void SaveAsExcelFile(string savePath)
        
{
            wb.SaveAs(savePath,excel.XlFileFormat.xlHtml,Type.Missing,Type.Missing,Type.Missing,Type.Missing,excel.XlSaveAsAccessMode.xlExclusive,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
        }


        
/// <summary>
        
/// 結(jié)束Excel進(jìn)程
        
/// </summary>

        public void KillExcelProcess()
        
{
            Process[] myProcesses;
            DateTime startTime;
            myProcesses 
= Process.GetProcessesByName("Excel");

            
//得不到Excel進(jìn)程ID,暫時(shí)只能判斷進(jìn)程啟動(dòng)時(shí)間
            foreach(Process myProcess in myProcesses)
            
{
                startTime 
= myProcess.StartTime;

                
if(startTime > beforeTime && startTime < afterTime)
                
{
                    myProcess.Kill();
                }

            }

        }


        
/// <summary>
        
/// 如果對(duì)Excel的操作沒有引發(fā)異常的話,用這個(gè)方法可以正常結(jié)束Excel進(jìn)程
        
/// 否則要用KillExcelProcess()方法來結(jié)束Excel進(jìn)程
        
/// </summary>

        public void Dispose()
        
{
            wb.Close(
null,null,null);
            app.Workbooks.Close();
            app.Quit();

            
//注意:這里用到的所有Excel對(duì)象都要執(zhí)行這個(gè)操作,否則結(jié)束不了Excel進(jìn)程
            if(rng != null)
            
{
                System.Runtime.InteropServices.Marshal.ReleaseComObject(rng);
                rng 
= null;
            }

            
if(tb != null)
            
{
                System.Runtime.InteropServices.Marshal.ReleaseComObject(tb);
                tb 
= null;
            }

            
if(ws != null)
            
{
                System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
                ws 
= null;
            }

            
if(wb != null)
            
{
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
                wb 
= null;
            }

            
if(app != null)
            
{
                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
                app 
= null;
            }


            GC.Collect();
        }

    }

}

        這段代碼能很好的解決Excel進(jìn)程不能正常結(jié)束的問題,如果主機(jī)操作系統(tǒng)不是服務(wù)器版的話,那么就要借助于ntsd -c q -p pid命令來結(jié)束。
        還有一個(gè)問題的關(guān)于Excel組件訪問權(quán)限的配置,一定要在組件服務(wù)里面正確配置,否則結(jié)束不了Excel進(jìn)程,具體的配置方法在我項(xiàng)目的doc文件夾下;在我前面的文章里面介紹了在web.config文件里面加入假扮用戶的方法,但是經(jīng)我測(cè)試發(fā)現(xiàn)這種方法雖然可以訪問Excel組件,但是結(jié)束不了進(jìn)程,除非用Kill方法強(qiáng)行結(jié)束。

點(diǎn)這里下載ExcelTest.rar

原文:http://www.cnblogs.com/lingyun_k/archive/2005/11/08/271796.html

關(guān)鍵詞:ASP.NET

贊助商鏈接: