获取对象属性值=NPOI EXPORT

使用dll ==== NPOI.dll

获取属性,设置属性=参考:http://blog.csdn.net/cestarme/article/details/6548126

额外的:

导出的时候碰到一个问题,链接没有响应

function export(){
 window.location.href="/cms/user/export";   
}

 看了一下控制台,报的错是未定义名字,就是说export jQuery有自定义的方法名或默认的名字,所以把export名字改了就可以了

 

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;


public ActionResult Export()
        {
            HSSFWorkbook book = new HSSFWorkbook();
            ISheet sheet = book.CreateSheet("会员信息表");
            ICellStyle style = book.CreateCellStyle();
            style.Alignment = HorizontalAlignment.Center;
            NPOI.SS.UserModel.IRow row0 = sheet.CreateRow(0);
            NPOI.SS.UserModel.IRow row1 = sheet.CreateRow(1);
            string[] colEName = {  
                                    "UserName","Password","Salt","RealName","UserLevel",
                                    "Birthday","Mobile","TelPhone","HomeAddress","Active"
                                };
            string[] colZName = {   
                                    "登录民(电子邮件)","密码","撒盐","真实姓名","用户等级",
                                    "出生日期","手机号码","固定电话","地址","是否活动"
                                };

            for (int i = 0; i < colEName.Length; i++)
            {
                row0.CreateCell(i).SetCellValue(colEName[i]);
                row1.CreateCell(i).SetCellValue(colZName[i]);
            }
            row0.Cells.ForEach(u =>
            {
                u.CellStyle = style;
            });

            row1.Cells.ForEach(u =>
            {
                u.CellStyle = style;
            });

            var userLevelList = this.ShopService.GetUserLevelList();
            var models = this.ShopService.GetUserList(new Qxun.Shop.Contract.UserRequest() { IsLock = 2, IsDel = 0, Status = 4 }).ToList();
            if (models.Count > 0)
            {
                for (int i = 0; i < models.Count; i++)
                {
                    NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(i + 2);
                    Type type = models[i].GetType();
                    for (int j = 0; j < colEName.Length; j++)
                    {
                        object obj = null;
                        string value = "";
                        if (colEName[j] == "UserLevel")
                        {
                            var userLevel = userLevelList.Where(u => u.ID == models[i].UserLevelID).FirstOrDefault();
                            if (userLevel != null)
                            {
                                value = userLevel.Title;
                            }
                        }
                        else if (colEName[j] == "Active")
                        {
                            obj = type.GetProperty("IsLock").GetValue(models[i], null);
                            if (Convert.ToInt32(obj)==0)
                            {
                                value = "活动";
                            }
                            else
                            {
                                value = "锁";
                            }
                        }
                        else
                        {
                            obj = type.GetProperty(colEName[j]).GetValue(models[i], null);
                            value = Convert.ToString(obj);
                        }
                        row2.CreateCell(j).SetCellValue(value);
                    }
                    row2.Cells.ForEach(u =>
                    {
                        u.CellStyle = style;
                    });
                }
            }
            // 写入内存流即可         
            MemoryStream ms = new MemoryStream();
            book.Write(ms);
            byte[] result = ms.ToArray();
            return File(result, "application/ms-excel", "会员信息表.xls");
        }

 

 

设置属性

 

 Type Ts = obj.GetType();
 object v = Convert.ChangeType(Value, Ts.GetProperty(FieldName).PropertyType);
 Ts.GetProperty(FieldName).SetValue(obj, v, null);

 

posted @ 2015-12-03 14:08  Danlis  阅读(401)  评论(0编辑  收藏  举报