zoukankan
html css js c++ java
net导出excel方法汇总
1
、由dataset生成
引用
http://www.cnblogs.com/yknb/articles/434084.html
public
void
CreateExcel(DataSet ds,
string
typeid,
string
FileName)
{
HttpResponse resp;
resp
=
Page.Response;
resp.ContentEncoding
=
System.Text.Encoding.GetEncoding(
"
GB2312
"
);
resp.AppendHeader(
"
Content-Disposition
"
,
"
attachment;filename=
"
+
FileName);
string
colHeaders
=
""
, ls_item
=
""
;
int
i
=
0
;
//
定义表对象与行对像,同时用DataSet对其值进行初始化
DataTable dt
=
ds.Tables[
0
];
DataRow[] myRow
=
dt.Select(
""
);
//
typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件
if
(typeid
==
"
1
"
)
{
//
取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
for
(i
=
0
;i colHeaders
+=
dt.Columns[i].Caption.ToString()
+
"
\t
"
;
colHeaders
+=
dt.Columns[i].Caption.ToString()
+
"
\n
"
;
//
向HTTP输出流中写入取得的数据信息
resp.Write(colHeaders);
//
逐行处理数据
foreach
(DataRow row
in
myRow)
{
//
在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
for
(i
=
0
;i ls_item
+=
row[i].ToString()
+
"
\t
"
;
ls_item
+=
row[i].ToString()
+
"
\n
"
;
//
当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
resp.Write(ls_item);
ls_item
=
""
;
}
}
else
{
if
(typeid
==
"
2
"
)
{
//
从DataSet中直接导出XML数据并且写到HTTP输出流中
resp.Write(ds.GetXml());
}
}
//
写缓冲区中的数据到HTTP头文件中
resp.End();
}
2
、由datagrid生成
public
void
ToExcel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.AppendHeader(
"
Content-Disposition
"
,
"
attachment;filename=Excel.xls
"
);
HttpContext.Current.Response.Charset
=
"
UTF-8
"
;
HttpContext.Current.Response.ContentEncoding
=
System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType
=
"
application/ms-excel
"
;
//
image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState
=
false
;
System.IO.StringWriter tw
=
new
System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw
=
new
System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
用法:ToExcel(datagrid1);
查看全文
相关阅读:
ElasticSearch——Logstash输出到Elasticsearch配置
ElasticSearch——分词
Kafka——JVM调优
HBase管理与监控——强制删除表
HBase管理与监控——HBase region is not online
HBase管理与监控——内存调优
C#利用WMI获取 远程计算机硬盘数据
防止SQL注入方法总结
C# 将一个DataTable的结构直接复制到另一个DataTable
C# 按位或,按位与, 按位异或
原文地址:https://www.cnblogs.com/zzxap/p/2175989.html
最新文章
ELK实践
Logstash 安装配置使用
Filebeat在windows下安装使用
ElasticSearch使用C#操作文档
ElasticSearch数据导入By Postman
Elasticsearch 索引文档的增删改查
javascript
由web程序出现乱码开始挖掘(Bom头、字符集与乱码)
面向对象namespace
面向对象魔术常量
热门文章
php 获取 ip 地址 函数:
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details
Thinkphp框架 表单自动验证登录注册 ajax自动验证登录注册
ThinkPHP框架表单验证
ThinkPHP登陆注册
thinkphp使用ajax
ElasticSearch、Logstash管理和监控——blocked by: [FORBIDDEN/12/index read-only / allow delete (api)]
Mysql——常用命令
ElasticSearch——常用命令
ElasticSearch——自定义模板
Copyright © 2011-2022 走看看