C # export the excle File

Source: Internet
Author: User

Using system;
Using system. configuration;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Using system. IO;
Using system. text;
Public partial class _ default: system. Web. UI. Page
Protected void page_load (Object sender, eventargs E)
Datatable tab = new datatable ();

Tab. Columns. Add ("");
Tab. Columns. Add ("B ");

Datarow row1 = tab. newrow ();
Row1 ["A"] = "cwg ";
Row1 ["B"] = "afsd ";
Tab. Rows. Add (row1 );

Datarow row2 = tab. newrow ();
Row2 ["A"] = "this is Chinese ";
Row2 ["B"] = "this is Chinese ";
Tab. Rows. Add (row2 );
Datarow row3 = tab. newrow ();
Row3 ["A"] = "this is Chinese ";
Row3 ["B"] = "this is Chinese ";
Tab. Rows. Add (row3 );
Datarow row4 = tab. newrow ();
Row4 ["A"] = "this is Chinese ";
Row4 ["B"] = "this is Chinese ";
Tab. Rows. Add (row4 );
Exportexcel1 (Tab );

/// <Summary>
/// Export the exlce table
/// </Summary>
/// <Param name = "ds"> </param>
Private void exportexcel1 (datatable DS)
Response. Clear ();

Response. addheader ("content-disposition", "online?filename=exportname.xls ");

Response. contenttype = "Appilication/vnd. MS-excel ";

// Sets the text output encoding.
Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");

// Response. charset = "";

Stringbuilder builer = new stringbuilder ();

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

Htmltextwriter HTW = new htmltextwriter (SW );

Gridview DG = new gridview ();

DG. datasource = Ds;
DG. databind ();
DG. rendercontrol (HTW );

Response. Write (SW. tostring ());
Response. End ();


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.