您的位置:首页 > 编程语言 > ASP

ASP.NET下导出Excel 以及在Ajax下面的实现

2013-01-27 19:39 106 查看
using System;
using System.Data;
using System.Configuration;
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;
using System.IO;
using System.Threading;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnOK_Click(object sender, EventArgs e)
{
Thread.Sleep(5000);
bind();
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}

protected void bind()
{
SqlConnection con = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select CustomerID,ContactName,ContactTitle,City from Customers", con);
DataSet ds = new DataSet();
sda.Fill(ds);
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
}
protected void btnExcel_Click(object sender, EventArgs e)
{
Thread.Sleep(2000);
SqlConnection con = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select CustomerID,ContactName,ContactTitle,City from Customers", con);
DataSet ds = new DataSet();
sda.Fill(ds);
DataTable dt = ds.Tables[0];

StringWriter sw = new StringWriter();
string sWriteLine = "";
string sExcelFileName = "Excel.xls";
int i = 0;
//写标题
for (i = 0; i < dt.Columns.Count - 1; i++)
{
sWriteLine += dt.Columns[i].ColumnName.ToString().Trim() + "\t";
}
sWriteLine += dt.Columns[dt.Columns.Count - 1].ColumnName.ToString().Trim();
sw.WriteLine(sWriteLine);
//写内容
foreach (DataRow dr in dt.Rows)
{
sWriteLine = "";
for (i = 0; i < dt.Columns.Count - 1; i++)
{
sWriteLine += dr[i].ToString().Trim() + "\t";
}
sWriteLine += dr[dt.Columns.Count - 1].ToString().Trim();
sw.WriteLine(sWriteLine);
}
sw.Close();
ds.Dispose();
con.Close();

Page.Response.Clear();
// 防止中文内容为乱码
Page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
//可令中文文件名不为乱码
Page.Response.AppendHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(sExcelFileName));
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
Page.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Charset = "gb2312";//编码,根据需要修改

Page.Response.Write(sw);
Page.Response.End();
}
}


前台页面<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

Namespace="System.Web.UI" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>无标题页</title>

</head>

<body>

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server">

</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

<ContentTemplate>

<div>

<asp:Button ID="btnOK" runat="server" OnClick="btnOK_Click" Text="获取" />

<asp:Button ID="btnExcel" runat="server" OnClick="btnExcel_Click" Text="Excel" /><br />

 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"

CellPadding="4" ForeColor="#333333" GridLines="None" Height="282px" OnPageIndexChanging="GridView1_PageIndexChanging"

Width="250px">

<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<RowStyle BackColor="#EFF3FB" />

<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<EditRowStyle BackColor="#2461BF" />

<AlternatingRowStyle BackColor="White" />

</asp:GridView>

</div>

</ContentTemplate>

<Triggers>

<asp:PostBackTrigger ControlID="btnExcel" />

</Triggers>

</asp:UpdatePanel>

<asp:UpdateProgress ID="UpdateProgress1" runat="server">

<ProgressTemplate>

程序加载中Loading

</ProgressTemplate>

</asp:UpdateProgress>

<br />

</form>

</body>

</html>
实际上Ajax下面实现导出Excel 就是添加Triggers
<Triggers>
<asp:PostBackTrigger ControlID="btnExcel" />
</Triggers>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: