About how to export data sources in Excel (this is) by drawing an HTML, the simplest, understandable, and code,

Source: Internet
Author: User

Private void getexcel ()
{

Userlogin ul = (userlogin) session ["userinfo"];
Dataset DS = new dataset ();
If (infomainclass. enabled)
{
DS = new childrenmanager (). getchildrenlist (keywords. Text, convert. toint32 (infomainclass. selectedvalue ));
}
Else {
DS = new childrenmanager (). getchildrenlist (keywords. Text, ul. childschoolid );
}

Stringbuilder where = new stringbuilder ();


If (! Common. nullcheck. datasetisnull (DS) // determines whether the DS is empty ,,
{
Stringbuilder finalhtml = new stringbuilder ();

Finalhtml. appendline ("<HTML> Finalhtml. appendline ("<style type = \" text/CSS \ "> tr {Height: 25px ;}</style> ");

Finalhtml. append (gettablehtml (DS ));

Finalhtml. appendline ("</body>

Httpresponse resp = httpcontext. Current. response;
Stringbuilder sb = new stringbuilder ();
Resp. contentencoding = system. Text. encoding. utf8;
Resp. contenttype = "application/MS-excel ";
Resp. appendheader ("content-disposition", "attachment?filename=child.xls ");

Resp. Write (finalhtml. tostring ());
Resp. End ();
}
Else
{
Clientscript. registerstartupscript (this. GetType (), "nodata", "alert (\" cannot find qualified students! \ "); Window. Close ();", true );
}
}

String [] Columns = {"school name", "name", "Household Register Type", "certificate number", "gender", "Date of Birth", "ethnicity ", "domicile registration address", "actual residence", "Father's name", "Father's phone number", "Mother's name", "Mother's phone number", "other guardian's name ", "Relationship between other guardians and children", "telephone number of other guardians"
};
String [] colnames = {"school name", "name", "Household Register Type", "certificate number", "gender", "Date of Birth", "nationality ", "domicile registration address", "actual residence", "Father's name", "Father's phone number", "Mother's name", "Mother's phone number", "other guardian's name ", "Relationship between other guardians and children", "telephone number of other guardians"
};
Private string gettablehtml (Dataset DS)
{

Stringbuilder finalhtml = new stringbuilder ();
Finalhtml. appendline ("<Table border = 1 style = \" font-size: 14px; \ "> ");
// Generate the title line
Finalhtml. appendline ("<tr> ");
For (INT I = 0; I <columns. length; I ++)
{
Finalhtml. appendformat ("<TD> {0} </TD>", colnames [I]);
}
Finalhtml. appendline ("</tr> ");
Foreach (datarow DR in DS. Tables [0]. Rows)
{
Finalhtml. appendline ("<tr> ");
For (INT I = 0; I <columns. length; I ++)
{
String colname = columns [I];
Object val = Dr [colname];
// Handle Special Cases
If (colname. Equals ("Date of Birth") | colname. indexof ("date")> = 0)
{
Try
{
Val = convert. todatetime (VAL). tostring ("yyyy-mm-dd ");
}
Catch {}
}

Finalhtml. appendformat ("<TD style = \" Vnd. ms-excel.numberformat: @ \ ">{0} </TD>", Val );
}
Finalhtml. appendline ("</tr> ");
}

Finalhtml. appendline ("</table> ");
Return finalhtml. tostring ();
}

Protected void button#click (Object sender, imageclickeventargs E)
{
Getexcel ();
}

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.