一、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;
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
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
++
)
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
for( int j=0; j < 10; j++ )
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
{
ran = (Microsoft.Office.Interop.Excel.Range)rans[i+1,j+1];
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ranMerge= ran.MergeArea;
ranRows= ranMerge.Rows;
int mergeRows= ranRows.Count;
ranCells= ranMerge.Cells;
int mergeCells= ranCells.Count;
Response.Write( "<br/>" + i + ":" +j + " : " + ran.Text );
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
System.Runtime.InteropServices.Marshal.ReleaseComObject (ranCells);
ranCells = null;
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
System.Runtime.InteropServices.Marshal.ReleaseComObject (ranRows);
ranRows = null;
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
System.Runtime.InteropServices.Marshal.ReleaseComObject (ranMerge);
ranMerge = null;
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
System.Runtime.InteropServices.Marshal.ReleaseComObject (ran);
ran = null;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
System.Runtime.InteropServices.Marshal.ReleaseComObject (rans);
rans
=
null
;
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
System.Runtime.InteropServices.Marshal.ReleaseComObject (curSheet);
curSheet
=
null
;
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
myBook.Close(
false
,Type.Missing,Type.Missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject (myBook);
myBook
=
null
;
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
myBooks.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject (myBooks);
myBooks
=
null
;
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject (myExcel);
myExcel
=
null
;
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
GC.Collect();
GC.WaitForPendingFinalizers();
暂时总结这两个问题,这些解决办法都来源于网上,我这里只是总结一下,顺便把我收集的几个Excel控件给大家下载:
http://www.cnblogs.com/Files/lingyun_k/ExcelWriter.rar
这个有破解
http://www.cnblogs.com/Files/lingyun_k/Aspose%20Excel%20V2.3.1.1.NET.rar
还有一个是ExcelQuicker,功能也挺强的,大家搜一下就可以找到,不过我觉得金质打印王的对Excel操作比它要方便,但是不支持WebForm