一、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
object missing = System.Reflection.Missing.Value;
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