Export Excel Document (Gridview) in ASP.

Source: Internet
Author: User

The main idea is to export the document through the GridView. create a new ASPX page, the page creates a 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:boundfield datafield="Name"headertext="name"/> <asp:boundfield datafield="Sex"headertext="Sex"/> <asp:boundfield datafield="BirthDay"headertext="Date of birth"/> <asp:boundfield datafield="StudentID"headertext="ID Number"/> <asp:boundfield datafield="Jieduid"headertext="borrow took card number"/> <asp:boundfield datafield="familymemberinfo_name1"headertext="First Guardian name"/> </Columns> "Azure"Font-size="12px"horizontalalign="Center"/> <rowstyle horizontalalign="Center"/> <pagerstyle horizontalalign="Center"/> </asp:GridView>
Background:
        /// <summary>        ///define functions to export Excel/// </summary>        /// <param name= "FileType" ></param>        /// <param name= "FileName" ></param>        Private voidExport (stringFileType,stringFileName) {             for(inti =0; i < GridView1.Rows.Count; i++)            {                //This gives the specified column edit format, the digital output as text, to prevent the digital overflowGridview1.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=NewStringWriter (); HtmlTextWriter HW=NewHtmlTextWriter (TW);            Gridview1.rendercontrol (HW); Response.Write (TW.            ToString ());        Response.End (); }        /// <summary>        ///This method must be overridden, or an error will occur/// </summary>        /// <param name= "Control" ></param>         Public Override voidVerifyrenderinginserverform (Control control) {}

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.