Asp.net imports database records into excel files. This is a simple code for importing excel files. Use exprot. aspx to export records from a specified database to xls files.
Asp tutorial. net import database tutorial records into an excel file
This is a simple excel File Import code. Use exprot. aspx to export records from a specified database into xls files.
Protected void button2_click (object sender, eventargs e)
{
Response. redirect ("export. aspx? Search = "+ txtsearch. text );
}
%>
Export. aspx
Get a datatable,
Directly loop the datatable In the div at the front end:
<Div runat = "server" id = "exceldiv">
<Table>
</Div>
Background:
<%
If (! Ispostback)
{
Export ("Report ");
}
Private void export (string filename)
{
Response. clear ();
Response. addheader ("content-disposition", "attachment; filename =" + filename + ". xls ");
Response. charset = "gb2312 ";
Response. contenttype = "application/vnd.xls ";
Response. contentencoding = system. text. encoding. getencoding ("gb2312 ");
System. io. stringwriter stringwrite = new system. io. stringwriter ();
System.web.ui.html textwriter htmlwrite = new htmltextwriter (stringwrite );
Exceldiv. rendercontrol (htmlwrite );
Response. write (stringwrite. tostring ());
Response. end ();
}