ASP.NET技巧:數(shù)據(jù)島出到Excel最為簡易的方法
只需將ContentType 設(shè)置為 "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技巧:請求網(wǎng)址并解析返回的html
- ASP.NET技巧:做個DataList可分頁的數(shù)據(jù)源
- 調(diào)試ASP.NET應(yīng)用程序的方法和技巧
- ASP.NET技巧:為Blog打造個性日歷
- 幾個ASP.NET技巧
- ASP.NET編程中的十大技巧
- ASP.NET 2.0 URL映射技巧
- asp.net下GDI+的一些常用應(yīng)用(水印,文字,圓角處理)技巧
- ASP.NET User Control使用技巧一則
- ASP.NET 2.0 URL映射技巧
- 幾個 ASP.NET 小技巧
- ASP.NET 小技巧(2個)
- asp.net 開發(fā)的一些常用技巧
- asp.net項目開發(fā)中用到的小技巧
- ASP.net Textbox的技巧使用
- ASP.NET 后臺登錄小技巧介紹
- Asp.Net性能優(yōu)化技巧匯總
- ASP.NET常用小技巧
相關(guān)文章
Entity Framework加載控制Loading Entities
本文詳細講解了Entity Framework加載控制Loading Entities的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03
ASP.NET的HtmlForm控件學習及Post與Get的區(qū)別概述
HtmlForm 控件用于控制form元素,本文主要介紹下HtmlForm控件的Method/Action方法(要提交數(shù)據(jù)的頁面,即數(shù)據(jù)要傳送至哪個網(wǎng)址)及Post與Get的區(qū)別感興趣的朋友可以了解下,或許對你學習HtmlForm控件有所幫助2013-02-02
利用Typings為Visual Studio Code實現(xiàn)智能提示功能
最近在學習Node.js及ThinkJS這個框架,用vscode作為開發(fā)環(huán)境。默認情況下vscode對ThinkJS的代碼提示并不好,所以研究了一下,原來可以同通過Typings來讓vscode擁有強大的智能代碼提示功能。下面本文就介紹了如何利用Typings為Visual Studio Code實現(xiàn)智能提示功能。2017-02-02

