各位大侠,求救。。。

lrcitpower 2011-09-21 03:39:23

Response.Clear();
Response.Buffer = true;
string pagename = System.IO.Path.GetFileName(Request.Path).ToString();
pagename = pagename.Replace(".aspx", "");
//为了防止重名,获得日期为文件名年月日时分秒毫秒
string datetime = System.DateTime.Now.ToString("yyyMMddHHmmss");
string FileName = pagename + datetime + ".xls";

Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
Response.Close();
...全文
121 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
肖无疾 2011-09-21
  • 打赏
  • 举报
回复
从来不用此句
Response.Close();
关闭到客户端的套接字连接。
gudujianxiao 2011-09-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 anchenyanyue 的回复:]
C# code


System.Web.UI.WebControls.DataGrid dgExport = null;

if (dtData != null)
{

Response.ClearContent();
Response.AddHeader("cont……
[/Quote]
同样的代码用了好几次.那就要考虑重构了.写个公共方法.调用同一方法
caxieyou 2011-09-21
  • 打赏
  • 举报
回复
学习下,遇到类似问题
qf19881213 2011-09-21
  • 打赏
  • 举报
回复
把内容导入到execl里面?
double_huang 2011-09-21
  • 打赏
  • 举报
回复
你可以插入断点调试看!
md5e 2011-09-21
  • 打赏
  • 举报
回复
文件头,需要是utf-8,放的是文件名和文件大小等信息,不要带路径
lrcitpower 2011-09-21
  • 打赏
  • 举报
回复
这种写法有什么特别要注意的地方没?
lrcitpower 2011-09-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 liuchaolin 的回复:]
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("不带路径的文件中文名.xls", Encoding.UTF8).ToString());
[/Quote]


这个是什么意思?
lrcitpower 2011-09-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 id270 的回复:]
可能。有可能。你压根没运行保存代码
[/Quote]


这个不可能,同样的代码我在好几处用,只有一个页面上有这种情况,其他的完全正确,没什么异常情况的
暗尘掩月 2011-09-21
  • 打赏
  • 举报
回复

System.Web.UI.WebControls.DataGrid dgExport = null;

if (dtData != null)
{

Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlEncode("xxxx.xls"));

Response.ContentType = "application/xls";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
sw.WriteLine("<div align=\"center\">xxx</div>");
sw.WriteLine("<br />");

//为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrids
dgExport = new System.Web.UI.WebControls.DataGrid();
dgExport.DataSource = dtData;
dgExport.DataBind();

dgExport.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

}
md5e 2011-09-21
  • 打赏
  • 举报
回复
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("不带路径的文件中文名.xls", Encoding.UTF8).ToString());
快乐的小二兔 2011-09-21
  • 打赏
  • 举报
回复

可能。有可能。你压根没运行保存代码
lrcitpower 2011-09-21
  • 打赏
  • 举报
回复
。。。
我发的信息怎么不显示。。。晕

就是代码运行到
Response.Flush();
Response.End();
Response.Close();
不出现保存路径的对话框,数据也不成导出。代码却运行结束,也没什么错误
lrcitpower 2011-09-21
  • 打赏
  • 举报
回复
这是我用的导出GridView中数据到EXCEL的代码,
当运行到:
Response.Flush();
Response.End();
Response.Close();
没用出现文件保存对话框,就直接运行完成。数据也没有导出成功。
各位大侠帮忙看看是什么情况?
快乐的小二兔 2011-09-21
  • 打赏
  • 举报
回复
啥问题????

62,046

社区成员

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

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

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

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