源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-06-21 00:27 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:ASP.NET技巧:数据岛出到Excel最为简易的方法
只需将ContentType 设置为 "application/vnd.ms-excel",表示以Excel方式输出. 代码如下: DataToExcel.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataToExcel.aspx.cs" Inherits="DataToExcel" %> <html xmlns="[url=http://www.w3.org/1999/xhtml]http://www.w3.org/1999/xhtml[/url]"> <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();         }     } }  
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部