ASP.NET技巧:數(shù)據(jù)島出到Excel最為簡(jiǎn)易的方法
只需將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實(shí)時(shí)進(jìn)度條
- ASP.NET技巧:請(qǐng)求網(wǎng)址并解析返回的html
- ASP.NET技巧:做個(gè)DataList可分頁的數(shù)據(jù)源
- 調(diào)試ASP.NET應(yīng)用程序的方法和技巧
- ASP.NET技巧:為Blog打造個(gè)性日歷
- 幾個(gè)ASP.NET技巧
- ASP.NET編程中的十大技巧
- ASP.NET 2.0 URL映射技巧
- asp.net下GDI+的一些常用應(yīng)用(水印,文字,圓角處理)技巧
- ASP.NET User Control使用技巧一則
- ASP.NET 2.0 URL映射技巧
- 幾個(gè) ASP.NET 小技巧
- ASP.NET 小技巧(2個(gè))
- asp.net 開發(fā)的一些常用技巧
- asp.net項(xiàng)目開發(fā)中用到的小技巧
- ASP.net Textbox的技巧使用
- ASP.NET 后臺(tái)登錄小技巧介紹
- Asp.Net性能優(yōu)化技巧匯總
- ASP.NET常用小技巧
相關(guān)文章
VisualStudio2019中為.NET Core WinForm App啟用窗體設(shè)計(jì)器
這篇文章主要介紹了VisualStudio2019中為.NET Core WinForm App啟用窗體設(shè)計(jì)器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Asp.Net Core WebAPI使用Swagger時(shí)API隱藏和分組詳解
這篇文章主要給大家介紹了關(guān)于Asp.Net Core WebAPI使用Swagger時(shí)API隱藏和分組的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Asp.Net Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04ASP.NET Core中快速構(gòu)建PDF文檔的步驟分享
這篇文章主要給大家介紹了關(guān)于ASP.NET Core中快速構(gòu)建PDF文檔的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12一步步打造簡(jiǎn)單的MVC電商網(wǎng)站BooksStore(2)
這篇文章主要和大家一起一步步打造一個(gè)簡(jiǎn)單的MVC電商網(wǎng)站,MVC電商網(wǎng)站BooksStore第二篇,添加分類導(dǎo)航、加入購(gòu)物車,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04