• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ASP.NET系统开发-生Blog
博客园    首页    新随笔    联系   管理    订阅  订阅
导出EXCEL

protected void Button1_Click(object sender, EventArgs e)//按钮
    {
        GridView ctl = this.GridView1;//页面控件
        //DataGrid1是你在窗体中拖放的控件
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
        HttpContext.Current.Response.Charset = "UTF-8";

        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        ctl.Page.EnableViewState = false;
        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
        ctl.RenderControl(hw);
        HttpContext.Current.Response.Write(tw.ToString());
        HttpContext.Current.Response.End();
    }

    public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
    {

    }

 

方式2:

 

StringWriter sw = new StringWriter();
        sw.WriteLine("留言消息\t评论用户\t评论时间");

        for (int i = 0; i < ds.Tables[0].Rows.Count;i++ )
        {
            string strs = ds.Tables[0].Rows[i]["message"].ToString() + "\t" + ds.Tables[0].Rows[i]["UserName"].ToString() + "\t" + ds.Tables[0].Rows[i]["InsertTime"].ToString();
            sw.WriteLine(strs);
        }
        sw.Close();
        Response.AddHeader("Content-Disposition", "attachment; filename=Message.xls");
        Response.ContentType = "application/ms-excel";
        Response.ContentEncoding = Encoding.GetEncoding("GB2312");
        Response.Write(sw);
        Response.End();

 

using System.Data.Common;
using System.IO;
using System.Text;

 

posted on 2009-12-27 22:29  生  阅读(129)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3