数据岛出到Excel最为简易的方法
作者:互联网
2009-06-30
只需将ContentType 设置为 "application/vnd.ms-excel",表示以Excel方式输出.
代码如下:
DataToExcel.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataToExcel.aspx.cs" Inherits="DataToExcel" %>
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();
}
}
}
相关标签:
相关推荐
专题
+ 收藏
+ 收藏
+ 收藏
+ 收藏
+ 收藏
+ 收藏
最新数据
相关文章
详解.NET Core如何构建一个弹性的HTTP请求机制
.NET Core使用Redis实现创建分布式锁
.NET内存管理释放的两种方式
基于.NET8实现WinFrom应用窗口自动缩放功能
浅析如何在 ASP.NET Core中实现速率限制
.NET Core 委托原理解析(最新推荐)
.NET 9 new features-Microsoft.ML.Tokenizers 库(文本标记化功能)
如何在 .NET 中使用 Tesseract 识别图片文字
ASP.NET Core Web API之Token验证的实现
asp.net core web api项目添加自定义中间件的实现
AI精选
