ASP.NET技巧:数据岛出到Excel最为简易的方法
只需将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技巧:教你制做Web实时进度条
- ASP.NET技巧:请求网址并解析返回的html
- ASP.NET技巧:做个DataList可分页的数据源
- 调试ASP.NET应用程序的方法和技巧
- ASP.NET技巧:为Blog打造个性日历
- 几个ASP.NET技巧
- ASP.NET编程中的十大技巧
- ASP.NET 2.0 URL映射技巧
- asp.net下GDI+的一些常用应用(水印,文字,圆角处理)技巧
- ASP.NET User Control使用技巧一则
- ASP.NET 2.0 URL映射技巧
- 几个 ASP.NET 小技巧
- ASP.NET 小技巧(2个)
- asp.net 开发的一些常用技巧
- asp.net项目开发中用到的小技巧
- ASP.net Textbox的技巧使用
- ASP.NET 后台登录小技巧介绍
- Asp.Net性能优化技巧汇总
- ASP.NET常用小技巧
相关文章
Entity Framework加载控制Loading Entities
本文详细讲解了Entity Framework加载控制Loading Entities的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-03-03
ASP.NET的HtmlForm控件学习及Post与Get的区别概述
HtmlForm 控件用于控制form元素,本文主要介绍下HtmlForm控件的Method/Action方法(要提交数据的页面,即数据要传送至哪个网址)及Post与Get的区别感兴趣的朋友可以了解下,或许对你学习HtmlForm控件有所帮助2013-02-02
利用Typings为Visual Studio Code实现智能提示功能
最近在学习Node.js及ThinkJS这个框架,用vscode作为开发环境。默认情况下vscode对ThinkJS的代码提示并不好,所以研究了一下,原来可以同通过Typings来让vscode拥有强大的智能代码提示功能。下面本文就介绍了如何利用Typings为Visual Studio Code实现智能提示功能。2017-02-02


最新评论