private void Btn_TrcukOD_Click(object sender, EventArgs e) { if (PublicVariable.SplitedFilePath.Count > 0) { Dgv_StatisticSetting_CellLeave(this, null); //添加要生成的文件的标题行信息 MatrixStruce = new PublicVariable.StatisticResultStruce(); string title = ""; foreach (PublicVariable.StatisticSetting v in PublicVariable.statisticSettings) { title += v.FieldName + PublicVariable.StrConnector; } MatrixStruce.Title = title + "计数"; MatrixStruce.Matrix = new Dictionary <string, List <double> >(); for (int i = 0; i < PublicVariable.SplitedFilePath.Count; i++) { Tssl_ProgressInfo.Text = i + 1 + "/" + PublicVariable.SplitedFilePath.Count; FileOperate.StatisticODData(PublicVariable.SplitedFilePath[i], MatrixStruce, tssl_Progress, Tssl_FileSize); } Frm_DataView frm_DataView = new Frm_DataView(); frm_DataView.Show(); SendContentsEvent += frm_DataView.FillDataGridview; SendContentsEvent?.Invoke(MatrixStruce); SendContentsEvent -= frm_DataView.FillDataGridview; } else { MessageBox.Show("请在右侧列表框中选中要统计的文件!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
public void FillDataGridview(PublicVariable.StatisticResultStruce matrix) { DataTable dt = new DataTable("Matrix"); string[] tempStr = matrix.Title.Split(PublicVariable.CharSplitor); for (int i = 0; i < tempStr.Length; i++) { dt.Columns.Add(tempStr[i]); } foreach (string key in matrix.Matrix.Keys) { DataRow dataRow = dt.NewRow(); string[] tempkey = key.Split(PublicVariable.CharSplitor); for (int i = 0; i < tempkey.Length; i++) { dataRow[i] = tempkey[i]; } for (int i = 0; i < matrix.Matrix[key].Count; i++) { dataRow[i + tempkey.Length] = matrix.Matrix[key][i]; } dt.Rows.Add(dataRow); } Dgv_DataView.DataSource = dt; for (int i = 0; i < Dgv_DataView.ColumnCount; i++) { Dgv_DataView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; } Tssl_MatrixSize.Text = Dgv_DataView.Rows.Count + "*" + Dgv_DataView.Columns.Count; }
private void Btn_TrcukOD_Click(object sender, EventArgs e) { Btn_TrcukOD.Enabled = false; if (PublicVariable.SplitedFilePath.Count > 0) { Dgv_StatisticSetting_CellLeave(this, null); //添加要生成的文件的标题行信息 MatrixStruce = new PublicVariable.StatisticResultStruce(); string title = ""; foreach (PublicVariable.StatisticSetting v in PublicVariable.statisticSettings) { title += v.FieldName + PublicVariable.StrConnector; } MatrixStruce.Title = title + "计数"; MatrixStruce.Matrix = new Dictionary <string, List <double> >(); for (int i = 0; i < PublicVariable.SplitedFilePath.Count; i++) { Tssl_ProgressInfo.Text = i + 1 + "/" + PublicVariable.SplitedFilePath.Count; FileOperate.StatisticODData(PublicVariable.SplitedFilePath[i], MatrixStruce, tssl_Progress, Tssl_FileSize); } Frm_DataView frm_DataView = new Frm_DataView(); frm_DataView.Show(); SendContentsEvent += frm_DataView.FillDataGridview; if (MatrixStruce.Matrix.Count >= 10000) { string message = string.Format("共有{0}行数据,数据导入较慢,是否继续导入!\r\n 是\t继续导入 \r\n否\t直接导出为文件", MatrixStruce.Matrix.Count); string dialogResult = MessageBox.Show(message, "导入提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question).ToString(); if (dialogResult == "Yes") { SendContentsEvent?.Invoke(MatrixStruce); } } else { SendContentsEvent?.Invoke(MatrixStruce); } SendContentsEvent -= frm_DataView.FillDataGridview; } else { MessageBox.Show("请在右侧列表框中选中要统计的文件!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } Btn_TrcukOD.Enabled = true; }
public void SaveFile(string filePath, PublicVariable.StatisticResultStruce contents) { StreamWriter sw = new StreamWriter(filePath, false, Encoding.Default); sw.WriteLine(contents.Title + "车辆数"); foreach (string s in contents.Matrix.Keys) { string tempLine = s.ToString(); tempLine += string.Join(",", contents.Matrix[s]); sw.WriteLine(tempLine); } sw.Flush(); sw.Close(); string messageinfo = string.Format("已完成!\r\n保存路径:{0}", filePath); MessageBox.Show(messageinfo, "系统提示", MessageBoxButtons.OK); }
private void Btn_temp_Click(object sender, EventArgs e) { if (PublicVariable.SplitedFilePath.Count == 0) { MessageBox.Show("请在右侧列表框中选中要统计的文件!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } //添加要生成的文件的标题行信息 MatrixStruce = new PublicVariable.StatisticResultStruce { Title = "日期,时间,车型,总里程,计数", Matrix = new Dictionary <string, List <double> >() }; for (int i = 0; i < PublicVariable.SplitedFilePath.Count; i++) { Tssl_ProgressInfo.Text = i + 1 + "/" + PublicVariable.SplitedFilePath.Count; StreamReader sr = new StreamReader(PublicVariable.SplitedFilePath[i], Encoding.Default); string line = ""; double n = 0; while ((line = sr.ReadLine()) != null) { n += 1; try { string[] splited_line = line.Split(new char[] { '\t', ' ' }); double miles = Convert.ToDouble(splited_line[8]); int veh = Convert.ToInt32(splited_line[6]); if (miles <= 0 | veh <= 4) { continue; } string key = splited_line[4] + "," + splited_line[5].Substring(0, 2) + "," + splited_line[14]; if (MatrixStruce.Matrix.ContainsKey(key)) { MatrixStruce.Matrix[key][0] += miles; MatrixStruce.Matrix[key][1] += 1; } else { MatrixStruce.Matrix.Add(key, new List <double>() { miles, 1 }); } } catch (Exception) { continue; } if (n % 10000 == 0) { tssl_Progress.Text = n.ToString(); Application.DoEvents(); } } sr.Close(); } Frm_DataView frm_DataView = new Frm_DataView(); frm_DataView.Show(); SendContentsEvent += frm_DataView.FillDataGridview; SendContentsEvent?.Invoke(MatrixStruce); SendContentsEvent -= frm_DataView.FillDataGridview; }
public PublicVariable.StatisticResultStruce StatisticODData(string filePath, PublicVariable.StatisticResultStruce statisticResult, ToolStripLabel stripLabel, ToolStripLabel label = null) { if (label != null) { PrintFileInfo(label, filePath); } int n = 0; int errorLineCount = 0; string[] lineCols; StreamReader sr = new StreamReader(filePath, Encoding.Default); string line; List <int> lstIDindex = new List <int>(); foreach (PublicVariable.StatisticSetting v in PublicVariable.statisticSettings) { if (v.FieldSettingMethod == PublicVariable.DGVStruck.ID) { lstIDindex.Add(v.RealIndex); } } List <int[]> lstSumIndex = new List <int[]>(); foreach (PublicVariable.StatisticSetting v in PublicVariable.statisticSettings) { int[] temp = new int[2]; if (v.FieldSettingMethod == PublicVariable.DGVStruck.Sum) { temp[0] = v.VirtualIndex; temp[1] = v.RealIndex; lstSumIndex.Add(temp); } } while ((line = sr.ReadLine()) != null) { n += 1; string ID = ""; try { lineCols = line.Split(PublicVariable.Separator); foreach (int index in lstIDindex) { ID += ID != "" ? PublicVariable.StrConnector + lineCols[index] : lineCols[index]; } sum(ID, lineCols); } catch (Exception) { //_ = sr.ReadLine(); if (statisticResult.Matrix.ContainsKey(ID) && statisticResult.Matrix[ID].Count == 0) { statisticResult.Matrix.Remove(ID); } errorLineCount += 1; continue; } if (n % 10000 == 0) { stripLabel.Text = n + "/" + errorLineCount; Application.DoEvents(); } } sr.Close(); return(statisticResult); void sum(string ID, string[] str) { if (statisticResult.Matrix.ContainsKey(ID)) { //用来统计该ID的数量 statisticResult.Matrix[ID][statisticResult.Matrix[ID].Count - 1] += 1; foreach (int[] index in lstSumIndex) { statisticResult.Matrix[ID][index[0]] += Convert.ToDouble(str[index[1]]); } } else { PublicVariable.StatisticResultStruce statisticResultStruc = new PublicVariable.StatisticResultStruce { Matrix = new Dictionary <string, List <double> >() }; List <double> matrix = new List <double>(); statisticResult.Matrix.Add(ID, matrix); foreach (int[] index in lstSumIndex) { statisticResult.Matrix[ID].Add(Convert.ToDouble(str[index[1]])); } //用来统计该ID有多少辆车 matrix.Add(1); } } }