一、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”用戶),並賦予最大權限
二、結束Excel進程
1、我在上篇隨筆中用的是判斷進程啓動時間來結束Excel進程,雖然看起來有點不妥,但是我用了還從沒出過問題,從沒錯殺其他Excel進程
2、釋放所用到的所有Excel對象的資源,這裏拷貝一段代碼:
這段代碼來自:http://community.csdn.net/Expert/topic/3486/3486601.xml?temp=2.860659E-02
Microsoft.Office.Interop.Excel.Application myExcel=new Microsoft.Office.Interop.Excel.ApplicationClass();
myExcel.Visible= false;
//打開新文件
Microsoft.Office.Interop.Excel.Workbooks myBooks = myExcel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook myBook = myBooks.Open(sourceFile,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing, missing,missing,missing,missing);
Microsoft.Office.Interop.Excel.Worksheet curSheet = (Microsoft.Office.Interop.Excel.Worksheet)myBook.ActiveSheet;
Microsoft.Office.Interop.Excel.Range rans = (Microsoft.Office.Interop.Excel.Range)curSheet.Cells;
Microsoft.Office.Interop.Excel.Range ran = null;
Microsoft.Office.Interop.Excel.Range ranMerge = null;
Microsoft.Office.Interop.Excel.Range ranRows = null;
Microsoft.Office.Interop.Excel.Range ranCells = null;
for( int i=0; i < 10; i++ )
{
for( int j=0; j < 10; j++ )
{
ran = (Microsoft.Office.Interop.Excel.Range)rans[i+1,j+1];
ranMerge= ran.MergeArea;
ranRows= ranMerge.Rows;
int mergeRows= ranRows.Count;
ranCells= ranMerge.Cells;
int mergeCells= 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