Export data from gridview to excel (processing data such as ID card)

Source: Internet
Author: User

// The following is a demo

Protected void page_load (Object sender, eventargs E)

{
Datatable dt = createtable ();
// Fill in data
Initialdatatable (DT );
// Bind
Binddata (DT );
}

Private void binddata (datatable DT)
{
Grdinfo. datasource = DT;
Grdinfo. databind ();
}

Private void initialdatatable (datatable DT)
{
For (INT I = 0; I <100; I ++)
{
Datarow DR = DT. newrow ();
Dr ["name"] = "name" + I. tostring ("000 ");
Dr ["Age"] = I. tostring ();
Dr ["gender"] = "male ";
Dr ["ID card"] = "123456789012345" + I. tostring ("000 ");
DT. Rows. Add (DR );
}
}

Private datatable createtable ()
{
Datatable dttemp = new datatable ();
Datacolumn clmxm = new datacolumn ("name", typeof (system. String ));
Dttemp. Columns. Add (clmxm );
Datacolumn clmnl = new datacolumn ("Age", typeof (system. String ));
Dttemp. Columns. Add (clmnl );
Datacolumn clmxb = new datacolumn ("gender", typeof (system. String ));
Dttemp. Columns. Add (clmxb );
Datacolumn clmsfz = new datacolumn ("ID card", typeof (system. String ));
Dttemp. Columns. Add (clmsfz );
Return dttemp;
}

Protected void btnexport_click (Object sender, eventargs E)
{
// You can use either of the following methods to export data:
// Method 1.
/* When using the following method to export data in the form of ID card to excel, you need to add a style to the cell data of the ID card before export
Foreach (gridviewrow DG in grdinfo. Rows)
{
DG. cells [3]. Attributes. Add ("style", "Vnd. ms-excel.numberformat :@;");
String A = DG. cells [3]. text;
}
Or
Protected void grdinfo_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
// 3 indicates the ID card Column
E. Row. cells [3]. Attributes. Add ("style", "Vnd. ms-excel.numberformat :@;");
}
}
*/
// The black border is displayed in the data area, and the rest are blank.
Export (grdinfo, "application/vnd. MS-excel", "data.xls ");

// Method 2
/*
If you use the following method to export data in the form of ID card to excel
The following statement cannot be used
Foreach (gridviewrow DG in grdinfo. Rows)
{
DG. cells [3]. Attributes. Add ("style", "Vnd. ms-excel.numberformat :@;");
String A = DG. cells [3]. text;
}
Or
Protected void grdinfo_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
// 3 indicates the ID card Column
E. Row. cells [3]. Attributes. Add ("style", "Vnd. ms-excel.numberformat :@;");
}
}
Style needs to be added to the <TD> label <TD style = vnd. ms-excel.numberformat: @>
*/
// Same as the default Excel file, it has a default grid.
Export1 (grdinfo, "", "data.xls ");
}

Public void Export (system. Web. UI. Control source, string filetype, string filename)
{
Httpresponse response = httpcontext. Current. response;
Response. Clear ();
Response. Buffer = true;
Response. charset = "UTF-8"; // default-Value
Response. contentencoding = system. Text. encoding. utf7;
Response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename, system. Text. encoding. utf8). tostring ());
Response. contenttype = filetype;
Source. Page. enableviewstate = false;
System. Io. stringwriter Sw = new system. Io. stringwriter ();
Htmltextwriter hW = new htmltextwriter (SW );
Source. rendercontrol (HW );
Response. Write (SW. tostring ());
Response. End ();
}
Public static void export1 (gridview source, string filetype, string filename)
{
If (source. Rows. Count> 0)
{
Httpcontext CTX = httpcontext. Current;
Stringwriter Sw = new stringwriter ();
Sw. writeline ("Sw. writeline ("<HTML xmlns: x = \" urn: Schemas-Microsoft-com: Office: Excel \ "> ");
Sw. writeline ("Sw. writeline ("<! -- [If gte mso 9]> ");
Sw. writeline ("<XML> ");
Sw. writeline ("<X: excelworkbook> ");
Sw. writeline ("<X: excelworksheets> ");
Sw. writeline ("<X: excelworksheet> ");
Sw. writeline ("<X: Name> data </X: Name> ");
Sw. writeline ("<X: worksheetoptions> ");
Sw. writeline ("<X: Print> ");
Sw. writeline ("<X: validprinterinfo/> ");
Sw. writeline ("</X: Print> ");
Sw. writeline ("</X: worksheetoptions> ");
Sw. writeline ("</X: excelworksheet> ");
Sw. writeline ("</X: excelworksheets> ");
Sw. writeline ("</X: excelworkbook> ");
Sw. writeline ("</XML> ");
Sw. writeline ("<! [Endif] --> ");
Sw. writeline ("Sw. writeline ("<body> ");
Sw. writeline ("<Table> ");
For (INT I = 0; I <source. Rows. Count; I ++)
{
If (I = 0)
{
Sw. writeline ("<tr> ");
For (Int J = 0; j <source. Columns. Count; j ++)
{
Sw. writeline ("<TD> <strong>" + source. Columns [J]. headertext + "</strong> </TD> ");
}
Sw. writeline ("</tr> ");
}
Sw. writeline ("<tr> ");
For (Int J = 0; j <source. Columns. Count; j ++)
{
// Sw. writeline ("<TD>" + source. Rows [I]. cells [J]. Text + "</TD> ");
// J = 3 indicates the ID card Column
If (j = 3)
{
// Add this sentence, like ID card needs to add style = vnd. ms-excel.numberformat: @ this sentence,
// Otherwise, data is displayed in scientific notation
Sw. writeline ("<TD style = vnd. ms-excel.numberformat: @>" + source. Rows [I]. cells [J]. Text + "</TD> ");
}
Else
{Sw. writeline ("<TD>" + source. Rows [I]. cells [J]. Text + "</TD> ");}
}
Sw. writeline ("</tr> ");
}
Sw. writeline ("</table> ");
Sw. writeline ("</body> ");
Sw. writeline ("Sw. Close ();
CTX. response. Clear ();
CTX. response. Buffer = true;
CTX. response. charset = "UTF-8 ";
Source. Page. enableviewstate = false;
CTX. response. addheader ("content-disposition", "attachment; filename =" + filename + "");
CTX. response. contenttype = "application/MS-excel ";
CTX. response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
CTX. response. Write (SW );
CTX. response. End ();
}
}

Public override void verifyrenderinginserverform (Control)
{
}

// You can set the style in method 1.
Protected void grdinfo_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
// 3 indicates the ID card Column
E. Row. cells [3]. 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.