C23+将数据导出到Excel汇总.docVIP

  • 1
  • 0
  • 约7.19千字
  • 约 9页
  • 2016-12-10 发布于重庆
  • 举报
C23将数据导出到Excel汇总

/sufei/archive/2009/05/23/1487540.html /forum.php?modviewthreadtid4 一、中导出Excel的方法: 在中导出Excel有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上;一种是将文件直接将文件输出流写给浏览器。在Response输出时,t分隔的数据,导出Excel时,等价于分列,n等价于换行。 1、将整个html全部输出Excel 此法将html中所有的内容,如按钮,表格,图片等全部输出到Excel中。Response.Clear;Response.Buffer true;Response.AppendHeaderContent-Disposition,attachment;filename+DateTime.Now.ToStringyyyyMMdd+.xls;Response.ContentEncodingSystem.Text.Encoding.UTF8;Response.ContentTypeapplication/vnd.ms-excel;this.EnableViewStatefalse;这里我们利用了ContentType属性,它默认的属性为text/html,这时将输出为超文本,即我们常见的网页格式到客户端,如果改为ms-excel将将输出excel格式,也就是说以电子表格的格式输出到客户端,这时浏览器将提示你下载保存。ContentType的属性还包括:image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 。同理,我们也可以输出导出图片、word文档等。下面的方法,也均用了这个属性。 2、将DataGrid控件中的数据导出Excel 上述方法虽然实现了导出的功能,但同时把按钮、分页框等html中的所有输出信息导了进去。而我们一般要导出的是数据,DataGrid控件上的数据。 System.Web.UI.Control ctlthis.DataGrid1; //DataGrid1是你在窗体中拖放的控件 HttpContext.Current.Response.AppendHeaderContent-Disposition,attachment;filenameExcel.xls; HttpContext.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.StringWriter tw new System.IO.StringWriter ; System.Web.UI.HtmlTextWriter hw new System.Web.UI.HtmlTextWriter tw; ctl.RenderControlhw; HttpContext.Current.Response.Writetw.ToString; HttpContext.Current.Response.End; 如果你的DataGrid用了分页,它导出的是当前页的信息,也就是它导出的是DataGrid中显示的信息。而不是你select语句的全部信息。 为方便使用,写成方法如下: public void DGToExcelSystem.Web.UI.Control ctlHttpContext.Current.Response.AppendHeaderContent-Disposition,attachment;filenameExcel.xls;HttpContext.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.StringWriter tw new System.IO.StringWriter ;System.Web.UI.HtmlTextWriter hw new System.Web.UI.HtmlTextWr

文档评论(0)

1亿VIP精品文档

相关文档