How to export an Excel document (Gridview) in asp.net

Source: Internet
Author: User

The main idea is to export the document through the GridView. Create a new ASPX page, the page creates the GridView control, and the background binds the data source.
Then print directly in load to export

The GridView at the front desk

 <asp:gridview id= "GridView1" bordercolor= "Black" runat= "server" autogeneratecolumns= "False" font-size = "12px" width= "656px" allowsorting= "True" height= "172px" > <Columns> <asp:boundfiel
                D datafield= "name" headertext= "name"/> <asp:boundfield datafield= "Sex" headertext= "gender"/> <asp:boundfield datafield= "Birthday" headertext= "date of birth"/> <asp:boundfield "datafield="
                Entid "headertext="/> <asp:boundfield datafield= "Jieduid" headertext= "borrow took"/>
            <asp:boundfield datafield= "familymemberinfo_name1" headertext= "first Guardian name"/> </Columns> 

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

Background:

<summary>///define functions to export Excel///</summary>///<param name= "FileType" ></p aram>///<param name= "FileName" ></param> private void Export (String FileType, String filen AME) {for (int i = 0; i < GridView1.Rows.Count; i++) {//here gives the specified column an edit format that will Digital output is text, preventing digital overflow gridview1.rows[i]. CELLS[3].
            Attributes.Add ("Style", "vnd.ms-excel.numberformat:@");
            } Response.Charset = "GB2312";
            response.contentencoding = System.Text.Encoding.UTF8;
            response.contentencoding = System.Text.Encoding.GetEncoding ("gb2312"); Response.appendheader ("Content-disposition", "attachment;filename=" + httputility.urlencode (filename, Encoding.UTF8).
            ToString ());
            Response.ContentType = FileType; This.
            EnableViewState = false;
            StringWriter tw = new StringWriter (); HtmlTextWriter HW = new HtmlTextWriter (TW);
            Gridview1.rendercontrol (HW); Response.Write (TW.
            ToString ());
        Response.End (); ///<summary>///This method must be overridden, otherwise error///</summary>///<param name= "contr Ol ' ></param> public override void Verifyrenderinginserverform (Control control) {}

Author: Angry Trycatch

Source: http://www.cnblogs.com/likeli/

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.