asp.net 把数据导出为excel
本篇介绍c#中如何使用DataTable导出Excel,至于其他的导出方法,这里不作介绍!
1.首页从数据库读取数据,得到DataTable:
DataTable dt = HelperExecuteSql.Query("select ID,name,author,newsContent,inDate from newsInfo").Tables[];
2.使用StringWriter类:将信息写入字符串(其命名空间为:System.IO)
private StringWriter GetStringWriter(DataTable dt)
{
StringWriter sw = new StringWriter();
//读列名,也可以自定义列名(即导出excel的表头)
//foreach (DataColumn dc in dt.Columns)
//{ sw.Write(dc.ColumnName + "\t");}
//表头,自定义
sw.Write("序号\t");
sw.Write("新闻名\t");
sw.Write("作者\t");
sw.Write("新闻内容\t");
sw.Write("上传时间\t");//读列值,重新的一行
sw.Write(sw.NewLine);
if (dt != null)
{
foreach (DataRow dr in dt.Rows)
{
for (int i = ; i < dt.Columns.Count; i++)
{
sw.Write(dr[i].ToString() + "\t");
}
sw.Write(sw.NewLine);
}
}
sw.Close();
return sw;
}
3. ExcelImport(dt, "新闻列表");
protected void ExcelImport(DataTable dt, string ExportFileName)
{
StringWriter sw = GetStringWriter(dt);
//当前编码
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//把输出的文件名进行编码
string fileName = HttpUtility.UrlEncode(ExportFileName, System.Text.Encoding.UTF8);
//文件名
string str = "attachment;filename=" + fileName + ".xls";
//把文件头输出,此文件头激活文件下载框
HttpContext.Current.Response.AppendHeader("Content-Disposition", str);//http报头文件
HttpContext.Current.Response.ContentType = "application/ms-excel";
this.Page.EnableViewState = false;
Response.Write(sw);
Response.End();
}
asp.net 把数据导出为excel的更多相关文章
- asp.net将数据导出到excel
本次应用datatable导出,若用gridview(假设gridview设为了分页显示)会出现只导出当前页的情况. protected void btnPrn_Click(object sender ...
- asp.net DataSet数据导出到Excel中
方法: [STAThread]///这是必须的 public override void VerifyRenderingInServerForm(System.Web.UI.Control co ...
- asp.net 将repeater上数据导出到excel
1,首先得到一个DataTable public DataTable GetTable(string sql) { SqlConnnection con=new SqlConnection(Confi ...
- Asp.net网页中DataGridView数据导出到Excel
经过上网找资料,终于找到一种可以直接将GridView中数据导出到Excel文件的方法,归纳方法如下: 1. 注:其中的字符集格式若改为“GB2312”,导出的部分数据可能为乱码: 导出之前需要关闭分 ...
- asp.net教程:GridView导出到Excel或Word文件
asp.net教程:GridView导出到Excel或Word文件</ br> 在项目中我们经常会遇到要求将一些数据导出成Excel或者Word表格的情况,比如中国移动(我是中国移动用户) ...
- 将datagrid中数据导出到excel中 -------<<工作日志2014-6-6>>
前台datagrid数据绑定 #region 导出到excel中 /// <summary> /// 2014-6-6 /// </summary> / ...
- asp.net大数据导出execl实现分开压缩并下载
asp.net大数据导出execl实现分开压缩并下载 /// <summary> /// 导出数据到EXCEL 多个表的 /// </summary> /// <para ...
- 学习笔记 DataGridView数据导出为Excel
DataGridView数据导出为Excel 怎样把WinForm下的“DGV”里的绑定数据库后的数据导出到Excel中. 比如:在窗体里有个一“DGV”,DataGridView1,绑定了数据源 ...
- 将C1Chart数据导出到Excel
大多数情况下,当我们说将图表导出到Excel时,意思是将Chart当成图片导出到Excel中.如果是这样,你可以参考帮助文档中保存和导出C1Chart章节. 不过,也有另一种情况,当你想把图表中的数据 ...
随机推荐
- 【BZOJ】3669: [Noi2014]魔法森林(lct+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=3669 首先看到题目应该可以得到我们要最小化 min{ max{a(u, v)} + max{b(u, ...
- HDU 4671 Partition(定理题)
题目链接 这题,明显考察搜索能力...在中文版的维基百科中找到了公式. #include <cstdio> #include <cstring> #include <st ...
- 字符串分割与存入List集合
List<string> namelist = new List<string>(); string[] namejh = null; string name= "张 ...
- placeholder在不同浏览器下的表现及兼容方法
1.什么是placeholder? placeholder是html5新增的一个属性,当input或者textarea设置了该属性后,该值的内容将作为灰字提示显示在文本框中,当文本框获得焦点(或 ...
- yii uploadfile 错误提示: fileinfo php extension is not installed
rule规则里面增加 'checkExtensionByMimeType'=>false,
- CSS3系列:流式(弹性)布局(flex布局)
我的新伸缩盒子.http://www.cnblogs.com/leee/p/5533436.html
- 算法与数据结构题目的 PHP 实现:栈和队列 设计一个有 getMin 功能的栈
刚入手了一本<程序员代码面试指南>,书中题目的代码都是 Java 实现的,琢磨着把这些代码用 PHP 敲一遍,加深印象. 题目:设计一个有 getMin 功能的栈 —— 实现一个特殊的栈, ...
- PHP 单例模式代码片段
<?php error_reporting(E_ALL | E_STRICT); class single{ public $hash; static protected $ins = null ...
- Yii源码阅读笔记(二十)
View中应用布局和缓存内容部分: /** * Begins recording a block. * This method is a shortcut to beginning [[Block]] ...
- Partitioning
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The simplest scheme f ...