使用.net核心使用现有词.doc查找并替换

时间:2018-08-03 22:17:15

标签: c# ms-word filestream

我正在尝试查找并替换我创建并添加到.net核心项目的Word文档模板中的特定单词/字符串。

我目前正在使用NPOI进行此操作,但由于我的C#知识不是很好,我似乎无法弄清楚!

从理论上讲,我想阅读现成的.docx模板,然后替换所需的内容并保存。

我已经尽力想了一切,但仍然无法正常工作。

这是我剩下的代码:

var docUrl = @"App_Data/Document_Template.docx";

using (FileStream file = new FileStream(docUrl, FileMode.Open, FileAccess.Read))
        {
            XWPFDocument doc = new XWPFDocument(file);

            // get the document template using docUrl and replace string with another string.

            doc.Write(file);

        }

任何帮助都是很棒的,因为我花了很多时间研究并尝试实现这一点,而没有运气!

1 个答案:

答案 0 :(得分:1)

要替换MS Word中的特定单词,首先应从nuget中添加以下参考:

  `
      n <- seq_len(5) #number of pages to parse

      for (p in n)
      {

       aa <- paste0("some website")
       remDr$navigate(aa)

       doc <- htmlParse(remDr$getPageSource()[[1]])
       Sys.sleep(2)

       docf <- readHTMLTable(doc)

        ab2<-docf$'NULL'

      write.table(ab2, "sheet.csv",
          na = "",
          row.names = FALSE,
          col.names = !(n[1]==1),### this is not working
          append = TRUE,
          sep = ",")
        } `

此示例代码将指定的字符串替换为另一个字符串:

Install-Package Microsoft.Office.Interop.Word

为警告起见,您应该知道此程序包正在运行,但可能与点网核心不完全兼容!

有关更多信息,请使用以下链接:Programmatically search for and replace text in documents