导出主要考虑响应流的问题

   curContext.Response.ContentType = "application/vnd.ms-excel";
   curContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".xls");
curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
 

public void ExportExecl(DataTable dtData)
{
System.Web.UI.WebControls.DataGrid dgExport = null;
// 当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null; if (dtData != null)
{
// 设置编码和附件格式
DateTime dt = DateTime.Now;
string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString();
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".xls");
curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
curContext.Response.Charset = ""; // 导出excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter); // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid
dgExport = new System.Web.UI.WebControls.DataGrid();
dgExport.DataSource = dtData.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind(); // 返回客户端
dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
} }

最新文章

  1. python-phpbb3 (可以用python3 操作 phpbb3 论坛的模块)
  2. javaWeb 数据库连接池连接数据库
  3. jQuery 1.9 Ajax代码带注释
  4. linux系统-代码行数计算
  5. 安装SQL Server Management Studio遇到的29506错误
  6. 卡尔曼滤波器【Kalman Filter For Dummies】
  7. iOS: 在键盘之上显示一个 View
  8. android在view.requestFocus(0)返回false的解决办法
  9. 理解 KMP 算法
  10. Python select 详解(转)
  11. Selenium2+python自动化38-显式等待(WebDriverWait)
  12. 利用cve-2017-11882的一次渗透测试
  13. linux 查看机器内存方法 (free命令)
  14. 修改Python IDLE代码配色及语法高亮主题
  15. java并发编程:线程安全管理类--原子包--java.util.concurrent.atomic
  16. OpenCV学习笔记 - Video Analysis - 录制视频
  17. 关于jFinal开发中遇到的中文乱码问题解决办法
  18. 我的QT5学习之路(一)——浅谈QT的安装和配置
  19. leetcode498
  20. dlut1188(wanghang的迷宫)

热门文章

  1. Linux下su与su -命令的本质区别
  2. jquery ajax调用WCF,采用System.ServiceModel.WSHttpBinding协议
  3. vnc viewer中开启剪切板复制内容到ubuntu系统中
  4. mysql-5.7 saving and restore buffer pool state 详解
  5. ActiveMQ + NodeJS + Stomp 入门
  6. SolrCloud:依据Solr Wiki的译文
  7. cscope使用技巧
  8. Java 如何实现线程间通信
  9. angular学习笔记(五)-阶乘计算实例(2)
  10. Django视图之URLconfs