我有一页面,通过下面的方式把<div></div>之间的数据导出到EXCEL,如果是执行單一页面,没有问题。但我的整个站点是用框架做的,把该页面以框架的方式打开,再导出EXCEL时,有两种情况:如果我选择弹出对话框里的“打开”,我的整个IE窗口都会关掉;如果选择“保存”,IE也会关掉,但EXCEL能下载回本地。
希望得到各位高手的帮助!!!!
源码如下://导出函数
private void GenerateByHtmlString(string FileType, string FileName)
{
Response.Charset = "utf-8";
//Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(FileName));
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
divtest.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
//按钮里调用:
protected void btnOutExcel_Click(object sender, EventArgs e)
{
GenerateByHtmlString("application/ms-excel", "ResApply.xls");
}
希望得到各位高手的帮助!!!!
源码如下://导出函数
private void GenerateByHtmlString(string FileType, string FileName)
{
Response.Charset = "utf-8";
//Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(FileName));
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
divtest.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
//按钮里调用:
protected void btnOutExcel_Click(object sender, EventArgs e)
{
GenerateByHtmlString("application/ms-excel", "ResApply.xls");
}
最好window.open("saveExcel.aspx")
把 Response.End(); 去掉,也不能解决问题。对话框是有弹出来,但你一点击,IE就关了。
TO:net_lover(【孟子E章】)
其实我的代码是参考老哥你的,自己也感觉window.open可行,但我的页面内容比较多,我只想输出一部分即可,不想再弹出一个窗口。不知还有没有其他方法可解决???
望指教!
把上面这句改成下面这样的response.AddHeader("Content-Disposition","inline;filename="+System.Web.HttpUtility.UrlEncode(strFileName,System.Text.Encoding.GetEncoding("UTF-8")));//下载对话框中的文件名
注意其中的inline,就是这个东西在起作用
我的目的是想对数据进行打印或保存啊!
HEAD里面加<base='_blank'>