导出Excel

来源:互联网 发布:兰博德网络加速 编辑:程序博客网 时间:2024/06/18 14:12

Response.Clear();  
  Response.Write("<table><tr>.....<table">);  
  Response.Charset = "GB2312";
  Response.ContentEncoding = System.Text.Encoding.UTF8;
  Response.ContentType="Application/vnd.ms-excel";  
  Response.AddHeader("Content-Disposition","attachment;filename=ReqForQuote.xls");  
  Response.End();


=======================================================
    #region 导出Excel
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        if (!string.IsNullOrEmpty(temp))
        {
            Response.Write(temp);
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "Application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment;filename=Excel.xls");
            Response.End();
        }
        else
        {
            Response.Write("<script>alert('导出前请先统计!')</script>");
        }
    }
    #endregion

==================================================
ASP.net:

using System.IO;

public partial class admin_user_excel2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["admin_login"] == null)
        {
            Response.Redirect("../login.aspx");
        }
        try
        {
            if (Request.Cookies["sql_wj_cx"] == null)//无cookie
            {
                Response.Write("<script>window.location='userList.aspx';</script>");
            }
            else//读取cookie
            {
                HttpCookie cookies = Request.Cookies["sql_wj_cx"];
                string sql_user = cookies.Values["sql_str_wj"];
                DataBase db = new DataBase();
                this.GridView1.DataSource = db.FILL(sql_user).Tables[0];
                this.GridView1.DataBind();
            }
            string day = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
            ExportToExcel("application/ms-excel", "591win调查_" + day + ".xls");
            Response.Write("<script>alert('导出Excel成功!');window.location='userList.aspx';</script>");
        }
        catch { }
        finally
        {
            Response.Write("<script>window.location='userList.aspx';</script>");
        }
    }

    public void ExportToExcel(string FileType, string FileName)
    {
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF8;//UTF8
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        GridView1.RenderControl(hw);
        Response.Output.Write(tw.ToString());
        Response.Flush();
        Response.End();

    }
}

原创粉丝点击