XLSLoader.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using CommonLang;
  2. using CommonLang.Log;
  3. using NPOI.HSSF.UserModel;
  4. using NPOI.SS.UserModel;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Reflection;
  8. namespace XmdsCommonServer.XLS
  9. {
  10. /// <summary>
  11. /// FileName: XLSLoader.cs
  12. /// Author: Alex.Yu
  13. /// Corporation:...
  14. /// Description:
  15. /// DateTime: 2015/7/8 11:45:39
  16. /// </summary>
  17. public class XLSLoader
  18. {
  19. public class WSHFieldInfo
  20. {
  21. public FieldInfo mFieldInfo;
  22. public string mFieldName;
  23. public WSHFieldInfo(FieldInfo fieldInfo, string fieldName)
  24. {
  25. this.mFieldInfo = fieldInfo;
  26. this.mFieldName = fieldName;
  27. }
  28. }
  29. public readonly static Logger log = LoggerFactory.GetLogger("XmdsXLSLoader");
  30. public XLSLoader()
  31. {
  32. }
  33. public static List<T> LoadSheet<T>(ISheet sheet, int row_head_index = 1, int row_data_index = 3) where T : new()
  34. {
  35. //取首行,拿到类型.
  36. HashMap<int, WSHFieldInfo> head_map = new HashMap<int, WSHFieldInfo>();
  37. IRow head_row = null;
  38. ICell head_cell = null;
  39. int ci = 0;
  40. try
  41. {
  42. Type data_type = typeof(T);
  43. head_row = sheet.GetRow(row_head_index);
  44. for (ci = head_row.FirstCellNum ; ci <= head_row.LastCellNum ; ci++)
  45. {
  46. head_cell = head_row.GetCell(ci);
  47. if (head_cell != null)
  48. {
  49. head_cell.SetCellType(CellType.String);
  50. if (head_cell.StringCellValue != null)
  51. {
  52. FieldInfo field = data_type.GetField(head_cell.StringCellValue);
  53. if (field != null)
  54. {
  55. head_map.Add(ci, new WSHFieldInfo(field, head_cell.ToString()));
  56. }
  57. }
  58. }
  59. }
  60. }
  61. catch (Exception error)
  62. {
  63. throw new Exception(string.Format("LoadSheetError,sheetname = {0} row_headIndex = {1} cellNum = {2},error = {3}", sheet.SheetName, row_head_index, ci, error.ToString()));
  64. }
  65. int ri = 0;
  66. T data = new T();
  67. string rowContent = "", rowName = "";
  68. try
  69. {
  70. //取数据,赋值.
  71. List<T> ret = new List<T>(sheet.LastRowNum);
  72. for (ri = row_data_index ; ri <= sheet.LastRowNum ; ri++)
  73. {
  74. IRow data_row = sheet.GetRow(ri);
  75. if (data_row == null)
  76. {
  77. break;
  78. }
  79. if(data_row.Cells.Count <= 0)
  80. {
  81. log.Warn("表识别到空行:" + sheet.SheetName + ", " + ri);
  82. continue;
  83. }
  84. ICell headCell = data_row.GetCell(data_row.FirstCellNum);
  85. headCell.SetCellType(CellType.String);
  86. if (!string.IsNullOrEmpty(headCell.StringCellValue))
  87. {
  88. data = new T();
  89. for (ci = data_row.FirstCellNum ; ci <= data_row.LastCellNum ; ci++)
  90. {
  91. WSHFieldInfo fi = head_map.Get(ci);
  92. ICell icell = data_row.GetCell(ci);
  93. if (icell != null && fi != null)
  94. {
  95. rowContent = icell.ToString();
  96. rowName = fi.mFieldName;
  97. switch (icell.CellType)
  98. {
  99. case CellType.Numeric:
  100. case CellType.Formula:
  101. {
  102. object value = icell.NumericCellValue;
  103. if(typeof(System.Enum).IsAssignableFrom(fi.mFieldInfo.FieldType))
  104. {
  105. value = Enum.Parse(fi.mFieldInfo.FieldType, value.ToString());
  106. }
  107. else
  108. {
  109. value = Convert.ChangeType(value, fi.mFieldInfo.FieldType);
  110. }
  111. fi.mFieldInfo.SetValue(data, value);
  112. }
  113. break;
  114. default:
  115. {
  116. object value = Parser.StringToObject(icell.StringCellValue, fi.mFieldInfo.FieldType);
  117. fi.mFieldInfo.SetValue(data, value);
  118. }
  119. break;
  120. }
  121. }
  122. }
  123. ret.Add(data);
  124. }
  125. else
  126. {
  127. break;
  128. }
  129. }
  130. return ret;
  131. }
  132. catch (Exception err)
  133. {
  134. throw new Exception(string.Format("LoadSheetError: 表名 = {0} 行 = {1} ,列= {2}, 列名={3}, 列内容={4} ,error = {5}",
  135. sheet.SheetName, ri + 1, ci + 1, rowName, rowContent, err.ToString()));
  136. }
  137. }
  138. }
  139. }