我想在不打開它的情況下閱讀單詞文檔。然後替換它中的一些文本,然後使用C#將其另存爲另一個名稱。但是文檔不會與更改一起保存,而是以與原始文檔相同的方式保存。任何幫助將不勝感激。提前致謝。C#單詞文檔
string path = Server.MapPath("~/CustomerDocument/SampleNDA5.docx");
WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(path, false);
{
var body = wordprocessingDocument.MainDocumentPart.Document.Body;
foreach (var text in body.Descendants<Text>())
{
if (text.Text.Contains("<var_Date>"))
{
text.Text = text.Text.Replace("<var_Date>", DateTime.Now.ToString("MMM dd,yyyy"));
}
}
wordprocessingDocument.SaveAs(Server.MapPath("~/CustomerDocument/SampleNDA10.docx"));
wordprocessingDocument.Close();
}
雖然答案中的鏈接很好,但您應該解釋上下文並引用適用於問題的鏈接中的相關部分(鏈接可能過時或在以後死亡,因此答案的主要內容應該是這裏)。 – crashmstr