ASP. NET: the easiest way to export data islands to excel

Source: Internet
Author: User

You only need to set contenttype to "application/vnd. MS-excel", indicating that the data is output in Excel mode.
CodeAs follows:
Datatoexcel. aspx:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "datatoexcel. aspx. cs" inherits = "datatoexcel" %>



datatoexcel







datatoexcel. 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. data. sqlclient;

Public partial class datatoexcel: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! This. ispostback)
{
This. response. contenttype = "application/vnd. MS-excel ";
String connstr = "Server = localhost; uid = sa; Pwd =; database = northwind ";
Sqlconnection conn = new sqlconnection (connstr );
Conn. open ();
String sqlcmd = "select lastname, firstname, title, address, city from employees ";
Sqlcommand cmd = new sqlcommand (sqlcmd, Conn );
Sqldataadapter adapter = new sqldataadapter (CMD );
Dataset DS = new dataset ();
Adapter. Fill (DS );
This. gridview1.datasource = Ds. Tables [0]. defaultview;
This. gridview1.databind ();
}
}
}

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.