• MVC的导出


    MVC  的导出
    //前台 传入的查询条件
    function DaochuExcel() {
    var filite = daochuFilter(); var url = "/Sku/DaochuExcel"; url += "?page=1"; url += "&rows=10000"; url += "&sidx=create_date"; url += "&sord=asc"; url += "&filters=" + JSON.stringify(filite); window.location = url; }

    后台代码

    public void DaochuExcel(long page, long rows, string sidx, string sord, string filters = null)
            {
                ResultObject res = new ResultObject();
    
    
                SqlFilter filt = null;
                if (!string.IsNullOrEmpty(filters) && !string.Equals(filters, "null", StringComparison.OrdinalIgnoreCase))
                {
                    JavaScriptSerializer j = new JavaScriptSerializer();
                    filt = j.Deserialize<SqlFilter>(filters);
                }
    
                List<SqlFilter> f = new List<SqlFilter>();
                f.Add(filt);
                var pageParamer = new PageParamerPlus(page, rows, sidx, sord, f);
                PageList<Trades> list = Trades.GetPageList(userInfo, pageParamer);//查询出 数据
    
            
                 var dt = ListToDataTable(list.rows);

    var data = new List<List<string>>(); // 选择字段 data.Add(new List<string>() { "主订单号", "客户名称", "制单日期"}); for (int i = 0; i < dt.Rows.Count; i++) { data.Add(new List<string>() { dt.Rows[i]["Tid"].ToString(), dt.Rows[i]["Customer_Name"].ToString(), dt.Rows[i]["Create_Date"].ToString(), }); } var workbook = new HSSFWorkbook(); var outputStream = Response.OutputStream; var sheet = workbook.CreateSheet("info"); var headRow = sheet.CreateRow(0); for (int i = 0; i < data[0].Count; i++) { headRow.CreateCell(i).SetCellValue(data[0][i]); } for (int i = 1; i < data.Count; i++) { var dataRow = sheet.CreateRow(i); for (int j = 0; j < data[i].Count; j++) { dataRow.CreateCell(j).SetCellValue(data[i][j]); } } Response.Clear(); workbook.Write(outputStream); Response.Buffer = true; Response.AppendHeader("Content-Disposition", "attachment;filename=export.xls"); Response.ContentEncoding = Encoding.UTF8; Response.ContentType = "applicationnd.ms-excel"; Response.Flush(); }

     将泛类型集合List类转换成DataTable

    #region 将泛类型集合List类转换成DataTable
            /// <summary>
            /// 将泛类型集合List类转换成DataTable
            /// </summary>
            /// <typeparam name="T"> </typeparam>
            /// <param name="entitys">泛类型集合</param>
            /// <returns></returns>
            public static DataTable ListToDataTable<T>(List<T> entitys)
            {
                //检查实体集合不能为空
                if (entitys == null || entitys.Count < 1)
                {
                    throw new Exception("需转换的集合为空");
                }
                //取出第一个实体的所有Propertie
                Type entityType = entitys[0].GetType();
                PropertyInfo[] entityProperties = entityType.GetProperties();
    
                //生成DataTable的structure
                //生产代码中,应将生成的DataTable结构Cache起来,此处略
                DataTable dt = new DataTable();
                for (int i = 0; i < entityProperties.Length; i++)
                {
                    //dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType);
                    dt.Columns.Add(entityProperties[i].Name);
                }
                //将所有entity添加到DataTable中
                foreach (object entity in entitys)
                {
                    //检查所有的的实体都为同一类型
                    if (entity.GetType() != entityType)
                    {
                        throw new Exception("要转换的集合元素类型不一致");
                    }
                    object[] entityValues = new object[entityProperties.Length];
                    for (int i = 0; i < entityProperties.Length; i++)
                    {
                        entityValues[i] = entityProperties[i].GetValue(entity, null);
                    }
                    dt.Rows.Add(entityValues);
                }
                return dt;
            }
            #endregion

    把json转化成list

    public static List<T> JSONStringToList<T>(string JsonStr)
            {
    
                JavaScriptSerializer Serializer = new JavaScriptSerializer();
    
                List<T> objs = Serializer.Deserialize<List<T>>(JsonStr);
    
                return objs;
    
            }
    
  • 相关阅读:
    cuda thrust函数首次调用耗费时间比后续调用长原因
    poj2823/hdu3415
    hiho1515
    hiho147周
    hdu1864/2844/2159 背包基础题
    qt + opencv
    Matlab函数编译成dll供c调用
    无处不在的编程思想
    五步使用法——搞定XMLHttpRequest
    AJAX与传统Web开发比较
  • 原文地址:https://www.cnblogs.com/linbicheng/p/4493379.html
Copyright © 2020-2023  润新知