asp.net to import database records into Excel files

Source: Internet
Author: User
Tags import database

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 ();
 }

Related Article

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.