ASP.NET中导出按钮的方法

1200阅读 0评论2013-01-22 lwyyuan
分类:C#/.net

这里记录一下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();
        }

 

上一篇:SQL学习小例子整理
下一篇:sql查询语句中遇到问题