Gridview 分多页时导出excel的解决方案

作者:袖梨 2022-06-25

Gridview 分多页时导出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("");
                dgrid.RenderControl(htw);
                context.Response.Write(sw.ToString());
                context.Response.End();
            }
        }


自此完成操作

相关文章

精彩推荐