在页面中导出excel,保存文件时出现javascript错误“拒绝访问”?

test234 2006-11-29 07:36:06
有一个DataGrid列表的页面,上面有Checkbox等(声明:这个页面在框架中),在win2000、win2003(没打sp1补丁)下,在导出到excel时,弹出保存文件的窗口后,点保存就会出现javascript错误,提示是“拒绝访问”。并且页面上的checkbox也不能选了,点击就出错,直接打开或取消没问题。这什么解决?
在winXP SP2下和win2003 SP1下都没有问题。不知道和系统有没有关系,或者哪位能告诉我win2000升级什么补丁不报错了也可以。
谢谢。

以下是导出代码大家帮忙看看:


private void ExportQuestions(DataSet ds)
{
object missing=Missing.Value;

Excel.Application excel = null;

Excel.Workbooks workbooks = null;

Excel.Workbook workbook = null;

Excel.Sheets sheets = null;

Excel.Worksheet worksheet = null;

excel = new Excel.ApplicationClass();

try
{

string excelname="";

excelname=this.tikunameTextbox.Text + "123.xls";

string filename=System.Web.HttpContext.Current.Server.MapPath("../")+"\\excel\\"+excelname;

FileInfo sTemplate = new FileInfo(System.Web.HttpContext.Current.Server.MapPath("../")+"\\templet\\123.xls"); //导入Excel模板

sTemplate.CopyTo(filename,true); //拷贝生成Excel新文件

///////////////参数初始化//////////////////
int sum=0;


excel.Visible = false;

excel.DisplayAlerts = false;

workbooks = excel.Workbooks;

workbook = workbooks.Open(filename,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);

sheets = workbook.Worksheets;

worksheet = (Excel.Worksheet)sheets[1];

// worksheet.Name = "试题导出列表"; //设置表名

// oCells = worksheet.Cells;

sum=1;

for(int i=0;i<ds.Tables[0].Rows.Count;i++) //写入Excel
{
worksheet.Cells[sum,1]=ds.Tables[0].Rows[i]["aa"].ToString().Trim();
worksheet.Cells[sum,2]=ds.Tables[0].Rows[i]["bb"].ToString().Trim();

sum++;
}

excel.get_Range(excel.Cells[1,1],excel.Cells[sum-1,2]).Borders.LineStyle = 1;

ds.Dispose();

workbook.Save();

System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);

workbook = null;

workbooks.Close();

System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);

workbooks = null;

System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

excel = null;

GC.Collect();

System.Web.HttpContext.Current.Response.Clear();

System.Web.HttpContext.Current.Response.ClearHeaders();

System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(excelname));

System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";

FileStream myFile = File.OpenRead(filename); //读取文件进入FileStream

byte[] fileCont = new byte[myFile.Length];

myFile.Read(fileCont,0,(int)myFile.Length); //将文件流中的内容转成byte数组

System.Web.HttpContext.Current.Response.BinaryWrite(fileCont);

System.Web.HttpContext.Current.Response.Flush();

System.Web.HttpContext.Current.Response.Clear();

System.Web.HttpContext.Current.Response.Close(); //关闭文件流

myFile.Close();

}
catch
{
if(workbook!=null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
}

workbook = null;
if(workbooks!=null)
{

workbooks.Close();

System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);

workbooks = null;
}

if(excel!=null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

}
excel = null;

GC.Collect();

System.Web.HttpContext.Current.Response.Clear();

System.Web.HttpContext.Current.Response.ClearHeaders();
Page.RegisterStartupScript("mess","<script>alert('导出失败!');</script>");
}
}
...全文
768 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
test234 2006-12-20
  • 打赏
  • 举报
回复
我把所有补丁都打了,还是不行,看来和操作系统有关系。。
dick78 2006-12-01
  • 打赏
  • 举报
回复
在工具里关闭客户端调试功能.
test234 2006-12-01
  • 打赏
  • 举报
回复
最新进展,点击保存时,不出现错误了,可以正常保存,可是保存后,再点击页面上的Checkbox等控件,会出现一个“未指明的错误”。
nimeide1234567890 2006-12-01
  • 打赏
  • 举报
回复
<%@ Page language="c#" Src="excel.cs" AutoEventWireup="false" Inherits="DataGrid_import_WordExcel.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>OutPutExcel</title>
</HEAD>
<link rel="stylesheet" href="Style.css" type="text/css">
<body>
<form id="Form1" method="post" runat="server">
<table width="170%"><tr><td>
<asp:datagrid
id="DataGrid1"
bgcolor="#efefef"
HeaderStyle-BackColor="#718BD6"
HeaderStyle-ForeColor="#FFFF66"
AlternatingItemStyle-BackColor="#FFFFFF"
itemstyle-backcolor="#FFFFFF"
runat="server">
<ItemStyle HorizontalAlign="center" Height="20"></ItemStyle>
<Columns>

