Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public partial class test: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
Databind ();
}
}
Public override void verifyrenderinginserverform (Control)
{
// Base. verifyrenderinginserverform (control );
}
Protected void databind ()
{
Datatable DDT = new datatable ("newexcel ");
DDT. Columns. Add ("A", typeof (string ));
DDT. Columns. Add ("B", typeof (string ));
DDT. Columns. Add ("C", typeof (string ));
Random RO = new random ();
For (INT I = 1; I <2000; I ++)
{
Datarow DR = DDT. newrow ();
Dr ["A"] = "000" + (czi (I ));
String Password = Ro. Next (100,999 9). tostring ();
Dr ["B"] = password;
Dr ["C"] = system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (password, "MD5 ");
DDT. Rows. Add (DR );
}
This. grdexcel. datasource = DDT. defaultview;
This. grdexcel. databind ();
}
Protected string czi (Int J)
{
String JJ = string. empty;
If (j <10)
JJ = "000" + J. tostring ();
Else if (j <99)
JJ = "00" + J. tostring ();
Else if (J & lt; 999)
JJ = "0" + J. tostring ();
Else if (J & lt; 9999)
JJ = J. tostring ();
Return JJ;
}
Protected void button#click (Object sender, eventargs E)
{
Response. Clear ();
Response. Buffer = true;
Response. charset = "gb2312 ";
// The following line is very important. The attachment parameter indicates downloading as an attachment. You can change it to online.
// Filename=fileflow.xls specifies the name of the output file. Note that the extension is consistent with the specified file type. It can be:. Doc |. xls |. txt |. htm
String strheader = "card ";
Response. appendheader ("content-disposition", "attachment; filename =" + strheader + ". xls ");
Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
// Response. contenttype: the specified file type can be application/MS-Excel | application/MS-word | application/MS-TXT | application/MS-HTML | or can be directly supported by other browsers document
Response. contenttype = "application/MS-excel ";
// This. enableviewstate = false;
// 2. Define an input stream
System. Io. stringwriter ostringwriter = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter ohtmltextwriter = new system. Web. UI. htmltextwriter (ostringwriter );
// 3. Bind the target data to the input stream output
// Ohtmltextwriter. writeencodedtext (server. urldecode (strheader ));
Grdexcel. rendercontrol (ohtmltextwriter );
// This indicates that the current page is output. You can also bind the DataGrid or other controls that support the obj. rendercontrol () attribute.
response. write (ostringwriter. tostring ();
response. end ();
}< br> protected void grdexcel_rowdatabound (Object sender, gridviewroweventargs e)
{< br>
E. row. cells [0]. attributes. add ("style", "Vnd. ms-excel.numberformat: @ ");
E. row. cells [1]. attributes. add ("style", "Vnd. ms-excel.numberformat: @ ");
E. row. cells [2]. attributes. add ("style", "Vnd. ms-excel.numberformat: @ ");
}< BR >}< br>