Excel 导出的方法 之二
// <summary>
/// 导出到Excel lichenghu
/// </summary>
/// <param name="dt"></param>
public static void ToExcel(DataTable dt)
{
string sb = "";
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
sb = sb + dr[i].ToString() + "\t";
}
sb = sb + "\n";
}
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=MyExcel.xls");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
hw.WriteLine(sb.ToString());
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
hw.Flush();
hw.Close();
tw.Flush();
tw.Close();
}
最新文章
- 浅谈ajax
- sql server创建备份计划
- Oracle存储过程中异步调用的实际操作步骤
- nodejs 任务调度使用
- ORA-00265: instance recovery required, cannot set ARCHIVELOG
- PHP连接SQL Server(sqlsrv)
- Failed to create a &#39;System.Type&#39; from the text &#39; &#39; in wpf(无法从文本创建类型)
- TCP keepalive under Linux
- C++程序原码
- text-overflow:ellipsis 的应用(转载)
- [置顶] Android安全机制分析
- IOS百度地图之--->;第二篇《大头针__简单使用及自定义》
- AI CV 会议2018
- GIF动态图制作
- Kafka单节点及集群配置安装
- xcode打包命令
- Pytest 简明教程
- 配置允许匿名用户登录访问vsftpd服务,进行文档的上传下载、文档的新建删除等操作
- 20155315 2016-2017-2 《Java程序设计》第七周学习总结
- linq 根据指定条件返回集合中不重复的元素