各位高手,请问?asp.net资料汇出excel发布后报错如下:
异常详细信息: System.UnauthorizedAccessException: 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。 ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限。ASP.NET 有一个在应用程序没有模拟时使用的基进程标识(通常,在 IIS 5 上为 {MACHINE}\ASPNET,在 IIS 6 上为网络服务)。如果应用程序正在通过 <identity impersonate="true"/> 模拟,则标识将为匿名用户(通常为 IUSR_MACHINENAME)或经过身份验证的请求用户。 要将 ASP.NET 访问权限授予某个文件,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 账户,选中所需访问权限对应的框。cs原码
protected void btnremit_Click(object sender, EventArgs e)
{
//资料汇出
deptid = Session["deptid"].ToString().Trim();
database cn = new database("MARKMANAGEDBConnectionString");
comm cm = new comm();
DataSet ds = new DataSet();
cn.OpenConn();
string str = "select userid,itemno,sum(number) as number from deptmove where fromno='" + txtcce.Text.ToString().Trim() + "' and deptid='" + deptid + "' group by userid,itemno";
cn.RunProc(str, ds);
cn.Dispose(cn.OpenConn());
if (ds.Tables[0].Rows.Count > 0)
{
int ico = 3;
GC.Collect();
Microsoft.Office.Interop.Excel._Application excel;
Microsoft.Office.Interop.Excel._Workbook xbk;
Microsoft.Office.Interop.Excel._Worksheet xst;
excel = new ApplicationClass();
xbk = excel.Workbooks.Add(true);
xst = (_Worksheet)xbk.ActiveSheet;
excel.Cells[1, ico / 2] = txtdept.Text + "移交凭单";
excel.Cells[2, 1] = "凭单编号";
excel.Cells[2, 2] = txtcce.Text.ToString().Trim();
excel.Cells[3, 1] = "日期";
excel.Cells[3, 2] = txtdate.Text.ToString().Trim();
excel.Cells[4, 1] = "时间";
excel.Cells[4, 2] = txttime.Text.ToString().Trim();
excel.Cells[5, 1] = "工号";
excel.Cells[5, 2] = "品号";
excel.Cells[5, 3] = "数量";
try
{
int i = 0;
for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
excel.Cells[i + 6, 1] = ds.Tables[0].Rows[i][0].ToString().Trim();
excel.Cells[i + 6, 2] = ds.Tables[0].Rows[i][1].ToString().Trim();
excel.Cells[i + 6, 3] = ds.Tables[0].Rows[i][2].ToString().Trim();
}
excel.Cells[i + 7, 1] = "移交人签名";
excel.Cells[i + 8, 1] = "承接人签名"; }
catch
{
excel.Visible = true;
Response.Write(cm.windowmesg("资料汇出失败"));
}
finally
{
excel.Visible = true;
} }
else
{
Response.Write(cm.windowmesg("资料汇出成功")); } //Export(this.gvdate, "application/ms-excel", "蛌痄等.xls"); }
异常详细信息: System.UnauthorizedAccessException: 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。 ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限。ASP.NET 有一个在应用程序没有模拟时使用的基进程标识(通常,在 IIS 5 上为 {MACHINE}\ASPNET,在 IIS 6 上为网络服务)。如果应用程序正在通过 <identity impersonate="true"/> 模拟,则标识将为匿名用户(通常为 IUSR_MACHINENAME)或经过身份验证的请求用户。 要将 ASP.NET 访问权限授予某个文件,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 账户,选中所需访问权限对应的框。cs原码
protected void btnremit_Click(object sender, EventArgs e)
{
//资料汇出
deptid = Session["deptid"].ToString().Trim();
database cn = new database("MARKMANAGEDBConnectionString");
comm cm = new comm();
DataSet ds = new DataSet();
cn.OpenConn();
string str = "select userid,itemno,sum(number) as number from deptmove where fromno='" + txtcce.Text.ToString().Trim() + "' and deptid='" + deptid + "' group by userid,itemno";
cn.RunProc(str, ds);
cn.Dispose(cn.OpenConn());
if (ds.Tables[0].Rows.Count > 0)
{
int ico = 3;
GC.Collect();
Microsoft.Office.Interop.Excel._Application excel;
Microsoft.Office.Interop.Excel._Workbook xbk;
Microsoft.Office.Interop.Excel._Worksheet xst;
excel = new ApplicationClass();
xbk = excel.Workbooks.Add(true);
xst = (_Worksheet)xbk.ActiveSheet;
excel.Cells[1, ico / 2] = txtdept.Text + "移交凭单";
excel.Cells[2, 1] = "凭单编号";
excel.Cells[2, 2] = txtcce.Text.ToString().Trim();
excel.Cells[3, 1] = "日期";
excel.Cells[3, 2] = txtdate.Text.ToString().Trim();
excel.Cells[4, 1] = "时间";
excel.Cells[4, 2] = txttime.Text.ToString().Trim();
excel.Cells[5, 1] = "工号";
excel.Cells[5, 2] = "品号";
excel.Cells[5, 3] = "数量";
try
{
int i = 0;
for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
excel.Cells[i + 6, 1] = ds.Tables[0].Rows[i][0].ToString().Trim();
excel.Cells[i + 6, 2] = ds.Tables[0].Rows[i][1].ToString().Trim();
excel.Cells[i + 6, 3] = ds.Tables[0].Rows[i][2].ToString().Trim();
}
excel.Cells[i + 7, 1] = "移交人签名";
excel.Cells[i + 8, 1] = "承接人签名"; }
catch
{
excel.Visible = true;
Response.Write(cm.windowmesg("资料汇出失败"));
}
finally
{
excel.Visible = true;
} }
else
{
Response.Write(cm.windowmesg("资料汇出成功")); } //Export(this.gvdate, "application/ms-excel", "蛌痄等.xls"); }
excel.Cells[1, ico / 2] = txtdept.Text + "移交凭单";
excel.Cells[2, 1] = "凭单编号";
excel.Cells[2, 2] = txtcce.Text.ToString().Trim();
excel.Cells[3, 1] = "日期";
excel.Cells[3, 2] = txtdate.Text.ToString().Trim();
excel.Cells[4, 1] = "时间";
excel.Cells[4, 2] = txttime.Text.ToString().Trim();
excel.Cells[5, 1] = "工号";
excel.Cells[5, 2] = "品号";
excel.Cells[5, 3] = "数量";
如果那个高手可以教我,我打算用GRIDVIEW汇出
2.添加相应的权限
2:在"开始"->"运行"中输入dcomcnfg.exe启动"组件服务"
3:依次双击"组件服务"->"计算机"->"我的电脑"->"DCOM配置"
4:在"DCOM配置"中找到"Microsoft Excel 应用程序",在它上面点击右键,然后点击"属性",弹出"Microsoft Excel 应用程序属性"对话框
5:点击"标识"标签,选择"交互式用户"
6:点击"安全"标签,在"启动和激活权限"上点击"自定义",然后点击对应的"编辑"按钮,在弹出的"安全性"对话框中填加一个"NETWORK SERVICE"用户(注意要选择本计算机名),并给它赋予"本地启动"和"本地激活"权限.
7:依然是"安全"标签,在"访问权限"上点击"自定义",然后点击"编辑",在弹出的"安全性"对话框中也填加一个"NETWORKSERVICE"用户,然后赋予"本地访问"权限.按照这样配置Excel的DCOM权限,我的程序现在可以运行了。
注意:这是在WIN2003上配置的,如果是在WIN2000,xp上,是配置ASPNET用户