在页面中导出excel,保存文件时出现javascript错误“拒绝访问”?
有一个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>");
}
}