关于datatable导出execl
2024-10-18 18:18:29
导出主要考虑响应流的问题 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();
} }
最新文章
- python-phpbb3 (可以用python3 操作 phpbb3 论坛的模块)
- javaWeb 数据库连接池连接数据库
- jQuery 1.9 Ajax代码带注释
- linux系统-代码行数计算
- 安装SQL Server Management Studio遇到的29506错误
- 卡尔曼滤波器【Kalman Filter For Dummies】
- iOS: 在键盘之上显示一个 View
- android在view.requestFocus(0)返回false的解决办法
- 理解 KMP 算法
- Python select 详解(转)
- Selenium2+python自动化38-显式等待(WebDriverWait)
- 利用cve-2017-11882的一次渗透测试
- linux 查看机器内存方法 (free命令)
- 修改Python IDLE代码配色及语法高亮主题
- java并发编程:线程安全管理类--原子包--java.util.concurrent.atomic
- OpenCV学习笔记 - Video Analysis - 录制视频
- 关于jFinal开发中遇到的中文乱码问题解决办法
- 我的QT5学习之路(一)——浅谈QT的安装和配置
- leetcode498
- dlut1188(wanghang的迷宫)
热门文章
- Linux下su与su -命令的本质区别
- jquery ajax调用WCF,采用System.ServiceModel.WSHttpBinding协议
- vnc viewer中开启剪切板复制内容到ubuntu系统中
- mysql-5.7 saving and restore buffer pool state 详解
- ActiveMQ + NodeJS + Stomp 入门
- SolrCloud:依据Solr Wiki的译文
- cscope使用技巧
- Java 如何实现线程间通信
- angular学习笔记(五)-阶乘计算实例(2)
- Django视图之URLconfs