Example #1
0
        public List <T> ExcelDataToEntityList(string filePath, string sheetName, Dictionary <string, ExcelFieldParam> ncMap)
        {
            Dictionary <int, string> ncMapIndex = new Dictionary <int, string>();


            DataTable dtImport = ExcelHelper.ReadExcelFileToDataTable(filePath, sheetName, true);

            int rowCount   = dtImport.Rows.Count;
            int columCount = dtImport.Columns.Count;

            string tableName   = ObjectHelper.GetEntityName <T>();
            string fieldIDName = tableName + BusinessHelper.EPASID;//表主键名称

            for (int colum = 0; colum < columCount; colum++)
            {
                string columName = dtImport.Columns[colum].Caption;
                if (ncMap.ContainsKey(columName))
                {
                    ncMapIndex.Add(colum, columName);
                }
            }

            List <T> itemList = new List <T>();

            for (int row = 0; row < rowCount; row++)
            {
                T item = System.Activator.CreateInstance <T>();
                //给实体类中主键赋值
                ObjectHelper.SetObjectValueByFieldName <T>(item, fieldIDName, Guid.NewGuid().ToString());

                for (int colum = 0; colum < columCount; colum++)
                {
                    if (ncMapIndex.ContainsKey(colum))
                    {
                        string displayName = ncMapIndex[colum];
                        string fieldName   = ncMap[displayName].ExcelFieldName;

                        object value = dtImport.Rows[row][colum];

                        //需要换算
                        if (ncMap[displayName].IsTrans == CustomEnable.Enable)
                        {
                            Type type = Type.GetType(ncMap[displayName].ClassName);//获取类 ""里为 命名空间.类名

                            if (ncMap[displayName].TransType == ExcelTransType.EnumTrans)
                            {
                                //枚举换算
                                Dictionary <string, int> enumMap = EnumAttribute.GetEnumFields(type);

                                if (enumMap.ContainsKey(value.ToString()))
                                {
                                    value = enumMap[value.ToString()];
                                }
                            }
                            else if (ncMap[displayName].TransType == ExcelTransType.EntityTrans)
                            {
                                //实体换算

                                List <string> strList = GetTransEntityByField <string>(ncMap[displayName].TransFieldName,
                                                                                       ncMap[displayName].ExcelFieldName, value.ToString(), ncMap[displayName].ClassName);

                                if (strList != null && strList.Count > 0)
                                {
                                    value = strList.FirstOrDefault();
                                }

                                fieldName = ncMap[displayName].TransFieldName;
                            }
                        }

                        ObjectHelper.SetObjectValueByFieldName <T>(item, fieldName, value);
                    }
                }

                itemList.Add(item);
            }

            return(itemList);
        }