在将c#.net中的数据导入到Excel中时出错
每次运行时提示:
“/datagrid”应用程序中的服务器错误。
--------------------------------------------------------------------------------
拒绝访问。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.UnauthorizedAccessException: 拒绝访问。
ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限。ASP.NET 有一个在应用程序没有模拟时使用的基进程标识(通常,在 IIS 5 上为 {MACHINE}\ASPNET,在 IIS 6 上为网络服务)。如果应用程序正在通过 <identity impersonate="true"/> 模拟,则标识将为匿名用户(通常为 IUSR_MACHINENAME)或经过身份验证的请求用户。
若要授予 ASP.NET 对文件的写访问权,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 帐户,选中所需访问权限对应的框。
源错误:
行 56: DataSet ds=GetData();
行 57: //下面的代码存在问题,可能是没有包含必要的命名空间
行 58: Excel.Application exce = new Excel.Application();//
行 59: //exce= new Excel.ApplicationClass();
行 60: int rowindex=1;
源文件: c:\inetpub\wwwroot\datagrid\excel2.aspx.cs 行: 58
堆栈跟踪:
[UnauthorizedAccessException: 拒绝访问。]
datagrid.excel2.CreateExcelTable() in c:\inetpub\wwwroot\datagrid\excel2.aspx.cs:58
datagrid.excel2.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\datagrid\excel2.aspx.cs:37
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
大虾们帮忙啊
源代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;
using Excel;
using System.Reflection;
namespace datagrid
{
public class excel2 : System.Web.UI.Page
{
//protected System.Data.SqlClient.SqlDataAdapter sda;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
private readonly string connection=ConfigurationSettings.AppSettings["connection"].ToString();
private Excel.Application exce;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
CreateExcelTable();
}
}
private DataSet GetData()
{ string cmd="select * from area";
SqlConnection conn=new SqlConnection();
conn.ConnectionString=connection;
SqlDataAdapter sda=new SqlDataAdapter(cmd,conn);
conn.Open();
DataSet ds=new DataSet();
sda.Fill(ds);
conn.Close();
return(ds);
}
private void CreateExcelTable()
{
DataSet ds=GetData();
exce= new Excel.ApplicationClass();
//Excel.Application exce = new Excel.Application();
int rowindex=1;
int column=0;
exce.Application.Workbooks.Add(true);
System.Data.DataTable table=ds.Tables[0];
foreach(DataColumn col in table.Columns)
{
column++;
exce.Cells[1,column]=col.ColumnName;
}
foreach(DataRow row in table.Rows)
{
rowindex++;
column++;
foreach(DataColumn col in table.Columns)
{
column++;
exce.Cells[rowindex,column]=row[col.ColumnName].ToString();
}
}
exce.Visible=false;
exce.DisplayAlerts=false;
exce.Save(MapPath("ExcelDB/ExcelTable.xls"));
exce.Application.Workbooks.Close();
exce.Application.Quit();
exce.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(exce);
GC.Collect();
}
}
}