日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

DataGrid 完全攻略之二(把数据导出到Excel)

發(fā)布時(shí)間:2024/10/8 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DataGrid 完全攻略之二(把数据导出到Excel) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
前臺(tái)代碼:html
<%@?Page?language="c#"?Codebehind="ExportExcel.aspx.cs"?AutoEventWireup="false"?Inherits="MsDataGrid.ExportExcel"?%>
<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.0?Transitional//EN"?>
<HTML>
????
<HEAD>
????????
<title>DataGrid使用舉例</title>
????????
<meta?name="GENERATOR"?Content="Microsoft?Visual?Studio?7.0">
????????
<meta?name="CODE_LANGUAGE"?Content="C#">
????????
<meta?name="vs_defaultClientScript"?content="JavaScript">
????????
<meta?name="vs_targetSchema"?content="http://schemas.microsoft.com/intellisense/ie5">
????
</HEAD>
????
<body?MS_POSITIONING="GridLayout">
????????
<form?id="Form1"?method="post"?runat="server">
????????????
<FONT?face="宋體">
????????????????
<asp:DataGrid?id="dgShow"?style="Z-INDEX:?100;?LEFT:?26px;?POSITION:?absolute;?TOP:?89px"?runat="server"?Width="842px"?Height="172px"?BorderColor="Tan"?BorderWidth="1px"?BackColor="LightGoldenrodYellow"?CellPadding="2"?GridLines="None"?ForeColor="Black"?PageSize="1"?AutoGenerateColumns="False"?ShowFooter="True"?AllowPaging="True">
????????????????????
<SelectedItemStyle?ForeColor="GhostWhite"?BackColor="DarkSlateBlue"></SelectedItemStyle>
????????????????????
<AlternatingItemStyle?BackColor="PaleGoldenrod"></AlternatingItemStyle>
????????????????????
<HeaderStyle?Font-Bold="True"?BackColor="Tan"></HeaderStyle>
????????????????????
<FooterStyle?BackColor="Tan"></FooterStyle>
????????????????????
<Columns>
????????????????????????
<asp:BoundColumn?DataField="StudentID"?ReadOnly="True"?HeaderText="學(xué)生ID"></asp:BoundColumn>
????????????????????????
<asp:BoundColumn?DataField="StudentName"?HeaderText="學(xué)生姓名"></asp:BoundColumn>
????????????????????????
<asp:BoundColumn?DataField="StudentPass"?HeaderText="密碼"></asp:BoundColumn>
????????????????????????
<asp:BoundColumn?DataField="Sex"?HeaderText="性別"></asp:BoundColumn>
????????????????????????
<asp:BoundColumn?DataField="Birthday"?HeaderText="生日"?DataFormatString="{0:yyyy-M-d}"></asp:BoundColumn>
????????????????????????
<asp:BoundColumn?DataField="Email"?HeaderText="郵件地址"></asp:BoundColumn>
????????????????????????
<asp:BoundColumn?DataField="Score"?HeaderText="分?jǐn)?shù)"></asp:BoundColumn>
????????????????????????
<asp:ButtonColumn?Text="選擇"?CommandName="Select"></asp:ButtonColumn>
????????????????????
</Columns>
????????????????????
<PagerStyle?HorizontalAlign="Center"?ForeColor="DarkSlateBlue"?BackColor="PaleGoldenrod"?Mode="NumericPages"></PagerStyle>
????????????????
</asp:DataGrid>
????????????????
<asp:Button?id="btnMIME"?style="Z-INDEX:?102;?LEFT:?230px;?POSITION:?absolute;?TOP:?44px"?runat="server"?Text="導(dǎo)出"></asp:Button></FONT>
????????
</form>
????
</body>
</HTML>
后臺(tái)代碼:cs
using?System;
using?System.Collections;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Web;
using?System.Web.SessionState;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.HtmlControls;
using?System.Data.SqlClient;
namespace?MsDataGrid
{
????
/**////?<summary>
????
///?WebForm1?的摘要說明。
????
///?</summary>

????public?class?ExportExcel?:?System.Web.UI.Page
????
{
????????
protected?System.Web.UI.WebControls.Button?btnMIME;
????????
protected?System.Web.UI.WebControls.DataGrid?dgShow;
????
????????
private?void?Page_Load(object?sender,?System.EventArgs?e)
????????
{
????????????
//?在此處放置用戶代碼以初始化頁(yè)面
????????????if(!IsPostBack)
????????????????BindData();
????????????
????????????
????????}

????????
private?void?BindData()
????????
{
????????????
string?strCon?=?System.Configuration.ConfigurationSettings.AppSettings["DSN"];
????????????SqlConnection?con?
=?new?SqlConnection(strCon);
????????????SqlDataAdapter?da?
=?new?SqlDataAdapter("Select?*?from?tbStudentinfo",con);
????????????DataSet?ds?
=?new?DataSet();
????????????da.Fill(ds,
"studentinfo");
????????????dgShow.DataSource?
=?ds.Tables["studentinfo"].DefaultView;
????????????dgShow.DataBind();
????????????
????????}

????????
Web?Form?Designer?generated?code#region?Web?Form?Designer?generated?code
????????
override?protected?void?OnInit(EventArgs?e)
????????
{
????????????
//
????????????
//?CODEGEN:該調(diào)用是?ASP.NET?Web?窗體設(shè)計(jì)器所必需的。
????????????
//
????????????InitializeComponent();
????????????
base.OnInit(e);
????????}

????????
????????
/**////?<summary>
????????
///?設(shè)計(jì)器支持所需的方法?-?不要使用代碼編輯器修改
????????
///?此方法的內(nèi)容。
????????
///?</summary>

????????private?void?InitializeComponent()
????????
{????
????????????
this.btnMIME.Click?+=?new?System.EventHandler(this.btnMIME_Click);
????????????
this.Load?+=?new?System.EventHandler(this.Page_Load);

????????}

????????
#endregion


????????
private?void?btnMIME_Click(object?sender,?System.EventArgs?e)
????????
{
????????????Response.ContentType?
=?"application/vnd.ms-excel";
????????????Response.Charset?
=?"";
????????????
this.EnableViewState?=?false;
????????????System.IO.StringWriter?sw?
=?new?System.IO.StringWriter();
????????????System.Web.UI.HtmlTextWriter?hw?
=?new?System.Web.UI.HtmlTextWriter(sw);
????????????
int?nCur?=?dgShow.CurrentPageIndex;
????????????
int?nSize?=?dgShow.PageSize;
????????????
????????????dgShow.AllowPaging?
=?false;
????????????BindData();
????????????????
????????????dgShow.Columns[
7].Visible?=false;
????????????dgShow.RenderControl(hw);
????????????dgShow.Columns[
7].Visible?=true;
????????????
????????????
//以下恢復(fù)分頁(yè)
????????????dgShow.AllowPaging?=?true;
????????????dgShow.CurrentPageIndex?
=?nCur;
????????????dgShow.PageSize?
=?nSize;
????????????BindData();
????????????Response.Write(sw.ToString());
????????????Response.End();
????????}

????}

}

