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

ASP.NET技巧:數據島出到Excel最為簡易的方法

只需將ContentType 設置為 "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技術ASP.NET技巧:數據島出到Excel最為簡易的方法,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 克山县| 虞城县| 会昌县| 遂川县| 杂多县| 江山市| 华池县| 苏尼特左旗| 芮城县| 嫩江县| 莱芜市| 陵川县| 合阳县| 平顶山市| 普宁市| 五大连池市| 乐东| 义马市| 乐清市| 黎川县| 黄骅市| 东明县| 会昌县| 临高县| 海口市| 泽库县| 寿光市| 安溪县| 灵武市| 武乡县| 昭通市| 天镇县| 九龙城区| 神木县| 赣州市| 项城市| 盐源县| 方正县| 昭觉县| 工布江达县| 桐柏县|