zoukankan      html  css  js  c++  java
  • 关于在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 + ":" ++ "   : " + 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控件给大家下载:
    https://files.cnblogs.com/lingyun_k/ExcelWriter.rar

    这个有破解
    https://files.cnblogs.com/lingyun_k/Aspose%20Excel%20V2.3.1.1.NET.rar

    还有一个是ExcelQuicker,功能也挺强的,大家搜一下就可以找到,不过我觉得金质打印王的对Excel操作比它要方便,但是不支持WebForm

  • 相关阅读:
    JSP学习-10-EL表达式
    深入浅出Mybatis(一)
    第10章—开启事务
    第09章—使用Lombok插件
    第08章—整合Spring Data JPA
    第06章—热部署
    第05章—Swagger2打造在线接口文档
    第03章—打造RESTful风格API
    第04章—整合Mybatis
    第01章—快速构建
  • 原文地址:https://www.cnblogs.com/lingyun_k/p/205097.html
Copyright © 2011-2022 走看看