</Columns>
</asp:datagrid>
</td></tr></table>
<P>
<asp:button id="Btn_Import_Excel" runat="server" Text="轉到EXCEL"></asp:button></P>
</form>
</body>
</HTML>
------------------------------
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.IO;
using System.Data.SqlClient ;
using System.Text;
using System.Configuration;

namespace DataGrid_import_WordExcel
{

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button BtnImportWord;
protected System.Web.UI.WebControls.Button Btn_Import_Excel;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public DataRow dr;
private DataSet myDS =new DataSet();

private void Page_Load(object sender, System.EventArgs e)
{
//CreateDataSet();
Data_Load();
if(!IsPostBack)
{
//DataBind();
}
}

#region Web 敦极扢數?汜傖腔測鎢
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Btn_Import_Excel.Click += new System.EventHandler(this.Btn_Import_Excel_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

private void ExportDataGrid(string FileType, string FileName) //植DataGrid絳堤
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

Response.AppendHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState =false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw =new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
private void Btn_Import_Excel_Click(object sender, System.EventArgs e)
{
ExportDataGrid("application/ms-excel", "Excel.xls"); //絳善Excel
}

private void Data_Load()
{

SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["produce1"]);
SqlCommand cmd=new SqlCommand("excel",myConnection);
cmd.CommandType=CommandType.StoredProcedure;

if(Session["companyno"]!="")
{
cmd.Parameters.Add("@sql1",SqlDbType.NVarChar,50);
cmd.Parameters["@sql1"].Value = Session["companyno"];
}
else
{
cmd.Parameters.Add("@sql1",SqlDbType.NVarChar,50);
cmd.Parameters["@sql1"].Value = "";
}

myConnection.Open();

DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}

DataView CreateDataSource()
{
string nowDSN=ConfigurationSettings.AppSettings["produce1"];
SqlConnection myConnection=new SqlConnection(nowDSN);

SqlCommand cmd=new SqlCommand("excel",myConnection);
cmd.CommandType=CommandType.StoredProcedure;

if(Session["companyno"]!="")
{
cmd.Parameters.Add("@sql1",SqlDbType.NVarChar,50);
cmd.Parameters["@sql1"].Value = Session["companyno"];
}
else
{
cmd.Parameters.Add("@sql1",SqlDbType.NVarChar,50);
cmd.Parameters["@sql1"].Value = "";
}

DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
return ds.Tables["block2"].DefaultView;
myConnection.Close();
Page.DataBind();
}

void DataBind()
{
DataView source=CreateDataSource();
if(!IsPostBack)
{
}
DataGrid1.DataSource = source;
DataGrid1.DataBind();
}

}
}
commonmj 2006-11-30
  • 打赏
  • 举报
回复
要配置excel组件的访问权限
xb375 2006-11-30
  • 打赏
  • 举报
回复
UP
test234 2006-11-30
  • 打赏
  • 举报
回复
to: heroes33() 是在下载框点击保存的时候出现的脚本错误,没方法控制。
heroes33 2006-11-30
  • 打赏
  • 举报
回复
不能用readyState==complate判断吗
test234 2006-11-30
  • 打赏
  • 举报
回复
我晕, commonmj的呢称....

不是调用excel的问题,excel调用成功了,只是在下载框中会出错,和excel权限没什么关系吧。我也用dcomcnfg试过了,在三个地方加权限,都加过了,好像不是那里的事。

csdn里有一个帖子也是这样的问题,但最后也没有解决办法.http://topic.csdn.net/t/20041028/20/3501069.html
test234 2006-11-29
  • 打赏
  • 举报
回复
有道理,不过好像和框架有关系,如果不在框架内单独运行页面是不会报错的,加在框架内就出现了错误。
lindping 2006-11-29
  • 打赏
  • 举报
回复
跟页面的属性readyState有关,readyState的值有四种
loading Object is loading its data.
loaded Object has finished loading its data.
interactive User can interact with the object even though it is not fully loaded.
complete Object is completely initialized.

当保存文件完毕以后,当前window的readyState值为interactive,所以所有页面上的控件都不能访问,但是该属性是只读的,不能改变,所以我遇到过这种问题,也没有找到解决问题的办法,好像是强制刷新页面解决的.

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