Chinaunix首页 | 论坛 | 博客
  • 博客访问: 56767
  • 博文数量: 16
  • 博客积分: 125
  • 博客等级: 民兵
  • 技术积分: 205
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-04 13:05
文章分类

全部博文(16)

文章存档

2016年(2)

2015年(3)

2013年(6)

2012年(5)

我的朋友

分类: C#/.net

2013-01-22 17:26:38

这里记录一下ASP.NET 中添加导出Excel功能的方法。

//导出
        protected void btnOutExcel_Click(object sender, EventArgs e)
        {
            if (rptList.Items.Count > 0)
            {
                //调用导出方法              
                ExportGridViewForUTF8("艺术品信息汇总");
            }
            else
            {
                PublicClass.ShowAlter(this.Page, "waring", "没有数据可导出!");
            }

        }

        ///


        /// 导出方法
        ///

        /// 保存的文件名称
        private void ExportGridViewForUTF8(string filename)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "utf-8";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");     //设置输出流为简体中文
            Response.ContentType = "application/ms-excel";    //设置输出文件类型为excel文件。
            this.EnableViewState = false;
            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            rptList.RenderControl(oHtmlTextWriter);

            Response.Write("

");
            Response.Write("");
            Response.Write("");
            Response.Write(" ");
            Response.Write(string.Format(" ", filename));
            Response.Write("");

            Response.Write("

");
            Response.Write(" ");
            Response.Write(" ");
            Response.Write(" ");
            Response.Write(" ");
            Response.Write(" ");
            Response.Write(" ");
            Response.Write("");
            Response.Write("");
            Response.Write("");
            Response.Write("");
            Response.Write("");
            Response.Write("");
            Response.Write("");
            Response.Write("");
            Response.Write("");

            Response.Write("

");
            Response.Write("");
            Response.Write("");
            Response.Write(oStringWriter.ToString());
            Response.Write("
{0}
序号 艺术品编号 艺术品名称 作者 创作日期 展览名称展出日期撤展日期所属展厅材质展品尺寸有源标签号无源标签号状态备注
");
            Response.Write("
");
            Response.End();
        }

 

阅读(1208) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~