分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

NPOI+反射+自定义特性实现上传excel转List及验证

发布时间:2023-09-06 02:05责任编辑:赖小花关键词:excel

1.自定义特性

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] ???public class PropertyDescriptionAttribute : Attribute ???{ ???????private bool _allownullorempty = true; ???????public PropertyDescriptionAttribute() { } ???????/// <summary> ???????/// 是否允许为null或空值 ???????/// </summary> ???????public bool AllowNullOrEmpty ???????{ ???????????get ???????????{ ???????????????return this._allownullorempty; ???????????} ???????????set ???????????{ ???????????????this._allownullorempty = value; ???????????} ???????} ???}

2.定义类(假设是用户信息)

public class UploadUserModel ???{ ???????[PropertyDescription(AllowNullOrEmpty = false)] ???????public string Name { get; set; } ???????[PropertyDescription(AllowNullOrEmpty = false)] ???????public string Phone { get; set; } ???}

自定义特定来标识这两个信息不能为空

3.实现

/// <summary>/// PROPERTY_NAME数组:值与excel列一一对应/// </summary>private readonly string[] PROPERTY_NAME = { "Name", "Phone" };private List<UploadUserModel> ExcelToList(HttpPostedFile excelFile) ???????{ ???????????IWorkbook workbook = null; ???????????ISheet sheet = null; ???????????int colCount = 0; ???????????List<UploadUserModel> users = new List<UploadUserModel>(); ???????????if (excelFile.FileName.IndexOf(".xlsx") > 0) ???????????{ ???????????????workbook = new XSSFWorkbook(excelFile.InputStream); ???????????} ???????????else if (excelFile.FileName.IndexOf(".xls") > 0) ???????????{ ???????????????workbook = new HSSFWorkbook(excelFile.InputStream); ???????????} ???????????if (workbook != null) ???????????{ ???????????????sheet = workbook.GetSheetAt(0); ???????????????if (sheet != null && sheet.LastRowNum > 0) ???????????????{ ???????????????????colCount = sheet.GetRow(0).LastCellNum;//获取列数 ???????????????????????????????????????//从第二行开始解析 ???????????????????for (int rowIndex = 1; rowIndex <= sheet.LastRowNum; rowIndex++) ???????????????????{ ???????????????????????var curRow = sheet.GetRow(rowIndex);//获取当前行 ??????????????????????????????????????????????UploadUserModel user = new UploadUserModel(); ???????????????????????Type cType = user.GetType(); ???????????????????????//解析列 ???????????????????????for (int colIndex = 0; colIndex < colCount; colIndex++) ???????????????????????{ ???????????????????????????var curCell = curRow.GetCell(colIndex); ???????????????????????????if (curCell != null) ???????????????????????????{ ???????????????????????????????curCell.SetCellType(CellType.String);//把单元格设置成String类型,统一取值方式 ???????????????????????????} ???????????????????????????//定义PROPERTY_NAME避免if判断 ???????????????????????????PropertyInfo propertyInfo = cType.GetProperty(PROPERTY_NAME[colIndex]); ???????????????????????????//获取自定义特性 ???????????????????????????object[] customAttrs = propertyInfo.GetCustomAttributes(typeof(PropertyDescriptionAttribute), true); ???????????????????????????if (customAttrs.Length > 0) ???????????????????????????{ ???????????????????????????????PropertyDescriptionAttribute attr = customAttrs[0] as PropertyDescriptionAttribute; ???????????????????????????????if (!attr.AllowNullOrEmpty)//属性值不能为空 ???????????????????????????????{ ???????????????????????????????????if (curCell == null) ???????????????????????????????????{ ???????????????????????????????????????throw new Exception("第" + (rowIndex + 1).ToString() + "行有未填项,请填写后重新上传。"); ???????????????????????????????????} ???????????????????????????????????else if (string.IsNullOrEmpty(curCell.StringCellValue)) ???????????????????????????????????{ ???????????????????????????????????????throw new Exception("第" + (rowIndex + 1).ToString() + "行有未填项,请填写后重新上传。"); ???????????????????????????????????} ???????????????????????????????} ???????????????????????????????object cellValue = null; ???????????????????????????????if (curCell == null) ???????????????????????????????{ ???????????????????????????????????cellValue = ""; ???????????????????????????????} ???????????????????????????????else ???????????????????????????????{ ???????????????????????????????????cellValue = curCell.StringCellValue; ???????????????????????????????} ???????????????????????????????if (!propertyInfo.PropertyType.IsGenericType) ???????????????????????????????{ ???????????????????????????????????//非泛型 ???????????????????????????????????propertyInfo.SetValue(user, curCell == null ? null : Convert.ChangeType(cellValue, propertyInfo.PropertyType), null); ???????????????????????????????} ???????????????????????????????else ???????????????????????????????{ ???????????????????????????????????//泛型Nullable<> ???????????????????????????????????Type genericTypeDefinition = propertyInfo.PropertyType.GetGenericTypeDefinition(); ???????????????????????????????????if (genericTypeDefinition == typeof(Nullable<>)) ???????????????????????????????????{ ???????????????????????????????????????propertyInfo.SetValue(user, curCell == null ? null : Convert.ChangeType(cellValue, Nullable.GetUnderlyingType(propertyInfo.PropertyType)), null); ???????????????????????????????????} ???????????????????????????????} ???????????????????????????} ???????????????????????????????????????????????????} ???????????????????????users.Add(user); ???????????????????} ???????????????} ???????????} ???????????else ???????????{ ???????????????throw new Exception("Excel解析异常"); ???????????} ???????????foreach (var item in users) ???????????{ ???????????????if (!checkPhoneGS(item.Phone)) ???????????????{ ???????????????????throw new Exception("手机号格式不正确:"+ item.Phone); ???????????????} ???????????} ???????????return users; ???????}

不要看着麻烦,核心代码很简单。用反射和定义PROPERTY_NAME数组是为了代码重用。最后那段泛型、非泛型判断可以删除,估计一般用不到

NPOI+反射+自定义特性实现上传excel转List及验证

原文地址:https://www.cnblogs.com/betterlife/p/9360707.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved