Export data of the DataGridView to Excel on the Asp.net webpage

Source: Internet
Author: User

1. Export data directly from the DataGridView to an Excel file and search for information online. Finally, find a method to export data directly from the GridView to an Excel file. The induction method is as follows: 1. Note: if the character set format is changed to "GB2312", some exported data may be garbled. Disable the paging part before export. If only the homepage data is exported on the page; ///// download data ///// file type // Excel table name private void Excel (string FileType, string FileName) {try {Response. clear (); Response. buffer = true; Response. charset = "UTF-8"; // return the data Response associated with the specified code page. contentEncoding = System. text. encoding. getEncoding ("UTF-8 "); // Attachment indicates downloading as an attachment, and filename specifies the output file name Response. appendHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. urlEncode (FileName, Encoding. UTF8 ). toString (); // specify the file type Response. contentType = FileType; this. enableViewState = false; System. globalization. cultureInfo myCItrad = new System. globalization. cultureInfo ("ZH-CN", true); // defines an input stream StringWriter tw = new StringWriter (myCItrad); HtmlText Writer hw = new HtmlTextWriter (tw); this. gvJstList. renderControl (hw); Response. write (tw. toString (); Response. end ();} catch (Exception err) {ScriptManager. registerStartupScript (this, this. getType (), "js", "showInfo ('ctl00 _ Contentplaceholder2_ValidationSummary1 ', 1,' error:" + err. message. replace ("\ r \ n", "\ r \ n "). replace ("'", "'") + "')", true); return ;}} if the code above is used to export a general GridView, it should be okay, but if GridVie The AutoGenerateDeleteButton, AutoGenerateEditButton, and AutoGenerateSelectButton values of w are set to True, or the GridView contains fields of the HyperLinkField type, the "XXXX" control, for example, "type" GridView ", must be placed in the form tag with runat = server." Exception! The solution is to Override the VerifyRenderingInServerForm method of the WebForm form! The Code is as follows: public override void VerifyRenderingInServerForm (Control control) {// OverRide in order to make it feasible to export to Excel !} 2./* For example, some exported tables have numbers such as column numbers and ID card numbers. If you do not process them, the exported data is a number by default, for example, the original string "0010" is changed to the number 10, and the string "1245787888" is changed to the scientific notation 1.236 + E9, so that the expected results cannot be reached, therefore, you need to add a formatted data type to the corresponding column before export. The following code is formatted as a string */Add the following code in the code above: foreach (GridViewRow dg in this. gridview1.Rows) {dg. cells [0]. attributes. add ("style", "vnd. ms-excel.numberformat: @; "); dg. cells [5]. attributes. add ("style", "vnd. ms-excel.numberformat: @; "); dg. cells [6]. attributes. add ("style", "vnd. ms-excel.numberformat: @; "); dg. cells [8]. attributes. add ("style", "vnd. ms-excel.numberformat: @; ");} or add the following code in the maid event RowDataBound: if (e. row. rowType = DataControlRowType. dataRow) {e. row. cells [3]. attributes. add ("style", "vnd. ms-excel.numberformat: @ "); e. row. cells [4]. attributes. add ("style", "vnd. ms-excel.numberformat :@");}

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.