ASP. NET 2.0, C # -- use the gridview control to export other files (export Excel files and export Word files)

Source: Internet
Author: User

// Note: if you use the gridview to export files on the visual studio2005 platform,

// The verifyrenderinginserverform method must be reloaded.

Public override void verifyrenderinginserverform (Control)

{

}

 

/// <Summary>

/// Method for exporting data to a file,

/// </Summary>

/// <Param name = "model"> model = 1: Export to execl, model = 2: Export to word </param>

Private void tofiles (INT Model)

{

String strfilename = datetime. Now. tostring ("yyyymmdd-hhmmss ");

System. Web. httpcontext Hc = system. Web. httpcontext. Current;

HC. response. Clear ();

HC. response. Buffer = true;

HC. response. contentencoding = system. Text. encoding. utf8; // set the output stream to simplified Chinese

 

If (model = 1)

{

// --- Export as an Excel file

HC. response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (strfilename, system. Text. encoding. utf8) + ". xls ");

HC. response. contenttype = "application/MS-excel"; // set the output file type to an Excel file.

}

Else

{

// --- Export as a Word file

HC. response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (strfilename, system. Text. encoding. utf8) + ". Doc ");

HC. response. contenttype = "application/MS-word"; // set the output file type to word.

}

 

System. Io. stringwriter Sw = new system. Io. stringwriter ();

System. Web. UI. htmltextwriter HTW = new system. Web. UI. htmltextwriter (SW );

This. gridview1.rendercontrol (HTW );

 

HC. response. Write (SW. tostring ());

HC. response. End ();

}

//-Export as an Excel file

Protected void toexecl_click (Object sender, eventargs E)

{

Tofiles (1 );

}

//-Export as a Word file

Protected void button#click (Object sender, eventargs E)

{

Tofiles (2 );

}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/bl_song/archive/2009/07/13/4344895.aspx

Related Article

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.