在开发会遇到将gridview中的数据导入到excel 这样的需求,当girdview有多页数据时按照一般的方式导出的数据只可能是当前页的数据,后几页的数据还在数据库内,没有呈现到页面上,传统的方式是将标记字符和文本写入到 ASP.NET 服务器控件输出流,没有呈现的页面不可能输出到excel中。下面提供一种解决方式将所以数据都能导入到excel中。
第一步:记录查询条件,当用户点击下载的时候以相同的条件查询,呈现到datatable
这一步没有固定的代码,可以在用户查询的时候用ViewState记录查询条件,例如
OrderMng.OrderList_GetSupOrderList_BindData( Param, fDynamicParam, PageParamByCus, dystr);(查询出单页的数据绑定到girdview)
ViewState["GParam"] = Param; ViewState["DPaRam"] = fDynamicParam; ViewState["PageParam"] = PageParamByCus; ViewState["strparam"] = dystr;(记录查询条件)
用户点下载的时候再以相同的条件,查询出所有数据到datatable
Hashtable GParam = (Hashtable)ViewState["GParam"];
Hashtable DPaRam = (Hashtable)ViewState["DPaRam"];
Hashtable PageParam= (Hashtable)ViewState["PageParam"];
string strparam=ViewState["strparam"].ToString();
是将开始查询的条件转化回来
DataTable dt = OrderMng.OrderList_GetSupOrderList(GParam, DPaRam, PageParam, strparam);
相同的条件查询后到datatable
第二步:处理datatable , 修改列名,排序,修改显示的内容
1 protected void DataTableTranfer(DataTable dt)
2 {
3 DataColumn dc = null ;
4 dt.Columns.Add( " 类型 " , Type.GetType( " System.String " ));
5 dt.Columns.Add( " 商品名称 " , Type.GetType( " System.String " ));
6 dt.Columns.Add( " 结算状态 " , Type.GetType( " System.String " ));
7 foreach (DataRow row in dt.Rows)
8 {
9 if (row[ " isSecret " ].ToString() == " 0 " )
10 {
11 row[ " 类型 " ] = " 网络订单 " ;
12 }
13 else
14 {
15 row[ " 类型 " ] = " 手工订单 " ;
16 }
17 }
18 row[ " 商品名称 " ] = " ID[: " + row[ " ProductID " ] + " ] " + row[ " ProductName " ];
19 // IsCheckOutk是页面上判断的方法
20 row[ " 结算状态 " ] = IsCheckOut(row[ " CheckOutID " ], row[ " isrepeal " ]);
21
22
23 // 修改列名
24 dt.Columns[ " OrderID " ].ColumnName = " 订单号 " ;
25 dt.Columns[ " BuyTime " ].ColumnName = " 交易日期 " ;
26 dt.Columns[ " BuyCount " ].ColumnName = " 我的销售数量 " ;
27 // 排序列
28 dt.Columns[ " 订单号 " ].SetOrdinal( 0 );
29 dt.Columns[ " 类型 " ].SetOrdinal( 1 );
30 dt.Columns[ " 交易日期 " ].SetOrdinal( 2 );
31 // 移除列
32 dt.Columns.Remove(dt.Columns[ " totalsize " ]);
33 dt.Columns.Remove(dt.Columns[ " rownum " ]);
34
35 }
第三步:将datatable转化成excel
public static void TableToExcel(DataTable tb, string fileName)
{
string Filename = fileName;
System.Web.HttpContext context = System.Web.HttpContext.Current;
if ((tb != null ))
{
context.Response.Clear();
context.Response.Charset = " GB2312 " ;
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding( " GB2312 " );
context.Response.ContentType = " application/ms-excel " ;
context.Response.AppendHeader( " content-disposition " , " attachment;filename=\ "" + System.Web.HttpUtility.UrlEncode(Filename, System.Text.Encoding.GetEncoding( " GB2312 " )) +DateTime.Now.ToString( " yyyyMMdd " )+ " .xls\ "" );
CultureInfo cult = new CultureInfo( " zh-CN " , true );
StringWriter sw = new StringWriter(cult);
HtmlTextWriter htw = new HtmlTextWriter(sw);
DataGrid dgrid = new DataGrid();
dgrid.DataSource = tb.DefaultView;
dgrid.AllowPaging = false ;
dgrid.DataBind();
htw.WriteLine( " <meta http-equiv=\ " Content - Type\ " content=\ " text / html;charset = GB2312\ " > " );
dgrid.RenderControl(htw);
context.Response.Write(sw.ToString());
context.Response.End();
}
}
自此完成操作,不足之处请指导~