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

 更新时间:2006年09月28日 00:00:00   作者:  

只需将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多文件上传实例讲解

    asp.net多文件上传实例讲解

    这篇文章主要为大家详细介绍了asp.net多文件上传,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • Entity Framework加载控制Loading Entities

    Entity Framework加载控制Loading Entities

    本文详细讲解了Entity Framework加载控制Loading Entities的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • ASP.NET的HtmlForm控件学习及Post与Get的区别概述

    ASP.NET的HtmlForm控件学习及Post与Get的区别概述

    HtmlForm 控件用于控制form元素,本文主要介绍下HtmlForm控件的Method/Action方法(要提交数据的页面,即数据要传送至哪个网址)及Post与Get的区别感兴趣的朋友可以了解下,或许对你学习HtmlForm控件有所帮助
    2013-02-02
  • 利用Typings为Visual Studio Code实现智能提示功能

    利用Typings为Visual Studio Code实现智能提示功能

    最近在学习Node.js及ThinkJS这个框架,用vscode作为开发环境。默认情况下vscode对ThinkJS的代码提示并不好,所以研究了一下,原来可以同通过Typings来让vscode拥有强大的智能代码提示功能。下面本文就介绍了如何利用Typings为Visual Studio Code实现智能提示功能。
    2017-02-02
  • 微信公众号支付(MVC版本)

    微信公众号支付(MVC版本)

    这篇文章主要为大家详细介绍了微信公众号支付,提供MVC版本,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • ASP.NET图片上传实例(附源码)

    ASP.NET图片上传实例(附源码)

    这篇文章主要介绍了一个ASP.NET图片上传实例,文章还为大家提供了ASP.NET图片自动上传和局部刷新显示的源码,希望大家喜欢
    2015-11-11
  • c# 执行事务函数代码

    c# 执行事务函数代码

    c#下 执行多条sql语句,实现事务
    2009-05-05
  • a.sp.net清除ListBox的列表项(删除所有项目)

    a.sp.net清除ListBox的列表项(删除所有项目)

    在网上搜索相关资料,相当多用户有相同要求,一次移除ListBox的列表所有项
    2012-01-01
  • C#中使用SQLite数据库的方法介绍

    C#中使用SQLite数据库的方法介绍

    SQLite是一个开源的轻量级的桌面型数据库,它将几乎所有数据库要素(包括定义、表、索引和数据本身)都保存在一个单一的文件中。SQLite用C编写实现,它在内存消耗、文件体积、操作性能、简单性方面都有不错的表现
    2012-01-01
  • 详解ASP.NET MVC 常用扩展点:过滤器、模型绑定

    详解ASP.NET MVC 常用扩展点:过滤器、模型绑定

    本篇文章主要介绍了详解ASP.NET MVC 常用扩展点:过滤器、模型绑定,非常具有实用价值,需要的朋友可以参考下
    2017-05-05

最新评论