方法二:
if(DataGrid1.Items.Count==0)
????{
?????Response.Write("<script>alert('對(duì)不起,你沒有查詢到任何記錄,不能導(dǎo)出數(shù)據(jù)')</script>");
????}
????else
????{
?????

???? Response.Clear();
?????Response.Buffer= true;
?????Response.Charset="GB2312";
?????Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");
?????Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
?????//設(shè)置輸出流為簡(jiǎn)體中文
?????Response.ContentType = "application/ms-excel";
?????//設(shè)置輸出文件類型為excel文件。
?????this.EnableViewState = false;
?????System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
?????System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
?????System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
?????DataGrid1.RenderControl(oHtmlTextWriter);
?????Response.Write(oStringWriter.ToString());
?????Response.End();

}
??????? Response.Clear();
??????? Response.Buffer = true;
??????? Response.Charset = "GB2312";
??????? Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
??????? // 如果設(shè)置為 GetEncoding("GB2312");導(dǎo)出的文件將會(huì)出現(xiàn)亂碼!!!
??????? Response.ContentEncoding = System.Text.Encoding.UTF7;
??????? Response.ContentType = "application/ms-excel";//設(shè)置輸出文件類型為excel文件。
??????? System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
??????? System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
??????? this.GridView1.RenderControl(oHtmlTextWriter);
??????? Response.Output.Write(oStringWriter.ToString());
??????? Response.Flush();
??????? Response.End();

轉(zhuǎn)載于:https://www.cnblogs.com/ghd258/archive/2005/10/12/253181.html

總結(jié)

以上是生活随笔為你收集整理的DataGrid 完全攻略之二(把数据导出到Excel)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。