只需将ContentType 设置为 “application/vnd.ms-excel”,表示以Excel方式输出. <html xmlns=”http://www.w3.org/1999/xhtml“> public partial class DataToExcel : System.Web.UI.Page
代码如下:
DataToExcel.aspx:
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”DataToExcel.aspx.cs” Inherits=”DataToExcel” %>
<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;
{
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
数据岛出到excel最为简易的方法_asp.net技巧
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 数据岛出到excel最为简易的方法_asp.net技巧
相关推荐
-      对.net framework 反射的反思_asp.net技巧
-      .net3.5和vs2008中的asp.net ajax_asp.net技巧
-      使用asp.net ajax框架扩展html map控件_asp.net技巧
-      asp.net应用程序资源访问安全模型_asp.net技巧
-      photoshop初学者轻松绘制螺旋漩涡特效_photoshop教程
-      photoshop通道结合图层模式抠狗尾巴草_photoshop教程
-      web.config详解+asp.net优化_asp.net技巧
-      asp.net中多彩下拉框的实现_asp.net技巧