ASP.NET結束Excel進程
一、操作權限問題,有兩種方法:
1、使用模擬帳戶,在Web.config文件中加入
- <!identity impersonate="true" userName="administrator" password=""/>
2、在DCOM組件服務中給MICROSOFT.EXCEL組件 賦予ASP.NET的操作權限,具體步驟:
(1)打開開始菜單的運行對話框,輸入dcomcnfg命令,確定,這時會彈出組件服務窗口
(2)展開計算機-〉我的電腦-〉DCOM配置,找到Microsoft Excel應用程序節點
(3)單擊右鍵-〉屬性,選中“安全”選項,在下面三個項目都選擇“自定義”,并單擊編輯按鈕
(4)在啟動權限對話框中點擊添加按鈕,添加相應的用戶(注意:如果是WIN2000,XP,則添加“機器名/ASPNET”用戶,我這里是以WIN2003為例,WIN2003是添加“NETWORK Service”用戶),并賦予***權限
二、ASP.NET結束Excel進程
1、我在上篇隨筆中用的是判斷進程啟動時間來ASP.NET結束Excel進程,雖然看起來有點不妥,但是我用了還從沒出過問題,從沒錯殺其他Excel進程。
2、釋放所用到的所有Excel對象的資源,這里拷貝一段代碼:
- objectmissing=System.Reflection.Missing.Value;
- Microsoft.Office.Interop.Excel.ApplicationmyExcel=newMicrosoft.Office.
Interop.Excel.ApplicationClass();- myExcel.Visible=false;
- //打開新文件
- Microsoft.Office.Interop.Excel.WorkbooksmyBooks=myExcel.Workbooks;
- Microsoft.Office.Interop.Excel.WorkbookmyBook=myBooks.
Open(sourceFile,missing,...);- Microsoft.Office.Interop.Excel.WorksheetcurSheet=
(Microsoft.Office.Interop.Excel.Worksheet)myBook.ActiveSheet;- Microsoft.Office.Interop.Excel.Rangerans=
(Microsoft.Office.Interop.Excel.Range)curSheet.Cells;- Microsoft.Office.Interop.Excel.Rangeran=null;
- Microsoft.Office.Interop.Excel.RangeranMerge=null;
- Microsoft.Office.Interop.Excel.RangeranRows=null;
- Microsoft.Office.Interop.Excel.RangeranCells=null;
- for(inti=0;i<10;i++)
- {
- for(intj=0;j<10;j++)
- {
- ran=(Microsoft.Office.Interop.Excel.Range)rans[i+1,j+1];
- ranranMerge=ran.MergeArea;
- ranRows=ranMerge.Rows;
- intmergeRows=ranRows.Count;
- ranCells=ranMerge.Cells;
- intmergeCells=ranCells.Count;
- Response.Write("<br/>"+i+":"+j+":"+ran.Text);
- System.Runtime.InteropServices.Marshal.ReleaseComObject(ranCells);
- ranCells=null;
- System.Runtime.InteropServices.Marshal.ReleaseComObject(ranRows);
- ranRows=null;
- System.Runtime.InteropServices.Marshal.ReleaseComObject(ranMerge);
- ranMerge=null;
- System.Runtime.InteropServices.Marshal.ReleaseComObject(ran);
- ran=null;
- }
- }
- System.Runtime.InteropServices.Marshal.ReleaseComObject(rans);
- rans=null;
- System.Runtime.InteropServices.Marshal.ReleaseComObject(curSheet);
- curSheet=null;
- myBook.Close(false,Type.Missing,Type.Missing);
- System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
- myBook=null;
- myBooks.Close();
- System.Runtime.InteropServices.Marshal.ReleaseComObject(myBooks);
- myBooks=null;
- myExcel.Quit();
- System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
- myExcel=null;
- GC.Collect();
- GC.WaitForPendingFinalizers();
暫時總結這兩個問題,這些解決辦法都來源于網上,我這里只是總結一下,順便把我收集的幾個Excel控件給大家下載:
http://files.cnblogs.com/lingyun_k/ExcelWriter.rar
這個有破解
http://files.cnblogs.com/lingyun_k/Aspose%20Excel%20V2.3.1.1.NET.rar
還有一個是ExcelQuicker,功能也挺強的,大家搜一下就可以找到,不過我覺得金質打印王的對Excel操作比它要方便,但是不支持WebForm,以上介紹ASP.NET結束Excel進程
【編輯推薦】