public Form1(string[] args) { InitializeComponent(); if (args.Length == 2) // svn 整合 { CompareFile(args[0], args[1]); this.Dispose(); } else if (args.Length == 0) // 批量导出 { string client1 = string.Empty; string client2 = string.Empty; string files = string.Empty; // 读取ini文件 string strIniFile = Application.StartupPath + "/批量输出.ini"; StringBuilder sb = new StringBuilder(25600); FileFolderHelper.ReadIniFile("main", "client1", "", sb, 255, strIniFile); client1 = sb.ToString(); FileFolderHelper.ReadIniFile("main", "client2", "", sb, 255, strIniFile); client2 = sb.ToString(); FileFolderHelper.ReadIniFile("main", "files", "", sb, 25600, strIniFile); files = sb.ToString(); string[] arrFiles = files.Split(','); textOutputCfg.Text = string.Empty; foreach (string file in arrFiles) { textOutputCfg.Text += file; textOutputCfg.Text += "\r\n"; } } }
private void button1_Click(object sender, EventArgs e) // 保存 { string targetsavefile = FileFolderHelper.BrowseFileSave(); FileFolderHelper.StringToFile(GetDataGridViewContent(), targetsavefile); }
static public bool DiffGenTab(string file1, string file2, string strkeys, string namefield, string rowcondition, ref string output, ref string output_basic, ref List <string> diffCols1, ref List <string> diffCols2) { ArrayList outputlines = new ArrayList(); string[] keys = strkeys.Split(new char[] { ',' }); int[] keyIndices = new int[keys.Length]; string content1 = FileFolderHelper.FileToString(file1); string[] lines1 = content1.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); // 排序文件1 if (file1.ToLower().EndsWith("other.tab")) { Array.Sort(lines1, CompareByKey); } string[] colnames1 = lines1[0].Split(new char[] { '\t' }); bool[] _1exists = new bool[colnames1.Length]; for (int i = 0; i < _1exists.Length; i++) { _1exists[i] = false; } // namefield处理 string[] nameFields = new string[0]; int[] namefieldsindices = new int[0]; if (namefield.Length > 0) { nameFields = namefield.Split(','); namefieldsindices = new int[nameFields.Length]; for (int i = 0; i < nameFields.Length; i++) { for (int j = 0; j < colnames1.Length; j++) { if (colnames1[j] == nameFields[i]) { namefieldsindices[i] = j; break; } } } } // keys for (int i = 0; i < keys.Length; i++) { for (int j = 0; j < colnames1.Length; j++) { if (keys[i] == colnames1[j]) { keyIndices[i] = j; break; } } } string content2 = FileFolderHelper.FileToString(file2); string[] lines2 = content2.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); // 过滤行 try { RowFilter(rowcondition, content1, ref lines1, content2, ref lines2); } catch (System.Exception ex) { string err = string.Format("过滤行时发生异常:{0}", ex.Message); MessageBox.Show(err); } // 排序文件2 if (file2.ToLower().EndsWith("other.tab")) { Array.Sort(lines2, CompareByKey); } string[] colnames2 = lines2[0].Split(new char[] { '\t' }); bool[] _2exists = new bool[colnames2.Length]; for (int i = 0; i < _2exists.Length; i++) { _2exists[i] = false; } for (int i = 0; i < colnames1.Length; i++) { for (int j = 0; j < colnames2.Length; j++) { if (colnames1[i] == colnames2[j]) { _1exists[i] = true; _2exists[j] = true; break; } } } // 找出纯粹的列差异 for (int i = 0; i < colnames1.Length; i++) { if (!_1exists[i]) { diffCols1.Add(colnames1[i]); } } for (int i = 0; i < colnames2.Length; i++) { if (!_2exists[i]) { diffCols2.Add(colnames2[i]); } } // 输出 //string outputcontent = string.Empty; StringBuilder sb = new StringBuilder(); ArrayList useful_indices_1 = new ArrayList(); ArrayList useful_indices_2 = new ArrayList(); for (int i = 0; i < _1exists.Length; i++) { if (_1exists[i]) { useful_indices_1.Add(i); } } for (int i = 0; i < _2exists.Length; i++) { if (_2exists[i]) { useful_indices_2.Add(i); } } foreach (int i in useful_indices_1) { sb.Append(colnames1[i]); sb.Append('\t'); } sb = sb.Remove(sb.Length - 1, 1); outputlines.Add(sb.ToString()); sb = new StringBuilder(); // 决定最后导出是否导出某列 bool[] bExportCols = new bool[useful_indices_1.Count]; for (int i = 0; i < bExportCols.Length; i++) { bExportCols[i] = false; } foreach (int ikey in keyIndices) { bExportCols[ikey] = true; } foreach (int iname in namefieldsindices) { bExportCols[iname] = true; } // 内容比对 int rowindex1 = 1, rowindex2 = 1; bool bThisLineDiff; FIND_AGAIN_IN_1: bThisLineDiff = false; //if (rowindex1 >= lines1.Length) // goto FINALIZE; string thisLine1 = string.Empty; string[] cols1 = null; if (rowindex1 < lines1.Length) { thisLine1 = lines1[rowindex1]; cols1 = thisLine1.Split(new char[] { '\t' }); } else { cols1 = new string[colnames1.Length]; } //string[] cols1 = thisLine1.Split(new char[] { '\t' }); string keycontent1 = string.Empty; for (int i = 0; i < keyIndices.Length; i++) { keycontent1 += cols1[keyIndices[i]]; keycontent1 += "\t"; } FIND_AGAIN_IN_2: string thisLine2 = string.Empty; string keycontent2 = string.Empty; string[] cols2 = null; //if (rowindex2 >= lines2.Length) // goto FINALIZE; if (rowindex2 < lines2.Length) { thisLine2 = lines2[rowindex2]; cols2 = thisLine2.Split(new char[] { '\t' }); } else { cols2 = new string[colnames2.Length]; } //cols2 = thisLine2.Split(new char[] { '\t' }); for (int i = 0; i < keyIndices.Length; i++) { keycontent2 += cols2[keyIndices[i]]; keycontent2 += "\t"; } // check if (rowindex1 >= lines1.Length && rowindex2 >= lines2.Length) { goto FINALIZE; } // compare switch (CompareKey(keycontent1, keycontent2)) { case 0: rowindex1++; rowindex2++; goto OUTPUT_THIS_LINE_BOTH; case 1: rowindex2++; //cols1 = new string[useful_indices_1.Count]; cols1 = new string[colnames1.Length]; goto OUTPUT_THIS_LINE_BOTH; //goto FIND_AGAIN_IN_2; case -1: rowindex1++; //cols2 = new string[useful_indices_2.Count]; cols2 = new string[colnames2.Length]; goto OUTPUT_THIS_LINE_BOTH; //goto FIND_AGAIN_IN_1; default: break; } OUTPUT_THIS_LINE_BOTH: // 接下来可以比对cols1和cols2 for (int i = 0; i < useful_indices_1.Count; i++) { int index1 = Convert.ToInt32(useful_indices_1[i]); int index2 = Convert.ToInt32(useful_indices_2[i]); string col1 = string.Empty; string col2 = string.Empty; if (index1 < cols1.Length) { col1 = cols1[index1]; } if (index2 < cols2.Length) { col2 = cols2[index2]; } if (col1 != col2) { sb.Append(col1 + "|" + col2); bExportCols[i] = true; bThisLineDiff = true; } else { bool isKey = false; foreach (int ikey in keyIndices) { if (i == ikey) { isKey = true; break; } } bool isName = false; foreach (int iname in namefieldsindices) { if (i == iname) { isName = true; break; } } if (isKey || isName) { sb.Append(col1); } } sb.Append('\t'); } sb = sb.Remove(sb.Length - 1, 1); if (bThisLineDiff) { outputlines.Add(sb.ToString()); } sb = new StringBuilder(); goto FIND_AGAIN_IN_1; FINALIZE: sb = new StringBuilder(); foreach (string line in outputlines) { string[] cols = line.Split(new char[] { '\t' }); for (int i = 0; i < cols.Length; i++) { if (bExportCols[i]) { sb.Append(cols[i]); sb.Append('\t'); } } sb = sb.Remove(sb.Length - 1, 1); sb.Append("\r\n"); } // 输出到内存 //FileFolderHelper.StringToFile(sb.ToString(), output); output = sb.ToString(); output_basic = string.Empty; return(true); }
private void ReadTab(string input) { tblAll = FileToDataTable(input); string strIniFile = Application.StartupPath + "/config_public.ini"; StringBuilder sb = new StringBuilder(255); // 忽略列 string[] arrIgnoreCols = null; FileFolderHelper.ReadIniFile("IgnoreColumns", "Count", "", sb, 255, strIniFile); int IgnoreCount = Convert.ToInt32(sb.ToString()); for (int j = 1; j <= IgnoreCount; j++) { FileFolderHelper.ReadIniFile("IgnoreColumns", "Table" + j.ToString(), "", sb, 255, strIniFile); string tabName = sb.ToString(); FileFolderHelper.ReadIniFile("IgnoreColumns", "Columns" + j.ToString(), "", sb, 255, strIniFile); string columns = sb.ToString(); if (tabName.ToLower() == m_filename.ToLower()) { arrIgnoreCols = columns.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); break; } } // ui int i = 0; foreach (DataColumn col in tblAll.Columns) { string colname = col.ColumnName; // 判断是否被ignore了。 bool bColIsIgnored = false; if (arrIgnoreCols != null) { foreach (string ignoreCol in arrIgnoreCols) { if (ignoreCol.ToLower() == colname.ToLower()) { bColIsIgnored = true; break; } } } if (bColIsIgnored) { continue; } // 没有ignore,添加checkbox CheckBox checkbox = new CheckBox(); checkbox.AutoSize = true; checkbox.Location = CalculateCheckPos(i); checkbox.Name = "checkBox" + i.ToString(); checkbox.Size = new System.Drawing.Size(78, 16); checkbox.TabIndex = i; checkbox.Text = colname; checkbox.UseVisualStyleBackColor = true; checkbox.Checked = true; if (ColIsNames(colname) || ColIsKey(colname)) { checkbox.Enabled = false; } checkbox.CheckedChanged += new System.EventHandler(this.checkBox_CheckedChanged); this.splitContainer1.Panel1.Controls.Add(checkbox); checkboxes.Add(checkbox); i++; } // datagridview this.dataGridView1.DataSource = tblAll; foreach (DataGridViewColumn col in dataGridView1.Columns) { if (ColIsKey(col.Name) || ColIsNames(col.Name)) { col.Frozen = true; } } // 去掉ignore的列。 if (arrIgnoreCols != null) { foreach (string col in arrIgnoreCols) { if (tblAll.Columns.Contains(col)) { tblAll.Columns.Remove(col); } } ReCalculateTable(); } // 着色 FileFolderHelper.ReadIniFile("ColoredColumns", "Count", "", sb, 255, strIniFile); int ColoredTabCount = Convert.ToInt32(sb.ToString()); for (int j = 1; j <= ColoredTabCount; j++) { FileFolderHelper.ReadIniFile("ColoredColumns", "Table" + j.ToString(), "", sb, 255, strIniFile); string tabName = sb.ToString(); FileFolderHelper.ReadIniFile("ColoredColumns", "ColumnColors" + j.ToString(), "", sb, 255, strIniFile); string tabColColors = sb.ToString(); if (tabName.ToLower() == m_filename.ToLower()) { string[] arrColColors = tabColColors.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string colColor in arrColColors) { string[] col_and_color = colColor.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); string col = col_and_color[0]; string color = col_and_color[1]; if (dataGridView1.Columns[col] != null) { dataGridView1.Columns[col].DefaultCellStyle.BackColor = Color.FromName(color); } } break; } } }