Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
lvResult.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
是用这种方法导出的。但是点导出按钮的时候,总提示用迅雷下载,而且下载的时候是当前.aspx文件。请问有什么办法解决?
Response.ContentType = "application/ms-excel";
在前面加上这句
这句我有。。调用这个方法的时候加进去的。没用。
@假扮天使: Response.Flush();补在Response.Write(tw.ToString());后面试试
@--宁静以致远--: 还是没用。
一般迅雷会自动把文件名改成导出的excel名字的
要不想让迅雷弹出,只能在迅雷设置里面把浏览器监控关掉,没有好办法了
试了好几次,在本地生成XML构造Excel还有用文件流写一个Excel文件,都不好使。唉。
可以看看我的代码:
/// <summary> /// 将GridView的数据输出到Excel文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnExportToExcel_Click(object sender, EventArgs e) { if (GridView1.Rows.Count == 0) { return; } Response.ClearContent(); Response.ContentEncoding = System.Text.Encoding.Default; Response.AddHeader("content-disposition", "attatchment;filename=" + HttpUtility.UrlEncode("Sheet1.xls", System.Text.Encoding.UTF8)); Response.ContentType = "application/vnd.ms-excel"; using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { GridView1.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); } } } /// <summary> /// 必须重载此方法,以便支持上面的导出操作 /// </summary> /// <param name="control">需要导出数据的控件</param> public override void VerifyRenderingInServerForm(Control control) { }