在ASP.NET中使用EXCEL

本文介绍如何在ASP.NET中设置COM组件权限以操作Excel文件,包括读取和写入Excel的具体步骤及示例代码。

在ASP.NET中使用EXCEL,首先需要对COM组件的权限进行设置。如果未设置权限,则会报访问拒绝的错误。详细错误信息通常如下:

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.UnauthorizedAccessException: 拒绝访问。

ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限。ASP.NET 有一个在应用程序没有模拟时使用的基进程标识(通常,在 IIS 5 上为 {MACHINE}/ASPNET,在 IIS 6 上为网络服务)。如果应用程序正在通过 <identity impersonate="true"/> 模拟,则标识将为匿名用户(通常为 IUSR_MACHINENAME)或经过身份验证的请求用户。

若要授予 ASP.NET 对文件的写访问权,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 帐户,选中所需访问权限对应的框。

设置权限的方法是在Windows的运行框中输入dcomcnfg,打开Com管理。在EXCEL应用程序的安全中,分别添加ASPNET、IUSER、IWAM等用户的访问、运行和配置权限。

设置权限之后,就可以使用ASP.NET读取Excel文件了。
首先创建一个aspx文件,在页面中加入一个Button和一个DataGrid控件。
在工程的引用中添加Excel引用,并将测试的电子表格文件放到D盘中。
在CS文件中的Button1_Click事件中输入如下代码:
 private void Button1_Click(object sender, System.EventArgs e)
  {
   string excelFilePath=@"D:/Book1.xls";
   Excel.Application myExcel=new Excel.ApplicationClass( ) ;
   object oMissing = System.Reflection.Missing.Value ;
   myExcel.Application.Workbooks.Open(excelFilePath,oMissing,oMissing,oMissing,oMissing,oMissing,    oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing) ;
   Excel.Workbook myBook = myExcel.Workbooks[1] ;
   Excel.Worksheet mySheet = (Excel.Worksheet)myBook.Worksheets[1] ;
   System.Data.DataTable dt=new System.Data.DataTable("mytable");
   dt.Columns.Add("F1", System.Type.GetType("System.String"));
   dt.Columns.Add("F2", System.Type.GetType("System.String"));
   dt.Columns.Add("F3", System.Type.GetType("System.String"));
   dt.Columns.Add("F4", System.Type.GetType("System.String"));
   dt.Columns.Add("F5", System.Type.GetType("System.String"));
   DataSet myDs = new DataSet();
   myDs.Tables.Add(dt);
   DataRow myRow;
   myDs.Clear();
   for( int i = 2 ; i <= 4 ; i ++ ) //第一行为标题,不读取
   {
    myRow = myDs.Tables["mytable"].NewRow();
    for( int j = 1 ; j <= 5 ; j ++ )
    {
     Excel.Range r=(Excel.Range)mySheet.Cells[i,j];
     string strValue=r.Text.ToString();
     string aa=strValue;
     string columnname="F"+j.ToString();
     myRow[columnname]=strValue;
    }
    myDs.Tables["mytable"].Rows.Add(myRow);
   } 
   DataGrid1.DataSource=myDs.Tables["mytable"].DefaultView;
   DataGrid1.DataBind();
  }

在Web窗体中加入一个Button控件,在它的click事件中输入如下代码,即可以实现写入Excel文件。

写Excel文件时,还要把项目文件夹的权限进行设置,对iuser_machine用户有可写的权限。

private void Button1_Click(object sender, System.EventArgs e)
  {string filename="";
   Excel.ApplicationClass oExcel;
   oExcel = new Excel.ApplicationClass();
   oExcel.UserControl = false;
   Excel.WorkbookClass wb = (Excel.WorkbookClass) oExcel.Workbooks.Add(System.Reflection.Missing.Value);
   for(int i = 1;i <= 5; i++)
   {
    oExcel.Cells[i,1]=i.ToString();
    oExcel.Cells[i,2]="'第2列";
    oExcel.Cells[i,3]="'第3列";
    oExcel.Cells[i,4]="'第4列";
   }  
   wb.Saved = true;
   filename= Request.PhysicalApplicationPath +  "test.xls";
   oExcel.ActiveWorkbook.SaveCopyAs(filename);
   oExcel.Quit();
   System.GC.Collect();
   Response.Redirect( Request.ApplicationPath +  "/test.xls");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值