private void button9_Click(object sender, EventArgs e) { var folder =new FolderBrowserDialog(); if (folder.ShowDialog() == DialogResult.OK) { GetAllDirList(folder.SelectedPath); var SixCount = 0; var SevenCount = 0; var sixsb = new StringBuilder(); var sevensb = new StringBuilder(); for (var i = 0; i < fl.Count; i++) { var six = false; var seven = false; var fi = (FileInfo)fl[i]; var dataTable = ReadExcel(fi.FullName); for (var k = 0; k < dataTable.Rows.Count; k++) { if (dataTable.Columns.Count == 6) { var a0 = dataTable.Rows[k][0].ToString(); var a1 = dataTable.Rows[k][1].ToString(); var a2 = dataTable.Rows[k][2].ToString(); var a3 = dataTable.Rows[k][3].ToString(); var a4 = dataTable.Rows[k][4].ToString(); var a5 = dataTable.Rows[k][5].ToString(); if ((a0 == "") || (a1 == "") || (a2 == "") || (a3 == "") || (a4 == "") || (a5 == "")) { continue; } six = true; sixsb.Append(dataTable.Columns[0].ColumnName + " " + dataTable.Columns[1].ColumnName + " " + dataTable.Columns[2].ColumnName + " " + dataTable.Columns[3].ColumnName + " " + dataTable.Columns[4].ColumnName + " " + dataTable.Columns[5].ColumnName); sixsb.Append("\r\n"); sixsb.Append(a0 + " " + a1 + " " + a2 + " " + a3 + " " + a4 + " " + a5); sixsb.Append("\r\n"); } if (dataTable.Columns.Count == 7) { var a0 = dataTable.Rows[k][0].ToString(); var a1 = dataTable.Rows[k][1].ToString(); var a2 = dataTable.Rows[k][2].ToString(); var a3 = dataTable.Rows[k][3].ToString(); var a4 = dataTable.Rows[k][4].ToString(); var a5 = dataTable.Rows[k][5].ToString(); var a6 = dataTable.Rows[k][6].ToString(); if ((a0 == "") || (a1 == "") || (a2 == "") || (a3 == "") || (a4 == "") || (a5 == "") || (a6 == "")) { continue; } seven = true; sevensb.Append(dataTable.Columns[0].ColumnName + " " + dataTable.Columns[1].ColumnName + " " + dataTable.Columns[2].ColumnName + " " + dataTable.Columns[3].ColumnName + " " + dataTable.Columns[4].ColumnName + " " + dataTable.Columns[5].ColumnName + " " + dataTable.Columns[6].ColumnName); sixsb.Append("\r\n"); sevensb.Append(a0 + " " + a1 + " " + a2 + " " + a3 + " " + a4 + " " + a5 + " " + a6); sevensb.Append("\r\n"); } } if (six) SixCount++; if (seven) SevenCount++; Program.MyFrm.SetTextMessage("SixCount:" + SixCount); Program.MyFrm.SetTextMessage("SevenCount:" + SevenCount); Program.MyFrm.SetTextMessage("========以下为六个字段的信息======================================"); Program.MyFrm.SetTextMessage(sixsb.ToString()); Program.MyFrm.SetTextMessage("========以下为七个字段的信息======================================"); Program.MyFrm.SetTextMessage(sevensb.ToString()); } } } private static DataTable ReadExcel(string strFileName) { var book = new Workbook(strFileName); var sheet = book.Worksheets[0]; var cells = sheet.Cells; return cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true); } private static ArrayList fl = new ArrayList(); //我把ArrayList当成动态数组用,非常好用 private static void GetAllDirList(string strBaseDir) { fl.Clear(); var di = new DirectoryInfo(strBaseDir); var diA = di.GetDirectories(); var fiA = di.GetFiles();//获得了所有起始目录下的文件 for (var i = 0; i < fiA.Length; i++) { fl.Add(fiA[i]); } for (var i = 0; i < diA.Length; i++) { GetAllDirList(diA[i].FullName); } }
时间: 2024-10-13 06:55:32