.Net源码论坛 [ 繁体中文 ]

发新话题 回复该主题

asp.net2.0 导出成为Excel [复制链接]

楼主
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 Business;
using CommonTool;
public partial class BindGridView : System.Web.UI.Page
{
    BFUser myBFUser = new BFUser();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridViewInfo();
        }
    }
    protected void BindGridViewInfo()
    {
        DataTable dt = new DataTable();
        dt = myBFUser.GetAllFlatInfo();
        this.gridviewinfomation.DataSource = dt;
        this.gridviewinfomation.DataBind();
    }
    #region 导出成为Excel
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        Tools.ExportExcel(gridviewinfomation, 2, "人员总数: ");
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    #endregion


//----------------------------------------------------------------
//  <copyright file="Tools.cs" company="Saga Technologies">
//  Project: FlyOA
//  Copyright (C) Saga Technologies, 2005.  All rights reserved.
//  </copyright>
//  Creator: <Jack zhang>
//  Created Date: [12-12-2006]
//----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;
using System.IO;
using System.Web.UI;
namespace CommonTool
{
    public class Tools
    {
        /// <summary>
        /// 导出Elcel
        /// </summary>
        /// <param name="exportTargetGridView">目标GridView</param>
        /// <param name="visibleNum">隐藏的列数</param>
        /// <param name="content">说明内容</param>
        public static void ExportExcel(GridView exportTargetGridView, int visibleNum, string content)
        {
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
            HttpContext.Current.Response.ContentType = "application/excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            for (int i = 0; i < visibleNum; i++)
            {
                exportTargetGridView.Columns[exportTargetGridView.Columns.Count - (i + 1)].Visible = false;
            }
            exportTargetGridView.RenderControl(htw);
            sw.WriteLine(content + exportTargetGridView.Rows.Count.ToString());
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
        }
    }
}
分享 转发
TOP
沙发

学习了
TOP
板凳

回复 2楼sunny87的帖子

多谢支持
TOP
地板

看帖回帖是美德
TOP
五楼

回复 4楼wsvip的帖子

知道
TOP
六楼

赚积分,小手一抖,积分到手
TOP
七楼

这个方法不错的
TOP
八楼

回复 6楼wsvip的帖子

。。。。
TOP
九楼

回复 7楼chghm的帖子

搜噶
TOP
十楼

不错,正在找
TOP
发新话题 回复该主题