using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; using Extensometer.Data; using Extensometer.Model; using Extensometer.Model.ResultModel; using Microsoft.Office.Interop.Excel; namespace Extensometer.BLL { public class ObjDataOperationClass { private static ObjDataOperationClass ObjDataOperation = new ObjDataOperationClass(); private ObjDataOperationClass() { } public static ObjDataOperationClass GetObjDataOperationClass() { return ObjDataOperation; } private string ObjDbPath = string.Empty; /// /// 创建 /// /// public void CreatObjDbFile(string DbPath) { using (var Db = new ObjectDBTable(DbPath)) { ObjDbPath = DbPath; } } /// /// 加载 /// /// public void LoadObjDbFile(string DbPath) { ObjDbPath = DbPath; LoadDll(); } public void LoadDll() { Thread thread = new Thread(()=> { using (var Db = new ObjectDBTable(ObjDbPath)) { } }); thread.Start(); } /// /// 全查询 /// public List CheckObjDb() { List result = new List(); using (var Db = new ObjectDBTable(ObjDbPath)) { result = Db.ObjectDatas.OrderBy(e => e.GaugeLeng).ToList(); } return result; } /// /// 添加一条数据 /// /// /// public ResultModel InsertObjDb(ObjectData objectData) { ResultModel result = new ResultModel(); using (var Db = new ObjectDBTable(ObjDbPath)) { if(Db.Insert(objectData)>0) { result.ErrCode = ErrCodeList.Success; result.ErrMessage = "添加成功"; } } return result; } /// /// List转成Exl表格 /// /// /// /// public ResultModel DataExport(List list,string FilePath) { ResultModel resultModel = new ResultModel(); if (list.Count > 0) { object misValue = Missing.Value; Application xlApp = new Application(); Workbook xlWorkBook = xlApp.Workbooks.Add(misValue); Worksheet xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1); PropertyInfo[] props = GetPropertyInfoArray(); for (int i = 0; i < props.Length; i++) { xlWorkSheet.Cells[1, i + 1] = props[i].Name; //write the column name } for (int i = 0; i < list.Count; i++) { xlWorkSheet.Cells[i + 2, 1] = list[i].GaugeLeng.ToString(); xlWorkSheet.Cells[i + 2, 2] = list[i].Time.ToString(); xlWorkSheet.Cells[i + 2, 3] = list[i].Point1x.ToString(); xlWorkSheet.Cells[i + 2, 4] = list[i].Point1y.ToString(); xlWorkSheet.Cells[i + 2, 5] = list[i].Point2x.ToString(); xlWorkSheet.Cells[i + 2, 6] = list[i].Point2y.ToString(); xlWorkSheet.Cells[i + 2, 7] = list[i].StrainValue.ToString(); xlWorkSheet.Cells[i + 2, 8] = list[i].Distance.ToString(); xlWorkSheet.Cells[i + 2, 9] = list[i].Displacement.ToString(); xlWorkSheet.Cells[i + 2, 10] = list[i].ForceValue.ToString(); } try { xlWorkBook.SaveAs(FilePath, XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); } catch { } } return resultModel; } /// /// 查询数据库路径是否为空 /// /// public ResultModel CheckDataPathIsEmpty() { ResultModel resultModel = new ResultModel(); try { if(string.IsNullOrEmpty(ObjDbPath)) { resultModel.ErrCode = ErrCodeList.PathEmpty; resultModel.ErrMessage = "数据路径为空"; } else { resultModel.ErrCode = ErrCodeList.Success; resultModel.ErrMessage = ""; } } catch { resultModel.ErrCode = ErrCodeList.OtherErr; resultModel.ErrMessage = "未知错误"; } return resultModel; } /// /// 反射获取类型的所有属性 /// /// private PropertyInfo[] GetPropertyInfoArray() { PropertyInfo[] props = null; try { Type type = typeof(ObjectData); object obj = Activator.CreateInstance(type); props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); } catch { } return props; } } }