zoukankan      html  css  js  c++  java
  • 使用Linq导出数据到execl

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {

             IQueryable<TableName> query = SelectData();
            this.anpList.RecordCount = query.Count();
            int curr_page_index = this.anpList.CurrentPageIndex;
            if (Request.QueryString["page"] != null && StringHandler.IsNumeric(Request.QueryString["page"].ToString()))
            {
                curr_page_index = Convert.ToInt32(Request.QueryString["page"]);
            }
            this.rptList.DataSource = query.Skip((curr_page_index - 1) * this.anpList.PageSize).Take(this.anpList.PageSize);
            this.rptList.DataBind();

         }

     

        /// <summary>
        /// 查询数据
        /// </summary>
        private IQueryable<TableName> SelectData()
        {
            IQueryable<TableName> query = from t in db.TableName
                                                                  select t;
            return query;
        }

     /// <summary>    

    /// 导出到execl   

    /// </summary>    

    protected void btnExport_Click(object sender, EventArgs e)    

    {       

                     IQueryable<TableName> query = SelectData();//查询数据源

    string file_name = string.Format("{0:yyyyMMddHHmmss}", DateTime.Now) + "";     // 导出的文件名日期   

    HttpResponse resp;        

    resp = Page.Response;        

    resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //设置编码

          resp.ContentType = "application/application/vnd.ms-excel";//内容类型

          resp.AppendHeader("Content-Disposition", "attachment;filename=" + file_name + ".xls");//文件名称和格式

          string colHeaders = "", itemes= "";

          colHeaders += "ColumnA ";  

    colHeaders += "ColumnB ";        

    colHeaders += "ColumnC ";        

                                         resp.Write(colHeaders);        

                                        foreach (var item in query)        

    {     

     itemes += item.ColumnA_Values + " "  

    + item.ColumnB_Values + " "                    

    + item.ColumnC_Values + " ";     

    }        

    resp.Write(itemes);        

    resp.End();    

    }

  • 相关阅读:
    【Flex】Fluorinefx在IIS 5.1下响应很慢
    【Oracle】客户端查询数据库最近执行的SQL语句
    【Flex】Cairngorm中View层的交互
    ubuntu安装python3.6提示 无法修正错误 的 解决
    linux 如何杀死,暂停,继续一个后台进程
    Ubuntu虚拟机双网卡的配置(Uboot,tftp下载)(原创)
    Ubuntu的dpkg命令用法
    Vim Plus的搭建,并完善相关插件,打造最强vim编辑器(原创)
    ubuntu下minicom的使用方法(类似win的超级终端)
    Ubuntu安装ssh
  • 原文地址:https://www.cnblogs.com/professional-NET/p/4678360.html
Copyright © 2011-2022 走看看