/// <summary> /// 枚举转DataTable /// </summary> /// <param name="firstValue">第一项请选择的值 默认0</param> /// <param name="showDescription">是否显示说明</param> /// <returns></returns> public static DataTable GetDataTable(int firstValue = 0, bool showDescription = false) { Type enumType = typeof(TEnum); // 获取类型对象 FieldInfo[] enumFields = enumType.GetFields(); DataTable table = new DataTable(); table.Columns.Add("Name", Type.GetType("System.String")); table.Columns.Add("Value", Type.GetType("System.Int32")); // DataRow trow = table.NewRow(); // trow[0] = "请选择"; //trow[1] = firstValue; //row[1] = (int)Enum.Parse(enumType, field.Name); 也可以这样 // table.Rows.Add(trow); //遍历集合 foreach (FieldInfo field in enumFields) { if (!field.IsSpecialName) { DataRow row = table.NewRow(); string fieldName = string.Empty; if (showDescription) { object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); if (arr != null) { fieldName = ((DescriptionAttribute)arr[0]).Description; } } else { fieldName = field.Name; } row[0] = fieldName; row[1] = ZConvert.StrToInt(field.GetRawConstantValue()); //row[1] = (int)Enum.Parse(enumType, field.Name); 也可以这样 table.Rows.Add(row); } } return(table); }
private void GenerateXlsList <T>(string xlsUrl, List <T> list, string format, string reportName, List <string> format2 = null) { HSSFWorkbook hSSFWorkbook = this.CreateWorkbook(""); int num = list.Count / 60000; int num2 = 60000; for (int i = 0; i <= num; i++) { List <T> list2 = list.Skip(i * num2).Take(num2).ToList <T>(); string text = reportName + ((i == 0) ? "" : ("-" + i.ToString())); ISheet sheet = hSSFWorkbook.CreateSheet(text); #region 标题 IRow row = sheet.CreateRow(0); for (int j = 0; j < format.Split(new char[] { ';' }).Count <string>(); j++) { string text2 = format.Split(new char[] { ';' })[j]; string cellValue = text2.Split(new char[] { '|' })[1]; ICell cell = row.CreateCell(j); cell.SetCellValue(cellValue); } #endregion PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); #region MyRegion for (int j = 0; j < list2.Count; j++) { IRow row2 = sheet.CreateRow(j + 1); #region MyRegion for (int k = 0; k < format.Split(new char[] { ';' }).Count <string>(); k++) { string text2 = format.Split(new char[] { ';' })[k]; string text3 = text2.Split(new char[] { '|' })[0]; if (properties.Count <= 0) { return; } #region MyRegion for (int l = 0; l < properties.Count; l++) { if (properties[l].Name == text3) { ICell cell = row2.CreateCell(k); string text4 = string.Empty; if (properties[text3].GetValue(list2[j]) != null) { text4 = properties[text3].GetValue(list2[j]).ToString(); } Type propertyType = properties[text3].PropertyType; if (propertyType == typeof(decimal) || propertyType == typeof(int) || propertyType == typeof(double)) { cell.SetCellValue(Convert.ToDouble(properties[text3].GetValue(list2[j]))); } else { if (properties[text3].GetValue(list2[j]) != null) { cell.SetCellValue(properties[text3].GetValue(list2[j]).ToString()); } else { cell.SetCellValue(string.Empty); } } } } #endregion } #endregion } #endregion sheet.ForceFormulaRecalculation = true; if (num == i) { if (format2 != null && format2.Count() > 0) { int z = list2.Count; foreach (var item in format2) { IRow row3 = sheet.CreateRow(z + 1); #region MyRegion for (int j = 0; j < format.Split(new char[] { ';' }).Count <string>(); j++) { } #endregion #region 合并 for (int j = 0; j < item.Split(new char[] { ';' }).Count <string>(); j++) { string text2 = item.Split(new char[] { ';' })[j]; //值 string cellValue = text2.Split(new char[] { '|' })[1]; string cellindex = text2.Split(new char[] { '|' })[0]; var arr = cellindex.Split(new char[] { '-' }); //合并 CellRangeAddress cellRangeAddress = new CellRangeAddress(z + 1, z + 1, ZConvert.StrToInt(arr[0]), ZConvert.StrToInt(arr[1])); sheet.AddMergedRegion(cellRangeAddress); ICell cell = row3.CreateCell(ZConvert.StrToInt(arr[0])); ICellStyle cellstyle = hSSFWorkbook.CreateCellStyle(); cellstyle.Alignment = HorizontalAlignment.Right; cellstyle.VerticalAlignment = VerticalAlignment.Center; cell.CellStyle = cellstyle; cell.SetCellValue(cellValue); } #endregion z++; } } } } FileStream fileStream = new FileStream(xlsUrl, FileMode.Create); hSSFWorkbook.Write(fileStream); fileStream.Close(); }