The user control of the datagridview into Excel

Source: Internet
Author: User

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

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.