private void btnReplace_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(File_Replace)) { MessageBox.Show("请选择文件!"); return; } if (MessageBox.Show("确定替换" + File_Replace + "?", "确定", MessageBoxButton.OKCancel) != MessageBoxResult.OK) { return; } busyCtl.IsBusy = true; Dictionary <string, string> replaceDic = new Dictionary <string, string>(); for (int i = 0; i < ReplaceDatas.Count; i++) { var data = ReplaceDatas[i]; if (string.IsNullOrEmpty(data.From)) { continue; } if (replaceDic.Keys.Contains(data.From)) { MessageBox.Show("第" + (i + 1) + "行查找的内容与之前的行重复。"); continue; } replaceDic.Add(data.From.Replace(Environment.NewLine, "^p"), data.To.Replace(Environment.NewLine, "^p")); } //if (replaceDic.Count == 0) //{ // MessageBox.Show("没有有效的查找替换内容!"); // return; //} BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += Worker_DoWork; worker.ProgressChanged += Worker_ProgressChanged; worker.RunWorkerCompleted += Worker_RunWorkerCompleted; worker.WorkerReportsProgress = true; ReplacePara para = new ReplacePara() { ReplaceDatas = replaceDic, FilePath = File_Replace, All = chkSelectDir.IsChecked.GetValueOrDefault(), FileNameFrom = txtFileFrom.Text.Trim(), FileNameTo = txtFileTo.Text.Trim() }; worker.RunWorkerAsync(para); }
private void Worker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; Dictionary <string, string> replaceDatas; //被替换、替换成的文本 bool all; //是否同目录全部替换 string fileFrom, fileTo; ReplacePara para = e.Argument as ReplacePara; replaceDatas = para.ReplaceDatas; all = para.All; fileFrom = para.FileNameFrom; fileTo = para.FileNameTo; lstFileInfo = new List <FileInfo>(); idx = 0; if (all) { FileInfo file = new FileInfo(para.FilePath); DirectoryInfo dir = file.Directory; FileInfo[] files = dir.GetFiles("*.*", SearchOption.AllDirectories); if (files != null && files.Length > 0) { foreach (var f in files) { if (f.IsReadOnly || (f.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { continue; } if (f.Name.EndsWith(".doc") || f.Name.EndsWith(".docx") || f.Name.EndsWith(".xls") || f.Name.EndsWith(".xlsx")) { lstFileInfo.Add(f); } } } } else { lstFileInfo.Add(new FileInfo(para.FilePath)); } Word.Application wordApp = new Word.ApplicationClass(); Excel.Application excelApp = new Excel.ApplicationClass(); //设置为不可见 wordApp.Visible = false; excelApp.Visible = false; foreach (FileInfo file in lstFileInfo) { worker.ReportProgress(idx++); string newPath = null; if (!string.IsNullOrEmpty(fileFrom)) { newPath = System.IO.Path.Combine(file.DirectoryName, file.Name.Replace(fileFrom, fileTo)); } try { if (file.Name.EndsWith(".doc") || file.Name.EndsWith(".docx")) { WordReplace(replaceDatas, file.FullName, newPath, wordApp); } if (file.Name.EndsWith(".xls") || file.Name.EndsWith(".xlsx")) { ExcelReplace(replaceDatas, file.FullName, newPath, excelApp); } } catch (Exception ex) { Log.Error("替换失败!", ex); } } //Close App Component try { wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); } catch { } try { excelApp.Quit(); } catch { } e.Result = true; }