Asp.net export Excel and Word

Source: Internet
Author: User

///   <Summary>
/// Export an Excel file
///   </Summary>
///   <Param name = "ds"> Dataset </Param>
///   <Param name = "FILENAME"> Excel file name (excluding the extension) </Param>
Public   Static   Void Toexcel (datatable DT, String Filename)
{
System. Io. stringwriter TW =   New System. Io. stringwriter ();
System. Web. UI. htmltextwriter HW =   New System. Web. UI. htmltextwriter (TW );

Gridview gv =   New Gridview ();
GV. rowdatabound + =   New Gridviewroweventhandler (gv_rowdatabound );
GV. datasource = DT;
GV. databind ();
GV. rendercontrol (HW );

System. Web. httpresponse response = System. Web. httpcontext. Current. response;

Response. Clear ();
Response. charset =   " Gb2312 " ;
Response. contenttype =   " Application/vnd. MS-Excel " ;
Response. appendheader ( " Content-Disposition " , " Attachment; filename = "   + System. Web. httputility. urlencode (filename) +   " . Xls " );
Response. Write ( " <HTML> " Content - Type \ " Content = \ " Text / HTML; charset = Gb2312 \ " > </Head> <body> " );
Response. Write (TW. tostring ());
Response. Write ( " </Body> " );
Response. End ();
HW. Close ();
HW. Flush ();
GV. Dispose ();
Tw. Close ();
Tw. Flush ();
}

/**/ ///   <Summary>
/// Export a word file
///   </Summary>
///   <Param name = "ds"> Dataset </Param>
///   <Param name = "FILENAME"> Word file name (excluding the extension) </Param>
Public   Static   Void Toword (datatable DT, String Filename)
{
System. Io. stringwriter TW =   New System. Io. stringwriter ();
System. Web. UI. htmltextwriter HW =   New System. Web. UI. htmltextwriter (TW );

Gridview gv =   New Gridview ();
GV. rowdatabound + =   New Gridviewroweventhandler (gv_rowdatabound );
GV. datasource = DT;
GV. databind ();
GV. rendercontrol (HW );

System. Web. httpresponse response = System. Web. httpcontext. Current. response;

Response. Clear ();
Response. charset =   " Gb2312 " ;
Response. contenttype =   " Application/MS-word " ;
Response. appendheader ( " Content-Disposition " , " Attachment; filename = "   + System. Web. httputility. urlencode (filename) +   " . Doc " );
Response. Write ( " <HTML> " Content - Type \ " Content = \ " Text / HTML; charset = Gb2312 \ " > </Head> <body> " );
Response. Write (TW. tostring ());
Response. Write ( " </Body> " );
response. end ();
HW. close ();
HW. flush ();
GV. dispose ();
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.