Beispiel #1
0
        public static string GetNameByType(PropertyInfo info)
        {
            var attributes = info.GetCustomAttributes(typeof(ColNameAttribute), false);

            if (attributes != null && attributes.Length > 0)
            {
                ColNameAttribute colName = attributes[0] as ColNameAttribute;
                if (colName != null)
                {
                    return(colName.Name ?? info.Name);
                }
                attributes = info.GetCustomAttributes(typeof(TableNameAttribute), false);
                if (attributes != null && attributes.Length > 0)
                {
                    TableNameAttribute tableName = attributes[0] as TableNameAttribute;
                    if (tableName != null)
                    {
                        if (!tableName.IsUserDateCreateTable)
                        {
                            return(tableName.Name);
                        }
                        else
                        {
                            string[] tableNames = tableName.Name.Split(new Char[] { '{', '}' });
                            if (tableNames.Length == 3)
                            {
                                return(string.Format("{0}{1}{2}", tableNames[0], DateTime.Now.ToString(tableNames[1]), tableNames[2]));
                            }
                        }
                    }
                }
            }

            return(info.Name);
        }
        /// <summary>
        /// 将IRow转换为ExcelDataRow
        /// </summary>
        /// <typeparam name="TExcelTemplate"></typeparam>
        /// <param name="row"></param>
        /// <param name="excelDataHeader"></param>
        /// <returns></returns>
        private ExcelDataRow ConvertToExcelDataRow <TExcelTemplate>(IRow row, ExcelDataHeader excelDataHeader)
            where TExcelTemplate : class, new()
        {
            var          propertys    = typeof(TExcelTemplate).GetProperties();
            ExcelDataRow excelDataRow = new ExcelDataRow
            {
                RowIndex = row.RowNum
            };

            propertys.ToList().ForEach(p =>
            {
                if (p.IsDefined(typeof(ColNameAttribute), true))
                {
                    ColNameAttribute colNameAttr = p.GetCustomAttribute <ColNameAttribute>();
                    var dataCol = excelDataHeader.DataCols.FirstOrDefault(dc => dc.ColName == colNameAttr.ColName);
                    excelDataRow.excelDataColList.Add(new ExcelDataCol
                    {
                        ColIndex     = dataCol.ColIndex,
                        ColName      = dataCol.ColName,
                        RowIndex     = row.RowNum,
                        CellString   = row.GetCell(dataCol.ColIndex).GetCellValue(),
                        PropertyName = p?.Name
                    });
                }
                else
                {
                    excelDataRow.excelDataColList.Add(new ExcelDataCol
                    {
                        ColIndex     = 0,
                        ColName      = "",
                        RowIndex     = row.RowNum,
                        CellString   = "",
                        PropertyName = p?.Name
                    });
                }
            });
            return(excelDataRow);
        }