ASP.NET将数据导入到Excel

1.直接将当前页面Repeater里的所有数据导出到Excel   (在BtnExcel单击事件里写)

//定义文档类型、字符编码
            string title = "**统计表" + "_" + DateTime.Today.ToShortDateString();
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            string filename = "attachment;filename=" + System.Web.HttpUtility.UrlEncode(title, System.Text.Encoding.UTF8) + ".xls";
            Response.AppendHeader("Content-Disposition", filename);
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            //Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档
            Response.ContentType = "application/ms-excel";
            Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");

            this.EnableViewState = false;
            // 定义一个输入流
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            this.lstPurchaseOrder.RenderControl(oHtmlTextWriter);
            //this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
            Response.Write(oStringWriter.ToString());
            Response.End();

 一个导出Excel的方法 进行调用!

 #region 导出Excel方法
        /// <summary>
        /// 导出Excel方法
        /// </summary>
        /// <param name="FileType"></param>
        /// <param name="FileName"></param>
        private void Export(string FileType, string FileName)
        {
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.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);
            gdvExcelOrder.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
        }
        #endregion
   #region 导出Excel
        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            string year = (DateTime.Now.Year - 2000).ToString("D2");
            string month = DateTime.Now.Month.ToString("D2");
            string day = DateTime.Now.Day.ToString("D2");
            string fileName = year + "-" + month + day;
            Export("application/ms-excel", fileName+".xls");
        }
        #endregion

注意:

如果

类型“GridView”的控件“ctl00_ContentPlaceHolder1_GridView1”必须放在具有 runat=server 的窗体标记内。 
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.Web.HttpException: 类型“GridView”的控件“ctl00_ContentPlaceHolder1_GridView1”必须放在具有 runat=server 的窗体标记内。

有报这个错误,那么请检查一下form标签里有没有 runat="server" 

如果有还是报错 可以加上下面的代码 就可以了

    #region 确认在运行时为指定的 ASP.NET 服务器控件呈现 HtmlForm 控件
        /// <summary>
        /// 确认在运行时为指定的 ASP.NET 服务器控件呈现 HtmlForm 控件
        /// </summary>
        /// <param name="control"></param>
        public override void VerifyRenderingInServerForm(Control control)
        { }
        #endregion

 

转载于:https://www.cnblogs.com/huangtu/archive/2013/02/28/2936971.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值