ObjDataOperationClass.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Extensometer.Data;
  10. using Extensometer.Model;
  11. using Extensometer.Model.ResultModel;
  12. using Microsoft.Office.Interop.Excel;
  13. namespace Extensometer.BLL
  14. {
  15. public class ObjDataOperationClass
  16. {
  17. private static ObjDataOperationClass ObjDataOperation = new ObjDataOperationClass();
  18. private ObjDataOperationClass()
  19. { }
  20. public static ObjDataOperationClass GetObjDataOperationClass()
  21. {
  22. return ObjDataOperation;
  23. }
  24. private string ObjDbPath = string.Empty;
  25. /// <summary>
  26. /// 创建
  27. /// </summary>
  28. /// <param name="DbPath"></param>
  29. public void CreatObjDbFile(string DbPath)
  30. {
  31. using (var Db = new ObjectDBTable(DbPath))
  32. {
  33. ObjDbPath = DbPath;
  34. }
  35. }
  36. /// <summary>
  37. /// 加载
  38. /// </summary>
  39. /// <param name="DbPath"></param>
  40. public void LoadObjDbFile(string DbPath)
  41. {
  42. ObjDbPath = DbPath;
  43. LoadDll();
  44. }
  45. public void LoadDll()
  46. {
  47. Thread thread = new Thread(()=> {
  48. using (var Db = new ObjectDBTable(ObjDbPath))
  49. {
  50. }
  51. });
  52. thread.Start();
  53. }
  54. /// <summary>
  55. /// 全查询
  56. /// </summary>
  57. public List<ObjectData> CheckObjDb()
  58. {
  59. List<ObjectData> result = new List<ObjectData>();
  60. using (var Db = new ObjectDBTable(ObjDbPath))
  61. {
  62. result = Db.ObjectDatas.OrderBy(e => e.GaugeLeng).ToList();
  63. }
  64. return result;
  65. }
  66. /// <summary>
  67. /// 添加一条数据
  68. /// </summary>
  69. /// <param name="objectData"></param>
  70. /// <returns></returns>
  71. public ResultModel InsertObjDb(ObjectData objectData)
  72. {
  73. ResultModel result = new ResultModel();
  74. using (var Db = new ObjectDBTable(ObjDbPath))
  75. {
  76. if(Db.Insert(objectData)>0)
  77. {
  78. result.ErrCode = ErrCodeList.Success;
  79. result.ErrMessage = "添加成功";
  80. }
  81. }
  82. return result;
  83. }
  84. /// <summary>
  85. /// List<ObjectData>转成Exl表格
  86. /// </summary>
  87. /// <param name="list"></param>
  88. /// <param name="FilePath"></param>
  89. /// <returns></returns>
  90. public ResultModel DataExport(List<ObjectData> list,string FilePath)
  91. {
  92. ResultModel resultModel = new ResultModel();
  93. if (list.Count > 0)
  94. {
  95. object misValue = Missing.Value;
  96. Application xlApp = new Application();
  97. Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
  98. Worksheet xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);
  99. PropertyInfo[] props = GetPropertyInfoArray();
  100. for (int i = 0; i < props.Length; i++)
  101. {
  102. xlWorkSheet.Cells[1, i + 1] = props[i].Name; //write the column name
  103. }
  104. for (int i = 0; i < list.Count; i++)
  105. {
  106. xlWorkSheet.Cells[i + 2, 1] = list[i].GaugeLeng.ToString();
  107. xlWorkSheet.Cells[i + 2, 2] = list[i].Time.ToString();
  108. xlWorkSheet.Cells[i + 2, 3] = list[i].Point1x.ToString();
  109. xlWorkSheet.Cells[i + 2, 4] = list[i].Point1y.ToString();
  110. xlWorkSheet.Cells[i + 2, 5] = list[i].Point2x.ToString();
  111. xlWorkSheet.Cells[i + 2, 6] = list[i].Point2y.ToString();
  112. xlWorkSheet.Cells[i + 2, 7] = list[i].StrainValue.ToString();
  113. xlWorkSheet.Cells[i + 2, 8] = list[i].Distance.ToString();
  114. xlWorkSheet.Cells[i + 2, 9] = list[i].Displacement.ToString();
  115. xlWorkSheet.Cells[i + 2, 10] = list[i].ForceValue.ToString();
  116. }
  117. try
  118. {
  119. xlWorkBook.SaveAs(FilePath, XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
  120. xlWorkBook.Close(true, misValue, misValue);
  121. xlApp.Quit();
  122. }
  123. catch
  124. { }
  125. }
  126. return resultModel;
  127. }
  128. /// <summary>
  129. /// 查询数据库路径是否为空
  130. /// </summary>
  131. /// <returns></returns>
  132. public ResultModel CheckDataPathIsEmpty()
  133. {
  134. ResultModel resultModel = new ResultModel();
  135. try
  136. {
  137. if(string.IsNullOrEmpty(ObjDbPath))
  138. {
  139. resultModel.ErrCode = ErrCodeList.PathEmpty;
  140. resultModel.ErrMessage = "数据路径为空";
  141. }
  142. else
  143. {
  144. resultModel.ErrCode = ErrCodeList.Success;
  145. resultModel.ErrMessage = "";
  146. }
  147. }
  148. catch
  149. {
  150. resultModel.ErrCode = ErrCodeList.OtherErr;
  151. resultModel.ErrMessage = "未知错误";
  152. }
  153. return resultModel;
  154. }
  155. /// <summary>
  156. /// 反射获取类型的所有属性
  157. /// </summary>
  158. /// <returns></returns>
  159. private PropertyInfo[] GetPropertyInfoArray()
  160. {
  161. PropertyInfo[] props = null;
  162. try
  163. {
  164. Type type = typeof(ObjectData);
  165. object obj = Activator.CreateInstance(type);
  166. props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
  167. }
  168. catch
  169. { }
  170. return props;
  171. }
  172. }
  173. }