Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > C#/.NET技巧

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

来源:中文源码网    浏览:184 次    日期:2024-05-16 15:20:45
【下载文档:  ASP.NET技巧:数据岛出到Excel最为简易的方法.txt 】


ASP.NET技巧:数据岛出到Excel最为简易的方法
只需将ContentType 设置为 "application/vnd.ms-excel",表示以Excel方式输出.代码如下:DataToExcel.aspx:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataToExcel.aspx.cs" Inherits="DataToExcel" %> DataToExcel
DataToExcel.aspx.csusing 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(); } }}

相关内容