Для поиска и замены я вот так делал. Метод написан для asp core. Он заменяет данные в тексте и выдает его в ответе сервера.
public IActionResult Index()
{
if (System.IO.File.Exists(@"d:\1_.docx"))
System.IO.File.Delete(@"d:\1_.docx");
System.IO.File.Copy(@"d:\1.docx", @"d:\1_.docx");
using (var doc = WordprocessingDocument.Open(@"d:\1_.docx", true))
{
var body = doc.MainDocumentPart.Document.Body;
var paras = body.Elements<Paragraph>();
foreach (var para in paras)
{
foreach (var run in para.Elements<Run>())
{
foreach (var text in run.Elements<Text>())
{
if (text.Text.Contains("replaced-text"))
{
text.Text = text.Text.Replace("replaced-text", DateTime.Now.ToString("dd.MM.yy"));
}
}
}
}
doc.Save();
doc.Close();
}
string path = @"d:\1_.docx";
FileStream fs = new FileStream(path, FileMode.Open);
string file_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
string file_name = "book3.docx";
return File(fs, file_type, file_name);
/*
using (MemoryStream mem = new MemoryStream())
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(mem, DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true))
{
wordDoc.AddMainDocumentPart();
// siga a ordem
Document doc = new Document();
Body body = new Body();
// 1 paragrafo
Paragraph para = new Paragraph();
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Normal" };
Justification justification1 = new Justification() { Val = JustificationValues.Center };
ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
paragraphProperties1.Append(paragraphStyleId1);
paragraphProperties1.Append(justification1);
paragraphProperties1.Append(paragraphMarkRunProperties1);
Run run = new Run();
RunProperties runProperties1 = new RunProperties();
Text text = new Text() { Text = "The OpenXML SDK rocks!" };
// siga a ordem
run.Append(runProperties1);
run.Append(text);
para.Append(paragraphProperties1);
para.Append(run);
// 2 paragrafo
Paragraph para2 = new Paragraph();
ParagraphProperties paragraphProperties2 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId2 = new ParagraphStyleId() { Val = "Normal" };
Justification justification2 = new Justification() { Val = JustificationValues.Start };
ParagraphMarkRunProperties paragraphMarkRunProperties2 = new ParagraphMarkRunProperties();
paragraphProperties2.Append(paragraphStyleId2);
paragraphProperties2.Append(justification2);
paragraphProperties2.Append(paragraphMarkRunProperties2);
Run run2 = new Run();
RunProperties runProperties3 = new RunProperties();
Text text2 = new Text();
text2.Text = "Teste aqui";
run2.AppendChild(new Break());
run2.AppendChild(new Text("Hello"));
run2.AppendChild(new Break());
run2.AppendChild(new Text("world"));
para2.Append(paragraphProperties2);
para2.Append(run2);
// todos os 2 paragrafos no main body
body.Append(para);
body.Append(para2);
doc.Append(body);
wordDoc.MainDocumentPart.Document = doc;
wordDoc.Close();
}
return File(mem.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "ABC.docx");
}
*/
}