Front-end page:
<% @ Page Language = "C #" enableeventvalidation = "false" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Div id = "print" runat = "server">
<Style type = "text/CSS">
TD
{
Background-color: Gray;
Font-size: Large
}
. Style1
{
Width: 143px;
}
</Style>
<Table id = "Table1" border = "1">
<Tr style = "background-color: Red">
<TD> User Name </TD>
<TD class = "style1" rowspan = "2"> password 22 </TD>
</Tr>
<Tr>
<TD> 22 </TD>
</Tr>
</Table>
</Div>
<Asp: button id = "btn_export" runat = "server" text = "export to excel (default)" onclick = "btn_exportclick"/>
<Asp: button id = "btn_exporttoexcelpaging" runat = "server" text = "export to excel paging" onclick = "btn_exportexcelpaging"/>
</Div>
</Form>
</Body>
</Html>
Background code:
Protected void btn_exportclick (Object sender, eventargs E)
{
Toexcel (print, "DD", true );
// String style = @ "<style>. Text {MSO-number-format :\@;}</SCRIPT> ";
// Response. clearcontent ();
// Response. addheader ("content-disposition", "attachment; filename=myexcelfile.xls ");
// Response. contenttype = "application/Excel ";
// Stringwriter Sw = new stringwriter ();
// Htmltextwriter HTW = new htmltextwriter (SW );
// Print. rendercontrol (HTW );
//// Gvusers. rendercontrol (HTW );
/// Style is added dynamically
// Response. Write (style );
// Response. Write (SW. tostring ());
// Response. End ();
}
Protected void btn_exportexcelpaging (Object sender, eventargs E)
{
// Disablecontrols (gvusers );
// Response. clearcontent ();
// Response. addheader ("content-disposition", "attachment; filename=myexcelfile.xls ");
// Response. contenttype = "application/Excel ";
// Stringwriter Sw = new stringwriter ();
// Htmltextwriter HTW = new htmltextwriter (SW );
// Gvusers. rendercontrol (HTW );
// Response. Write (SW. tostring ());
// Response. End ();
}
Public override void verifyrenderinginserverform (Control)
{
}
Public void toexcel (Control, string filename, bool unformat)
{
System. Io. stringwriter Tw = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter hW = new system. Web. UI. htmltextwriter (TW );
Control. rendercontrol (HW );
System. Web. httpresponse response = system. Web. httpcontext. Current. response;
Response. Write (control. tostring ());
Response. Clear ();
Response. charset = "UTF-8 ";
Response. contentencoding = system. Text. encoding. default;
Response. contenttype = "application/vnd. MS-excel ";
Response. appendheader ("content-disposition", "attachment; filename =" + system. Web. httputility. urlencode (filename) + ". xls ");
Response. write ("<HTML> String output = TW. tostring ();
If (unformat)
{
//-12-17 qianwt uses regular expressions to remove the link format and image format in HTML
Output = RegEx. Replace (output, @ "<\ s */? \ S * A [^>] *> "," ", regexoptions. Compiled | regexoptions. ignorecase );
Output = RegEx. Replace (output, @ "<\ s */? \ S * IMG [^>] *> "," ", regexoptions. Compiled | regexoptions. ignorecase );
}
Response. Write (output );
Response. Write ("</body> Response. End ();
HW. Close ();
HW. Flush ();
Tw. Close ();
Tw. Flush ();
}
}