private void Button_Click(object sender, RoutedEventArgs e) { string fileName = @"C:UsersAdminDesktop322.docx"; XWPFDocument document = null; try { using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { document = new XWPFDocument(file); } using (FileStream stream = File.OpenRead(fileName)) { XWPFDocument doc = new XWPFDocument(stream); //遍历段落 foreach (var para in doc.Paragraphs) { ReplaceKey(para); } //遍历表格 var tables = doc.Tables; foreach (var table in tables) { foreach (var row in table.Rows) { foreach (var cell in row.GetTableCells()) { foreach (var para in cell.Paragraphs) { ReplaceKey(para); } } } } using (MemoryStream ms = new MemoryStream()) { doc.Write(ms); using (FileStream fsWrite = new FileStream(@"D:1.txt", FileMode.Append)) { fsWrite.Write(ms.ToArray(), 0, ms.ToArray().Length); }; } } } catch (Exception ex) { Console.WriteLine(string.Format("文件{0}打开失败,错误:{1}", new string[] { fileName, ex.ToString() })); } } private void ReplaceKey(XWPFParagraph para) { string text = para.ParagraphText; var runs = para.Runs; string styleid = para.Style; for (int i = 0; i < runs.Count; i++) { var run = runs[i]; text = run.ToString(); runs[i].SetText(text+2, 0); } }