知识大全 asp.net中将DataGrid的内容导出为excel文件

Posted 文件

篇首语:时间是把锋利的刀,成全过我的疯狂,也粉碎过我的梦想。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 asp.net中将DataGrid的内容导出为excel文件相关的知识,希望对你有一定的参考价值。

asp.net中将DataGrid的内容导出为excel文件  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  在HTML页面中就一个按钮     //////<asp:button id= Button runat= server          Text= 导出 >      ///</asp:button>    和 一个DataGrid    ///<asp:datagrid id= DataGrid runat= server >      ///</asp:datagrid>    aspx cs 文件的代码是:    ////public class WebForm : System Web UI Page           protected System Web UI WebControls DataGrid DataGrid ;      protected System Web UI WebControls Button Button ;      private   void   Page_Load(object   sender    System EventArgs   e)     

  DataGrid DataSource=CreateDataSource();       DataGrid DataBind();       //   在此处放置用户代码以初始化页面     

  ICollection   CreateDataSource()             DataTable   dt   =   new   DataTable();       DataRow   dr;       dt Columns Add(new   DataColumn( 身份证号码    typeof(string)));       dt Columns Add(new   DataColumn( 图书单价 typeof(decimal)));       dt Columns Add(new   DataColumn( 购买数量 typeof(Int )));       dt Columns Add(new   DataColumn( 总价格 typeof(decimal)));       for   (int   i   =   ;   i   <   ;   i++)               dr   =   dt NewRow();        dr[ ]   =   ;        dr[ ]   =      *   i   / ;        dr[ ]   =   i   +   ;        dr[ ]   =   (decimal)dr[ ]   *   (Int )dr[ ];        dt Rows Add(dr);              DataView   dv   =   new   DataView(dt);       return   dv;     

  private   void   Button _Click(object   sender    System EventArgs   e)             Response Clear();       Response Buffer=   true;       Response Charset= GB ;       Response AppendHeader( Content Disposition attachment;filename=FileName xls );       Response ContentEncoding=System Text Encoding GetEncoding( GB ); //设置输出流为简体中文       Response ContentType   =   application/ms excel ; //设置输出文件类型为excel文件        this EnableViewState   =   false;       System Globalization CultureInfo   myCItrad   =   new   System Globalization CultureInfo( ZH CN true);       System IO StringWriter   oStringWriter   =   new   System IO StringWriter(myCItrad);       System Web UI HtmlTextWriter   oHtmlTextWriter   =   new   System Web UI HtmlTextWriter(oStringWriter);       this DataGrid RenderControl(oHtmlTextWriter); // 在这里设置保存那个数据源的数据        Response Write(oStringWriter ToString());       Response End();            private void DataGrid _ItemDataBound(object sender System Web UI WebControls DataGridItemEventArgs e)  //这个设置显示的格式             if(e Item ItemType == ListItemType Item || e Item ItemType == ListItemType AlternatingItem)               e Item Cells[ ] Attributes Add( style vnd ms excel numberformat:@ );        e Item Cells[ ] Attributes Add( style vnd ms excel numberformat:¥# ### );                   #region Web 窗体设计器生成的代码      override protected void OnInit(EventArgs e)             //       // CODEGEN: 该调用是 ASP NET Web 窗体设计器所必需的        //       InitializeComponent();       base OnInit(e);     

cha138/Article/program/net/201311/12899

相关参考

知识大全 Asp.NET自定义DataGrid控件

Asp.NET自定义DataGrid控件  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  一&nb

知识大全 ASP.NET中为DataGrid添加合计字段

ASP.NET中为DataGrid添加合计字段  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  论

知识大全 ASP.NET中实现DataGrid数据排序

ASP.NET中实现DataGrid数据排序  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Vi

知识大全 一个ASP.Net的DataGrid分页控件

一个ASP.Net的DataGrid分页控件  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  写的

知识大全 ASP.NET中为DataGrid添加单选框

ASP.NET中为DataGrid添加单选框  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!&nbs

知识大全 ASP.NET中DataGrid控件应用技巧简述

ASP.NET中DataGrid控件应用技巧简述  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  

知识大全 Asp.net中DataGrid控件的自定义分页

Asp.net中DataGrid控件的自定义分页  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  

知识大全 ASP.NET中自定义DataGrid分页设置的实现

ASP.NET中自定义DataGrid分页设置的实现  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

知识大全 asp.net中显示DataGrid控件列序号的几种方法

asp.net中显示DataGrid控件列序号的几种方法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下

知识大全 asp.net导出Excel方法总结

asp.net导出Excel方法总结  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &n