private void toolStripMenuItem1_Click(object sender, EventArgs e) { SaveFileDialog sf = new SaveFileDialog(); if (sf.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string path = sf.FileName.ToString(); OutPutFile outPutFile = new OutPutFile(); MrDataShow mrDataShow = new MrDataShow(); MRTableList mRTableList = new MRTableList(); DataTable dt2 = new DataTable(); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } foreach (TreeNode item in treeView1.Nodes) { foreach (TreeNode item2 in item.Nodes) { string name = item2.Text; mRTableList.tabletype = item.Text; mRTableList.tableName = item2.Text; dt2 = mrDataShow.GetTableData(mRTableList); dt2.TableName = item.Name + "-" + item2.Text; string str = outPutFile.SaveCSV(dt2, string.Format("{0}//{1}.csv", path, dt2.TableName)); } } MessageBox.Show("导出成功!", "提示"); } }
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { List <MRTableList> mrTable1 = SingletonMrData.mrTableL; string tabelName = treeView1.SelectedNode.Text; List <string> tableTypeL = new List <string>() { "MRE", "MRO", "MRS" }; if (tableTypeL.Contains(tabelName)) { return; } string tabelType = treeView1.SelectedNode.Parent.Text; //List<string> clolumnL = new List<string>();//需要呈现的所有列 MRTableList mRTableList = new MRTableList();// mRTableList.tabletype = tabelType; mRTableList.tableName = tabelName; MrDataShow mrDataShow = new MrDataShow(); dt = mrDataShow.GetTableData(mRTableList); dt.TableName = tabelName; dataGridView1.DataSource = dt; this.Text = string.Format("MR数据呈现 {0}", tabelName); }
/// <summary> /// 得到一张表的全部信息 /// </summary> /// <param name="mRTa"> 表头信息</param> /// <param name="column">列名信息</param> /// <returns></returns> public DataTable GetTableData(MRTableList mRTa) { List <MrTableAllColumn> mrAllcolumnList = SingletonMrData.mrAllcolumnL;//得到对应的dataTable List <MrTableAllColumn> oneTable = (from q in mrAllcolumnList where q.mrName == mRTa.tableName && q.tabletype == mRTa.tabletype select q).ToList(); string[] smrList = oneTable.First().smrList.Split();//得到该表的第一条数据 DataTable dt = new DataTable(); List <string> deleteCloumn = new List <string> { "KPid", "xmlName", "smrList", "vList" }; dt = TtoDataTable.ListtoDataTable(oneTable); int m = dt.Columns.Count; int n = m; foreach (var item2 in smrList) { //向dt中添加smr的数据 dt.Columns.Add(item2); } int i = 0; int j = 0; foreach (MrTableAllColumn item in oneTable) { foreach (var item2 in smrList) { dt.Rows[i][m] = item.vList.Split(' ')[j]; j++; m++; } j = 0; m = n; i++; } //删除不需要呈现的列 dt.Columns.Remove("KPid"); dt.Columns.Remove("fileFormatVersion"); dt.Columns.Remove("xmlName"); dt.Columns.Remove("vList"); dt.Columns.Remove("smrList"); dt.Columns.Remove("tabletype"); return(dt); }
/// <summary> /// 表,表头信息赋值 /// </summary> public void MrInitializeComponent(string dbname) { mrAllcolumnL.Clear(); mrTableL.Clear(); using (var db = new LROSRDbContext(dbname)) { db.Configuration.AutoDetectChangesEnabled = false; db.Configuration.ValidateOnSaveEnabled = false; var column = from q in db.MrTableAllColumn select q; var table1 = from q in column group q by new { q.fileFormatVersion, q.tabletype, q.mrName } into m select m.Key; var table2 = from q in table1 select new MRTableList { tabletype = q.tabletype, Version = q.fileFormatVersion, tableName = q.mrName }; foreach (MrTableAllColumn item in column) { MrTableAllColumn mrTableAllColumn = SingletonMrData.DeepCopy <MrTableAllColumn>(item); mrAllcolumnL.Add(mrTableAllColumn); } foreach (MRTableList item in table2) { MRTableList mRTableList = SingletonMrData.DeepCopy <MRTableList>(item); mrTableL.Add(item); } } }
/// <summary> /// 得到MR列表 /// </summary> /// <param name="path">文件夹路径</param> /// <param name="type">文件类型</param> /// <returns>MR列表</returns> public static List <MRTableList> GetMrTableList(string path) { string[] filenames = Directory.GetFiles(path); //是否是第一次遍历MRE这个XML bool isFristMRE = true; //是否是第一次遍历MRO这个XML bool isFristMRO = true; //是否是第一次遍历MRS这个XML bool isFristMRS = true; //默认id是从1开始的 int id = 1; //新建一个列表 List <MRTableList> tablelist = new List <MRTableList>(); foreach (string file in filenames) { //判断xml文件是否是MRE类型的 以下if判断类似 //Console.WriteLine("file文件的路径:{0}", file); //文件是MRE的处理方法 if (file.Contains("MRE") && isFristMRE) { isFristMRE = false; XmlDocument doc = new XmlDocument(); //string myPath = path + "\\" + file; //Console.WriteLine(file); doc.Load(file); XmlNode xn = doc.SelectSingleNode("bulkPmMrDataFile"); //Console.WriteLine("xn的值为:{0}", xn); XmlNodeList xnlist = xn.ChildNodes; XmlNode xneNB = xnlist[1]; //Console.WriteLine(xneNB); XmlNodeList xnl = xneNB.ChildNodes; foreach (XmlNode xn1 in xnl) { //Console.WriteLine("smr的值:{0}", xn1); MRTableList tableModel = new MRTableList(); /* * tableModel.columnName.Add("xiaogang"); * tableModel.columnName.Add("xiaogang"); * tableModel.columnName.Add("xiaogang"); * Console.WriteLine(tableModel.columnName); * */ XmlElement xe = (XmlElement)xn1; XmlNodeList xnlChild = xe.ChildNodes; string temp = xnlChild.Item(0).InnerText; //Console.WriteLine("temp的值为:{0}", temp); string[] sArray = temp.Split(' '); List <string> tempList = new List <string>(); for (int i = 0; i < sArray.Length; i++) { tempList.Add(sArray[i]); } tableModel.columnName = tempList; string str1 = sArray[0].Remove(0, 3); tableModel.tableName = str1; tableModel.tabletype = "MRE"; tableModel.id = id; tablelist.Add(tableModel); id++; } } //文件是MRO的处理方法 else if (file.Contains("MRO") && isFristMRO) { isFristMRO = false; XmlDocument doc = new XmlDocument(); doc.Load(file); XmlNode xn = doc.SelectSingleNode("bulkPmMrDataFile"); //Console.WriteLine("xn的值为:{0}", xn); XmlNodeList xnlist = xn.ChildNodes; XmlNode xneNB = xnlist[1]; //Console.WriteLine(xneNB); XmlNodeList xnl = xneNB.ChildNodes; foreach (XmlNode xn1 in xnl) { MRTableList tableModel = new MRTableList(); XmlElement xe = (XmlElement)xn1; XmlNodeList xnlChild = xe.ChildNodes; string temp = xnlChild.Item(0).InnerText; string[] sArray = temp.Split(' '); List <string> tempList = new List <string>(); for (int i = 0; i < sArray.Length; i++) { tempList.Add(sArray[i]); } tableModel.columnName = tempList; string str1 = sArray[0].Remove(0, 3); tableModel.tableName = str1; tableModel.tabletype = "MRO"; tableModel.id = id; tablelist.Add(tableModel); id++; } } //文件是MRS的处理方法 else if (file.Contains("MRS") && isFristMRS) { isFristMRS = false; XmlDocument doc = new XmlDocument(); doc.Load(file); XmlNode xn = doc.SelectSingleNode("bulkPmMrDataFile"); Console.WriteLine("xn的值为:{0}", xn); XmlNodeList xnlist = xn.ChildNodes; XmlNode xneNB = xnlist[1]; Console.WriteLine(xneNB); XmlNodeList xnl = xneNB.ChildNodes; foreach (XmlNode xn1 in xnl) { MRTableList tableModel = new MRTableList(); XmlElement xe = (XmlElement)xn1; //tableName取值 tableModel.tableName = xe.GetAttribute("mrName").ToString(); XmlNodeList xnlChild = xe.ChildNodes; string temp = xnlChild.Item(0).InnerText; string[] sArray = temp.Split(' '); List <string> tempList = new List <string>(); for (int i = 0; i < sArray.Length; i++) { tempList.Add(sArray[i]); } tableModel.columnName = tempList; tableModel.tabletype = "MRS"; tableModel.id = id; tablelist.Add(tableModel); id++; } } } return(tablelist); }
/// <summary> /// Mr数据入库 /// </summary> /// <param name="path">压缩文件,文件夹路径</param> /// <returns>入库成功</returns> public bool MrInput(string path, string deName) { int xmlNumber = 0; int dbNUmber = 0; //初始化XML文件夹 string xmlPath = @"..\..\MRXMLFile";//路径xml文件夹 ClearXMLFile(xmlPath); //解压 bool a = false;//解压是否成功 string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories); if (files.Length == 0) { return(false); } foreach (string file in files) { if (Path.GetExtension(file) == ".zip") { a = Decompress(file, xmlPath); if (!a) { return(false); } } } //读取XML List <MrTableAllColumn> mrTableAllColumn = new List <MrTableAllColumn>(); string[] xmlFiles = Directory.GetFiles(xmlPath, "*.*", SearchOption.AllDirectories); if (xmlFiles.Length == 0) { return(false); } foreach (string file in xmlFiles) { if (Path.GetExtension(file) == ".xml") { mrTableAllColumn.AddRange(GetMrtable(file)); } } xmlNumber = mrTableAllColumn.Count(); //Mr数据入库 using (var db = new LROSRDbContext(deName)) { db.Configuration.AutoDetectChangesEnabled = false; db.Configuration.ValidateOnSaveEnabled = false; //db.anto int KPid = 0;//设置主键ID int columnNumber = db.MrTableAllColumn.Count(); if (columnNumber != 0) { KPid = db.MrTableAllColumn.Select(q => q.KPid).Max() + 1; } foreach (MrTableAllColumn item in mrTableAllColumn) { item.KPid = KPid; KPid++; } db.MrTableAllColumn.AddRange(mrTableAllColumn); try { dbNUmber = db.SaveChanges(); if (dbNUmber != xmlNumber) { return(false); } } catch (Exception ex) { throw ex; } //得到列数据和表头数据的列表 var column = from q in db.MrTableAllColumn select q; var table1 = from q in column group q by new { q.fileFormatVersion, q.tabletype, q.mrName } into m select m.Key; var table2 = from q in table1 select new MRTableList { tabletype = q.tabletype, Version = q.fileFormatVersion, tableName = q.mrName }; foreach (MrTableAllColumn item in column) { MrTableAllColumn mrTableAllColumn1 = SingletonMrData.DeepCopy <MrTableAllColumn>(item); SingletonMrData.mrAllcolumnL.Add(mrTableAllColumn1); } foreach (MRTableList item in table2) { MRTableList mRTableList = SingletonMrData.DeepCopy <MRTableList>(item); SingletonMrData.mrTableL.Add(item); } } return(true); }
//开始按钮的事件 private void StartButton_Click(object sender, EventArgs e) { /* * string startTimeStr = cmbDateTimePicker1.Value.ToString("yyyyMMddHHmm"); * string endTimeStr = cmbDateTimePicker2.Value.ToString("yyyyMMddHHmm"); * double startTime = Convert.ToDouble(startTimeStr); * double endTime = Convert.ToDouble(endTimeStr); */ if (cmbTextBox1.Text == "" || cmbTextBox2.Text == "" || cmbCounter.Text == "") { MessageBox.Show("请选择指标或输入门限值!!!"); return; } DateTime startTime = cmbDateTimePicker1.Value; DateTime endTime = cmbDateTimePicker2.Value; string minStr = cmbTextBox1.Text; string maxStr = cmbTextBox2.Text; double mindb = Convert.ToDouble(minStr); double maxdb = Convert.ToDouble(maxStr); //临时变量取到类型和表名 string tabelType = cmbType.Text; string tabelName = cmbTable.Text; MRTableList mRTableList = new MRTableList();// mRTableList.tabletype = tabelType; mRTableList.tableName = tabelName; MrDataShow mrDataShow = new MrDataShow(); //这dt装着所有的数据 dt = mrDataShow.GetTableData(mRTableList); //指标 string convert = cmbCounter.Text; //新建表dt1 DataTable dtNew = dt.Clone(); List <DataRow> aList = new List <DataRow>(); for (int i = 0; i < dt.Rows.Count; i++) { //start大于;end小于 string startTime1 = dt.Rows[i]["startTime"].ToString(); string endTime1 = dt.Rows[i]["endTime"].ToString(); startTime1.Remove(16); endTime1.Remove(16); DateTime startTmDate; DateTime endTmDate; DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo(); dtFormat.ShortDatePattern = "yyyy-MM-ddThh:mm"; startTmDate = Convert.ToDateTime(startTime1, dtFormat); endTmDate = Convert.ToDateTime(endTime1, dtFormat); string convertStr = dt.Rows[i][convert].ToString(); //将得到的值转化为double double converDouble; if (double.TryParse(convertStr, out converDouble) == true) { if ((startTmDate >= startTime) && (endTmDate <= endTime) && (converDouble >= mindb) && (converDouble <= maxdb)) { aList.Add(dt.Rows[i]); } } } //新表赋值 for (int i = 0; i < aList.Count; i++) { dtNew.ImportRow(aList[i]); } //新表提取数据 DataTable dtNew1 = dtNew.DefaultView.ToTable(false, new string[] { "id", convert }); ShowDataGridView.DataSource = dtNew1; List <string> idList = new List <string>(); List <double> conlist = new List <double>(); foreach (DataRow rowTemp in dtNew1.Rows) { idList.Add((string)rowTemp[0]); double data; if (double.TryParse((string)rowTemp[1], out data) == true) { conlist.Add(data); } } chart1.Series[0].Points.DataBindXY(idList, conlist); }