How to export excel when the Gridview is divided into multiple pages

Source: Internet
Author: User

How to export excel when the Gridview is divided into multiple pages

Step 1: record the query conditions. When a user clicks download, the query conditions are the same and displayed to the datatable.

There is no fixed code in this step. You can use ViewState to record query conditions during user queries, for example

OrderMng. OrderList_GetSupOrderList_BindData (Param, fDynamicParam, PageParamByCus, dystr); (the data on a single page is queried and bound to girdview)
ViewState ["GParam"] = Param; ViewState ["DPaRam"] = fDynamicParam; ViewState ["PageParam"] = PageParamByCus; ViewState ["strparam"] = dystr; (record query conditions)

When the user clicks download, all data is queried to the datatable with the same conditions.

Hashtable GParam = (Hashtable) ViewState ["GParam"];
Hashtable DPaRam = (Hashtable) ViewState ["DPaRam"];
Hashtable PageParam = (Hashtable) ViewState ["PageParam"];
String strparam = ViewState ["strparam"]. ToString ();

Is to convert the conditions for starting the query back
DataTable dt = OrderMng. OrderList_GetSupOrderList (GParam, DPaRam, PageParam, strparam );

After the same condition is queried, go to the datatable

Step 2: Process datatable, modify column names, sort, and modify the displayed content

Code 1 protected void DataTableTranfer (DataTable dt)
2 {
3 DataColumn dc = null;
4 dt. Columns. Add ("Type", Type. GetType ("System. String "));
5 dt. Columns. Add ("Product Name", Type. GetType ("System. String "));
6 dt. Columns. Add ("settlement status", Type. GetType ("System. String "));
7 foreach (DataRow row in dt. Rows)
8 {
9 if (row ["isSecret"]. ToString () = "0 ")
10 {
11 row ["type"] = "Network order ";
12}
13 else
14 {
15 row ["type"] = "manual order ";
16}
17}
18 row ["Product Name"] = "ID [:" + row ["ProductID"] + "]" + row ["ProductName"];
19 // IsCheckOutk is the method for determining the page
20 row ["settlement status"] = IsCheckOut (row ["CheckOutID"], row ["isrepeal"]);
21
22
23 // modify the column name
24 dt. Columns ["OrderID"]. ColumnName = "Order Number ";
25 dt. Columns ["BuyTime"]. ColumnName = "transaction date ";
26 dt. Columns ["BuyCount"]. ColumnName = "my sales volume ";
27 // sort Columns
28 dt. Columns ["Order Number"]. SetOrdinal (0 );
29 dt. Columns ["type"]. SetOrdinal (1 );
30 dt. Columns ["transaction date"]. SetOrdinal (2 );
31 // remove a column
32 dt. Columns. Remove (dt. Columns ["totalsize"]);
33 dt. Columns. Remove (dt. Columns ["rownum"]);
34
35}

 

Step 3: Convert datatable into excel

Code 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 ();
}
}


Operation completed since

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.