手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >ASP.NET技巧:数据岛出到Excel最为简易的方法
ASP.NET技巧:数据岛出到Excel最为简易的方法
摘要:只需将ContentType设置为"application/vnd.ms-excel",表示以Excel方式输出.代码如下:DataToEx...

只需将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技巧:数据岛出到Excel最为简易的方法】相关文章:

ASP.NET技巧:请求网址并解析返回的html

asp.net实现文件无刷新上传方法汇总

wireshark抓取本地回环数据包和取出数据的方法

ASP.NET弹出消息框、确认框的代码收集

ASP.NET中 Execl导出的六种方法实例

ASP.NET 页面间数据传递方法

ASP.NET列出数据库活跃链接的方法

Asp.net:常见数据导入Excel

ASP.NET 页面刷新的实现方法

基于ASP.NET的数据迁移方法 dbf上传

精品推荐
分类导航