[Reprinted] repeater exports data to excel

Source: Internet
Author: User

 

Repeater exports data to excel

 

Method 1:

Stringwriter Sw = new stringwriter ();
Htmltextwriter HTW = new htmltextwriter (SW );
String attachment = "attachment; filename=myreport.xls ";
Response. clearcontent ();
Response. addheader ("content-disposition", attachment );
Response. contenttype = "application/octet-stream ";
Repmessage. rendercontrol (HTW );
Response. Write (SW. tostring ());
Response. Flush ();
Response. End ();

 

Method 2:

Stringbuilder sb = new stringbuilder ();
Stringwriter Sw = new stringwriter (SB );
Htmltextwriter HTW = new htmltextwriter (SW );

Page page = new page ();
Htmlform form = new htmlform ();

Repeater1. Enableviewstate = false;

Page. enableeventvalidation = false;

Page. designerinitialize ();

Page. Controls. Add (form );
Form. Controls. Add(Repeater1);

Page. rendercontrol (HTW );

Response. Clear ();
Response. Buffer = true;
Response. contenttype = "application/MS-excel ";
Response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode ("test.xls", encoding. utf8). tostring ());
Response. charset = "UTF-8 ";
Response. contentencoding = encoding. default;
Response. Write (sb. tostring ());
Response. End ();

 

 

Export data to excel by PAGE Repeater

  

 

1. When exporting data in repeater, the exported data must be a complete data block, for example, a complete table.

2. If the exported number is displayed in scientific notation, add the style: Style = "Vnd. ms-excel.numberformat: @" to the column.

3. Repeater:Code(C #):

Public static void reporttoexcel (system. Web. UI. Control CTL)
{
Httpcontext. Current. response. contentencoding = system. Text. encoding. utf8;
Httpcontext. Current. response. contenttype = "application/MS-excel ";
Httpcontext. Current. response. charset = "gb2312 ";
Httpcontext. current. response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode ("hello.xls", system. text. encoding. utf8 ). tostring ());
Stringwriter Sw = new stringwriter ();
Htmltextwriter HTW = new htmltextwriter (SW );
CTL. rendercontrol (HTW );
Httpcontext. Current. response. Write (SW. tostring ());
Httpcontext. Current. response. End ();
}

4. If pagination is supported and the page is not resend, use the following method:

String where = string. Format ("{0} And paystate = 1", wheresql); // spell the where condition again as needed
Datatable dt = function. paydesigner. getpaydesignerlist (where, ordersql, 1, recordcount); // recordcount indicates the total number of records.
Repeater1.datasource = DT;
Repeater1.databind ();
Reporttoexcel (repeater1 );

 

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.