private void Btn_find_Click(object sender, EventArgs e) { SetProgress(""); StartProgress(); listBoxMsg.Items.Clear(); string path = textBox_url.Text; string target = textBox_target.Text; string type = textBox_type.Text; ThreadStart childref = new ThreadStart(() => { ExcelFindData data = new ExcelFindData(); ExcelFind.Find(path, type, target, data, SetProgress); if (data.infoList.Count > 0) { foreach (var info in data.infoList) { ICell cell = info.cell; AddBoxMsg(text: $"路径: {info.path} : {cell.Sheet.SheetName}:第{cell.RowIndex}行,第{cell.ColumnIndex}列 内容为:{cell}"); } } else { AddBoxMsg($"没有找到和‘{target}’类似的内容"); } StopProgress(); }); StartThread(childref); }
public static void Find(string url, string ext, string content, ExcelFindData res, Action <string> action) { List <string> paths = FileUtil.GetAllFileName(url, ext); for (int i = 0; i < paths.Count; i++) { string path = paths[i]; ExportExcel(path, content, res); action?.Invoke($"当前进度:{path} ({i}/{paths.Count}) "); } }
private static void ExportExcel(string file, string content, ExcelFindData res) { if (!File.Exists(file)) { return; } try { var stream = new FileStream(file, FileMode.Open, FileAccess.Read); var workbook = ExcelUtil.NewWorkbook(stream); if (workbook == null) { return; } for (int i = 0; i < workbook.NumberOfSheets; i++) { ISheet sheet = workbook.GetSheetAt(i); for (int j = 0; j < sheet.LastRowNum; j++) { IRow row = sheet.GetRow(j); if (row != null) { for (int k = 0; k < row.LastCellNum; k++) { ICell cell = row.GetCell(k); string text = ""; if (cell != null) { text = cell.ToString(); if (text.Contains(content)) { ExcelFindInfo info = new ExcelFindInfo { cell = cell, path = file }; res.infoList.Add(info); } } } } } } workbook.Close(); stream.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }