广

ASP.NET

  • IOS开发
  • android开发
  • PHP编程
  • JavaScript
  • ASP.NET
  • ASP编程
  • JSP编程
  • Java编程
  • 易语言
  • Ruby编程
  • Perl编程
  • AJAX
  • 正则表达式
  • C语言
  • 编程开发

    数据岛出到Excel最为简易的方法

    2018-04-13 09:17:46 次阅读 稿源:互联网
    广告

      只需将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();
              }
          }
      }
      http://ring1981.cnblogs.com/archive/2006/06/19/429919.html

    一起学吧部分文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与一起学吧进行文章共享合作。

    广告
    广告
    广告