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

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" %>











    无标题页





    

 
程序加载中Loading

实际上Ajax下面实现导出Excel 就是添加Triggers


            
           


你可能感兴趣的:(asp.net)