收藏 分享(赏)

C_将数据导出到_Excel表.doc

上传人:hyngb9260 文档编号:6398256 上传时间:2019-04-11 格式:DOC 页数:16 大小:62KB
下载 相关 举报
C_将数据导出到_Excel表.doc_第1页
第1页 / 共16页
C_将数据导出到_Excel表.doc_第2页
第2页 / 共16页
C_将数据导出到_Excel表.doc_第3页
第3页 / 共16页
C_将数据导出到_Excel表.doc_第4页
第4页 / 共16页
C_将数据导出到_Excel表.doc_第5页
第5页 / 共16页
点击查看更多>>
资源描述

1、一、 中导出 Execl 的方法:在 中导出 Execl 有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上;一种是将文件直接将文件输出流写给浏览器。在 Response 输出时, t 分隔的数据,导出 execl 时,等价于分列,n 等价于换行。 1、将整个 html 全部输出 execl此法将 html 中所有的内容,如按钮,表格,图片等全部输出到 Execl 中。 Response.Clear(); Response.Buffer= true; Response.AppendHeader(“Content-Disposition“,“attachme

2、nt;filename=“+DateTime.Now.ToString(“yyyyMMdd“)+“.xls“); Response.ContentEncoding=System.Text.Encoding.UTF8; Response.ContentType = “application/vnd.ms-excel“; this.EnableViewState = false; 这里我们利用了 ContentType 属性,它默认的属性为 text/html,这时将输出为超文本,即我们常见的网页格式到客户端,如果改为 ms-excel 将将输出 excel 格式,也就是说以电子表格的格式输出到客

3、户端,这时浏览器将提示你下载保存。ContentType 的属性还包括:image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 。同理,我们也可以输出(导出 )图片、word 文档等。下面的方法,也均用了这个属性。2、将 DataGrid 控件中的数据导出 Execl上述方法虽然实现了导出的功能,但同时把按钮、分页框等 html 中的所有输出信息导了进去。而我们一般要导出的是数据,DataGrid 控件上的数据。 System.Web.UI.Control ctl=this.DataGrid1;/DataGrid1 是你在窗体中拖放的控件HttpC

4、ontext.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“;ctl.Page.EnableView

5、State =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(); 如果你的 DataGrid 用了分页,它导出的是当前页的信息,也就是它导出的是 DataG

6、rid中显示的信息。而不是你 select 语句的全部信息。为方便使用,写成方法如下: public void DGToExcel(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.Enco

7、ding.Default; HttpContext.Current.Response.ContentType =“application/ms-excel“;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.Writ

8、e(tw.ToString(); HttpContext.Current.Response.End(); 用法:DGToExcel(datagrid1);3、将 DataSet 中的数据导出 Execl有了上边的思路,就是将在导出的信息,输出(Response)客户端,这样就可以导出了。那么把 DataSet 中的数据导出,也就是把 DataSet 中的表中的各行信息,以 ms-excel 的格式 Response 到 http 流,这样就 OK 了。说明:参数 ds 应为填充有数据表的 DataSet,文件名是全名,包括后缀名,如 execl2006.xls public void Crea

9、teExcel(DataSet ds,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=“; /定义表对象与行对象,同时用 DataSet 对其值进行初始化 DataTable dt=ds.

10、Tables0; DataRow myRow=dt.Select();/可以类似 dt.Select(“id10“)之形式达到数据筛选目的int i=0; int cl=dt.Columns.Count; /取得数据表各列标题,各标题之间以 t 分割,最后一个列标题后加回车符 for(i=0;i1)range.BordersExcel.XlBordersIndex.xlInsideVertical.ColorIndex=Excel.XlColorIndex.xlColorIndexAutomatic;workbook.Close(missing,missing,missing);xlApp.Quit(); 三、附注: 虽然都是实现导出 execl 的功能,但在 和 winform 的程序中,实现的代码是各不相同的。在 中,是在服务器端读取数据,在服务器端把数据以 ms-execl 的格式,以 Response 输出到浏览器(客户端) ;而在 winform 中,是把数据读到客户端(因为 winform 运行端就是客户端),然后调用客户端安装的 office 组件,将读到的数据写在 execl 的工作簿中。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 实用文档 > 办公软件应用

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报