本页内容是《ASP.NET技巧:数据岛出到Excel最为简易的方法 》同时我们还提供济宁地区的网站建设,百度,雅虎,google的推广,点金,商友,等营销软件
请输入关键字:

标题搜索 内容搜索  

你现在所在的位置->首页->.net->ASP.NET技巧:数据岛出到Excel最为简易的方法

ASP.NET技巧:数据岛出到Excel最为简易的方法

时间:[2006-7-13 10:37:26]     作者:作者不详

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

【声明】本站刊载的《ASP.NET技巧:数据岛出到Excel最为简易的方法 》一文如果有侵害你权益的情况,请联系我们。我们将及时采取措施。
QQ:44637339 Email:just6@163.com Tel:13355163107 Lining studios

友情链接

本页内容是《ASP.NET技巧:数据岛出到Excel最为简易的方法 》 返回顶部
© 2001-2024 Lining studios 济宁速创科技有限公司, All Rights Reserved
 Processed Time:46.875ms
中华人民共和国信息产业部网站备案号: 鲁ICP备09103015号-1