程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 數據島出到Excel最為簡易的方法

數據島出到Excel最為簡易的方法

編輯:.NET實例教程


只需將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();
        }
    }
}
http://ring1981.cnblogs.com/archive/2006/06/19/429919.Html

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved