PS. The solution part is stored in the Program program.
Excel. aspx
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "Excel. aspx. cs" inherits = "Excel" %>
02
03 <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en ""Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <HTML xmlns ="Http://www.w3.org/1999/xhtml">
05 06 <title> untitled faces </title>
07 08 <body>
09 <Form ID = "form1" runat = "server">
10 <div>
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" performanceid = "sqlperformance1">
<Columns>
<Asp: boundfield datafield = "ID" headertext = "ID" insertvisible = "false" readonly = "true"
Sortexpression = "ID"/>
<Asp: boundfield datafield = "gender" headertext = "gender" sortexpression = "gender"/>
</Columns>
</ASP: gridview>
<Asp: button id = "exportexcel" runat = "server" text = "/ Excel "onclick = "exportexcel_click"
Width = "200px"/> <br/>
<Asp: button id = "exportword" runat = "server" text = "extract word into/save word into" onclick = "exportword_click"
Width = "200px"/> <br/>
<Asp: button id = "exporttext" runat = "server" text = "extract TXT example/save TXT example" width = "200px" onclick = "exporttext_click"/>
<Asp: sqldatasource id = "sqldatasource1" runat = "server"
Connectionstring = "<% $ connectionstrings: databaseconnectionstring
%>"
Selectcommand = "select * from [user]"> </ASP: sqldatasource>
</Div>
</Form>
</Body>
</Html>
Excel. aspx. CS
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;
Using system. text;
Using system. IO;
Public partial class Excel: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
Protected void exportexcel_click (Object sender, eventargs E)
{
// Export the Excel worksheet
Response. Clear ();
Response. addheader ("content-disposition", "attachment?filename=test.xls"); // Excel prepare name
Response. contenttype = "application/vnd. MS-excel ";
Response. charset = "";
System. Io. stringwriter Sw = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter HTW = new system. Web. UI. htmltextwriter (SW );
DataGrid DG = new DataGrid ();
DG. datasource = This. sqldatasource1.select (datasourceselectarguments. Empty );
DG. databind ();
DG. rendercontrol (HTW );
Response. Write (SW. tostring ());
Response. End ();
// Save the Excel worksheet
// System. Io. stringwriter Sw = new system. Io. stringwriter ();
// System. Web. UI. htmltextwriter HTW = new system. Web. UI. htmltextwriter (SW );
// DataGrid DG = new DataGrid ();
// DG. datasource = This. sqldatasource1.select (datasourceselectarguments. Empty );
// DG. databind ();
// DG. rendercontrol (HTW );
// System. Io. file. writealltext (@ "C:/test.xls", SW. tostring ());
}
Protected void exportword_click (Object sender, eventargs E)
{
// Extract the word
Response. Clear ();
Response. addheader ("content-disposition", "attachment?filename=test.doc"); // word alias name
Response. contenttype = "application/vnd. MS-word ";
Response. charset = "";
System. Io. stringwriter Sw = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter HTW = new system. Web. UI. htmltextwriter (SW );
DataGrid DG = new DataGrid ();
DG. datasource = This. sqldatasource1.select (datasourceselectarguments. Empty );
DG. databind ();
DG. rendercontrol (HTW );
Response. Write (SW. tostring ());
Response. End ();
// Save the word watermark
// System. Io. stringwriter Sw = new system. Io. stringwriter ();
// System. Web. UI. htmltextwriter HTW = new system. Web. UI. htmltextwriter (SW );
// DataGrid DG = new DataGrid ();
// DG. datasource = This. sqldatasource1.select (datasourceselectarguments. Empty );
// DG. databind ();
// DG. rendercontrol (HTW );
// System. Io. file. writealltext (@ "C:/test.doc", SW. tostring ());
}
Protected void exporttext_click (Object sender, eventargs E)
{
// Extract the TXT token
Response. Clear ();
Response. addheader ("content-disposition", "attachment=filename=test.txt"); // TXT handler name
Response. contenttype = "application/vnd. MS-word ";
Response. charset = "";
System. Text. stringbuilder sb = new system. Text. stringbuilder ();
Datatable dt = new datatable ();
Dt = (dataview) This. sqlperformance1.select (cesceselectarguments. Empty). totable ();
// Delimiter
For (INT I = 0; I <DT. Columns. Count; I ++)
{
SB. append (Dt. Columns [I]. columnname );
SB. append (",");
}
SB. append (environment. newline );
// Information
For (INT I = 0; I <DT. Rows. Count; I ++)
{
For (Int J = 0; j <DT. Columns. Count; j ++)
{
SB. append (Dt. Rows [I] [J]. tostring ());
SB. append (",");
}
SB. append (environment. newline );
}
Response. Write (sb. tostring ());
Response. End ();
// Save the TXT example
// System. Text. stringbuilder sb = new system. Text. stringbuilder ();
// Datatable dt = new datatable ();
// DT = (dataview) This. sqlperformance1.select (cesceselectarguments. Empty). totable ();
//// Register a bit name
// For (INT I = 0; I <DT. Columns. Count; I ++)
//...{
// Sb. append (Dt. Columns [I]. columnname );
// Sb. append (",");
//}
// Sb. append (environment. newline );
/// Information
// For (INT I = 0; I <DT. Rows. Count; I ++)
//...{
// For (Int J = 0; j <DT. Columns. Count; j ++)
//...{
// Sb. append (Dt. Rows [I] [J]. tostring ());
// Sb. append (",");
//}
// Sb. append (environment. newline );
//}
// System. Io. file. writealltext (@ "C:/test.txt", SB. tostring ());
}
Protected void button3_click (Object sender, eventargs E)
{
// Export ("application/MS-excel", ".xls"); // output Excel
Export ("application/MS-powerpnt", ".ppt"); // output PowerPoint
}
}