Export the gridview to a summary of Excel, world, and PDF

Source: Internet
Author: User
Below is a summary of how to export the data of the gridview to excel, world, and PDF.

1. Export to word

Response. addheader ("content-disposition", "attachment?filename=export.doc ");

Response. cache. setcacheability (httpcacheability. nocache );

Response. contenttype = "application/vnd. Word ";

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

System. Web. UI. htmltextwriter htmlwrite = new htmltextwriter (stringwrite );

// Create a form to contain the grid

Htmlform FRM = new htmlform ();

GV. Parent. Controls. Add (FRM );

FRM. attributes ["runat"] = "server ";

FRM. Controls. Add (GV );

FRM. rendercontrol (htmlwrite );

// Gridview1.rendercontrol (HTW );

Response. Write (stringwrite. tostring ());

Response. End ();

2. Export to excel

String attachment = "attachment; filename=export.xls ";

Response. clearcontent ();

Response. addheader ("content-disposition", attachment );

Response. contenttype = "application/MS-excel ";

Stringwriter Sw = new stringwriter ();

Htmltextwriter HTW = new htmltextwriter (SW );

// Create a form to contain the grid

Htmlform FRM = new htmlform ();

GV. Parent. Controls. Add (FRM );

FRM. attributes ["runat"] = "server ";

FRM. Controls. Add (GV );

FRM. rendercontrol (HTW );

// Gridview1.rendercontrol (HTW );

Response. Write (SW. tostring ());

Response. End ();

3. Export to PDF

Now we need to use the open-source itext.

Using itextsharp. text;

Using itextsharp.text.pdf;

Using itextsharp.text.html;

Using itextsharp.text.html. simpleparser;

Response. contenttype = "application/pdf ";

Response. addheader ("content-disposition", "attachment?filename=export= ");

Response. cache. setcacheability (httpcacheability. nocache );

Stringwriter Sw = new stringwriter ();

Htmltextwriter hW = new htmltextwriter (SW );

Htmlform FRM = new htmlform ();

GV. Parent. Controls. Add (FRM );

FRM. attributes ["runat"] = "server ";

FRM. Controls. Add (GV );

FRM. rendercontrol (HW );

Stringreader sr = new stringreader (SW. tostring ());

Document required Doc = new document (pagesize. A4, 10f, 10f, 10f, 0f );

Htmlworker htmlparser = new htmlworker (workflow DOC );

Response writer. getinstance (Response doc, response. outputstream );

Using Doc. open ();

Htmlparser. parse (SR );

Using Doc. Close ();

Response. Write (Response DOC );

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.