关于在ASP.NET中以DCOM方式操作Excel的几个问题

一、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;
forint i=0; i < 10; i++ )
{
forint 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值