欢迎来到天天文库
浏览记录
ID:18326692
大小:144.00 KB
页数:13页
时间:2018-09-16
《asp.net 导出 excel 数据的9种方案.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、简介Excel的强大之处在于它不仅仅只能打开Excel格式的文档,它还能打开CSV格式、Tab格式、websitetable等多钟格式的文档。它具备自动识别行号,字符,格式化数字等功能,例如:如果你在Excel单元格中输入数字"123456789012"会自动转化为"1.23457E+11"。背景介绍正因为Excel的强大和易用,大家都喜欢将数据导出为Excel备用。这里我会介绍一系列通过Asp.Net导出Excel数据的方法。http://touxiang.qqq23.com将导出文件存储到服务器并提供地址给客户端下载,或重定向到文件下载页面:当Response时
2、,数据列以"t"分隔,行以""分隔。好了,现在给大家展示这是怎么做的。使用代码导出方案1:导出全部HTML数据到Excel这种方法是将Html中的所有文档内容,包括按钮,表格,图片等所有页面内容导出为ExcelResponse.Clear();Response.Buffer=true;Response.AppendHeader("Content-Disposition","attachment;filename="+DateTime.Now.ToString("yyyyMMdd")+".xls");Response.ContentEncoding=System
3、.Text.Encoding.UTF8;Response.ContentType=""application/ms-excel";this.EnableViewState=false;这里我们使用了Page的"ContentType"属性,它默认为"text/Html",输出到客户端即为Html。如果我们将它改为"ms-excel",http://fuhao.ttplay8.cn页面将输出Excel格式的内容,客户端就可以下载并存储它了。页面property还包括:image/JPEG,text/HTML,image/GIFandvnd.ms-excel/mswor
4、d.方案2:从DataGrid导出数据到Excel尽管上面的方法能帮你导出Excel数据,但它导出了所有的HTML内容,包括按钮、图片等,这并不是我们所需要的。通常,我们仅仅需要导出DataGrid中的数据。System.Web.UI.Controlctl=this.DataGrid1;//DataGrid1(youcreatedinthewindowForm)HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");HttpCon
5、text.Current.Response.Charset="UTF-8";HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.Default;HttpContext.Current.Response.ContentType="application/ms-excel";ctl.Page.EnableViewState=false;System.IO.StringWritertw=newSystem.IO.StringWriter();System.Web.UI.HtmlTextWrit
6、erhw=newSystem.Web.UI.HtmlTextWriter(tw);ctl.RenderControl(hw);HttpContext.Current.Response.Write(tw.ToString());HttpContext.Current.Response.End();如果你有多个包含DataGrid并需导出数据的页面,我们可以封装方法:publicvoidDGToExcel(System.Web.UI.Controlctl){HttpContext.Current.Response.AppendHeader("Content-Dispos
7、ition","attachment;filename=Excel.xls");HttpContext.Current.Response.Charset="UTF-8";HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.UTF8;HttpContext.Current.Response.ContentType="application/ms-excel";ctl.Page.EnableViewState=false;System.IO.StringWritertw=newSyste
此文档下载收益归作者所有