色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

ASP.NET技巧:數(shù)據(jù)島出到Excel最為簡(jiǎn)易的方法

只需將ContentType 設(shè)置為 "application/vnd.ms-excel",表示以Excel方式輸出.
代碼如下:
DataToExcel.ASPx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataToExcel.ASPx.cs" Inherits="DataToExcel" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>DataToExcel</title>
</head>
<body>
    <form id="form1" runat="server">
            <ASP:GridView ID="GridView1" runat="server">
            </ASP:GridView>
    </form>
</body>
</html>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();
        }
    }
}

 

AspNet技術(shù)ASP.NET技巧:數(shù)據(jù)島出到Excel最為簡(jiǎn)易的方法,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 资阳市| 清水河县| 孝义市| 临清市| 温宿县| 静乐县| 仙桃市| 永福县| 陵水| 金阳县| 诸暨市| 农安县| 通道| 咸宁市| 小金县| 林甸县| 武宣县| 安新县| 上栗县| 宜宾市| 新巴尔虎左旗| 石狮市| 揭东县| 萍乡市| 永和县| 酉阳| 肇庆市| 民权县| 磐石市| 南投县| 无棣县| 班玛县| 桃江县| 大兴区| 板桥市| 游戏| 大英县| 兰州市| 朝阳县| 将乐县| 九江县|