關於在ASP.NET中以DCOM方式操作Excel的幾個問題

來源:互聯網
上載者:User

一、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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.