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 );