ASP tutorial. NET Import Database tutorial records into Excel files
This is a simple import Excel file code, using exprot.aspx to export the specified database records into an XLS file OH.
protected void button2_click (object sender, EventArgs e)
{
Response.Redirect ("export.aspx?search=" + txtsearch.text);
}
%>
Export.aspx
Gets a DataTable,
Straight Loop DataTable in front of Div:
<div runat= "Server" id= "Exceldiv" >
<table>
</div>
Background:
<%
if (!ispostback)
{
Export ("statement");
}
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.HtmlTextWriter htmlwrite = new HtmlTextWriter (Stringwrite);
Exceldiv.rendercontrol (htmlwrite);
Response.Write (stringwrite.tostring ());
Response.End ();
}