随笔 - 71, 文章 - 0, 评论 - 387, 引用 - 16
数据加载中……

数据岛出到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();
        }

    }

}

posted on 2006-06-19 21:56 Ring 阅读(466) 评论(3)  编辑 收藏 所属分类: B .Net 开发

评论

#1楼   回复  引用  查看    

不錯
2006-06-20 09:42 | 自適應軟件......      

#2楼   

|

#3楼   回复  引用    

@大豆
this.Response.ContentType = "application/vnd.ms-excel";
2006-06-20 10:21 | ring1      



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 429919


相关文章:

相关链接: