/// <summary> /// 构造函数初始化 /// </summary> /// <param name="dateFormate"></param> public ExcelTool(string dateFormate = "yyyy-mm-dd") { workbook = new XSSFWorkbook(); TType = typeof(T); //1:设置属性 properties = TType.GetProperties(); //2:设置Col的默认最大长度 MaxColumnLength = 50; //3:默认的字体样式 _defaultStyle = workbook.CreateCellStyle(); _fontColor = workbook.CreateFont(); _fontColor.Color = HSSFColor.Black.Index; _fontColor.FontHeightInPoints = 11; //4:设置日期样式 _dateStyle = workbook.CreateCellStyle(); IDataFormat format = workbook.CreateDataFormat(); _dateStyle.DataFormat = format.GetFormat(dateFormate); _dateStyle.SetFont(_fontColor); //2:检查是否有 if (typeof(IHasFontColor).IsAssignableFrom(TType)) { IsHasColor = true; } }
T FillRow(T prow = null) { T row = null; bool modeCreate = prow == null; if (modeCreate) { row = (T)Activator.CreateInstance(TType, null); } else { row = prow; } PropertyInfo[] propertys = TType.GetProperties(); foreach (PropertyInfo pro in propertys) { if (pro.Name == "id") { if (PropertyHelper.IsAutoIncrease(pro)) { continue; } else if (!modeCreate) { continue; } } object val = null; if (pro.PropertyType == typeof(Int32?)) { val = THelper.GetRandom(); } else if (pro.PropertyType == typeof(Int64?)) { val = Convert.ToInt64(THelper.GetRandom()); } else if (pro.PropertyType == typeof(String)) { val = pro.Name + " " + THelper.GetRandom(); } else if (pro.PropertyType == typeof(DateTime?)) { val = DateTime.Now; } if (val != null) { pro.SetValue(row, val); } } return(row); }