码迷,mamicode.com
首页 > Web开发 > 详细

MVC的导出

时间:2015-05-11 00:01:53      阅读:393      评论:0      收藏:0      [点我收藏+]

标签:

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;

        }

 

MVC的导出

标签:

原文地址:http://www.cnblogs.com/linbicheng/p/4493379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!