コード例 #1
0
ファイル: NPOIService.cs プロジェクト: mengxuef/NPOIExtension
        public static List <T> ExcelToList <T>(IWorkbook workbook, ISheet sheet, List <ExcelColumn> columns, int headRowIndex, int dataRowIndex)
        {
            InitHead <T>(columns);
            List <int> lstCellNum = new List <int>();
            List <T>   entityList = new List <T>();
            IRow       headRow    = sheet.GetRow(headRowIndex);

            for (int i = 0; i < columns.Count; i++)
            {
                int cellNum = headRow.GetCellNum(columns[i].Name);
                if (cellNum == -1)
                {
                    throw new ExcelValidException("缺少列:" + columns[i].Name);
                }
                lstCellNum.Add(cellNum);
            }
            PropertyInfo[] properties = typeof(T).GetProperties();
            Dictionary <string, PropertyInfo> dictProperty = new Dictionary <string, PropertyInfo>();

            for (int i = 0; i < properties.Length; i++)
            {
                dictProperty.Add(properties[i].Name, properties[i]);
            }
            IRow   row;
            ICell  cell;
            object cellValue;

            for (int i = dataRowIndex; i <= sheet.LastRowNum; i++)
            {
                row = sheet.GetRow(i);
                T entity = Activator.CreateInstance <T>();
                for (int j = 0; j < columns.Count; j++)
                {
                    cell      = row.GetCell(lstCellNum[j]);
                    cellValue = GetCellValue(cell);
                    if (columns[j].AllowNull == false && string.IsNullOrWhiteSpace(cellValue.ToString()))
                    {
                        throw new ExcelValidException(columns[j].Name + "不能为空");
                    }
                    if (columns[j] is ExcelColumn <T> )
                    {
                        var genericColumn = columns[j] as ExcelColumn <T>;
                        if (genericColumn.OnDataBind != null)
                        {
                            cellValue = genericColumn.OnDataBind(cellValue, entity, cell);
                        }
                    }
                    else
                    {
                        if (columns[j].OnDataBind != null)
                        {
                            cellValue = columns[j].OnDataBind(cellValue, entity, cell);
                        }
                    }
                    if (dictProperty[columns[j].Field].PropertyType.IsValueType && string.IsNullOrWhiteSpace(cellValue.ToString()))
                    {
                        cellValue = Activator.CreateInstance(dictProperty[columns[j].Field].PropertyType);
                    }
                    else
                    {
                        try
                        {
                            cellValue = Convert.ChangeType(cellValue, dictProperty[columns[j].Field].PropertyType);
                        }
                        catch
                        {
                            if (dictProperty[columns[j].Field].PropertyType == typeof(DateTime))
                            {
                                cellValue = CommonUtility.ConvertToDateTime(cellValue.ToString());
                            }
                        }
                    }
                    dictProperty[columns[j].Field].SetValue(entity, cellValue, null);
                }
                entityList.Add(entity);
            }
            return(entityList);
        }