打开XML替换Word文件中的文本,并使用MVC返回内存流 [英] Open xml replace text from word file and return memory stream using MVC

查看:86
本文介绍了打开XML替换Word文件中的文本,并使用MVC返回内存流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Word文件,其中包含我指定的模式文本{pattern},我想用从数据库读取的新字符串替换这些模式.因此,我使用了从docx模板文件打开的xml读取流,替换了我的模式字符串,然后返回到流,该流支持下载文件而无需创建临时文件.但是,当我打开它时,我在docx文件上产生了错误.下面是我的示例代码

I have an word file that contain my specified pattern text {pattern} and I want to replace those pattern with new my string which was read from database. So I used open xml read stream from my docx template file the replace my pattern string then returned to stream which support to download file without create a temporary file. But when I opened it generated me error on docx file. Below is my example code

public ActionResult SearchAndReplace(string FilePath)
{
    MemoryStream mem = new MemoryStream(System.IO.File.ReadAllBytes(FilePath));
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(mem, true))
    {
        string docText = null;
        using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
        {
            docText = sr.ReadToEnd();
        }

        Regex regexText = new Regex("Hello world!");
        docText = regexText.Replace(docText, "Hi Everyone!");

//Instead using this code below to write text back the original file. I write new string back to memory stream and return to a stream download file
        //using (StreamWriter sw = new //StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
        //{
        //    sw.Write(docText);
        //}

        using (StreamWriter sw = new StreamWriter(mem))
                    {
                        sw.Write(docText);
                    }
    }
    mem.Seek(0, SeekOrigin.Begin); 

    return File(mem, "application/octet-stream","download.docx"); //Return to download file
}

请向我建议所有解决方案,而不是从Word文件中读取文本并替换那些期望的模式文本,然后将数据写回到原始文件中.是否有解决方案将文字替换为WordprocessingDocument库?如何返回具有验证docx文件格式的内存流?

Please suggest me any solutions instead read a text from a word file and replace those expected pattern text then write data back to the original file. Are there any solutions replace text with WordprocessingDocument libary? How can I return to memory stream with validation docx file format?

推荐答案

您采用的方法不正确.如果您正在搜索的模式偶然与某个Open XML标记匹配,则将损坏文档.如果您要搜索的文本被分成多个运行,则您的搜索/替换代码将找不到该文本,并且将无法正常运行.如果要搜索和替换WordprocessingML文档中的文本,可以使用一种相当简单的算法:

The approach you are taking is not correct. If, by chance, the pattern you are searching for matches some Open XML markup, you will corrupt the document. If the text you are searching for is split over multiple runs, your search/replace code will not find the text and will not operate correctly. If you want to search and replace text in a WordprocessingML document, there is a fairly easy algorithm that you can use:

  • 将所有运行中断为单个运行 特点.这包括运行 具有特殊字符,例如 换行,回车或硬 标签.
  • 然后很容易找到一个 与字符匹配的一组运行 在您的搜索字符串中.
  • 一旦您确定了一组匹配的运行, 那么您可以更换那组跑步 使用新创建的运行( 运行的运行属性 包含第一个字符 匹配搜索字符串).
  • 替换单字符运行后 使用新创建的运行,您可以 然后合并相邻的运行 相同的格式.
  • Break all runs into runs of a single character. This includes runs that have special characters such as a line break, carriage return, or hard tab.
  • It is then pretty easy to find a set of runs that match the characters in your search string.
  • Once you have identified a set of runs that match, then you can replace that set of runs with a newly created run (which has the run properties of the run containing the first character that matched the search string).
  • After replacing the single-character runs with a newly created run, you can then consolidate adjacent runs with identical formatting.

我写了一篇博客文章,并记录了演练此算法的屏幕截图.

I've written a blog post and recorded a screen-cast that walks through this algorithm.

博客文章: http://openxmldeveloper.org/archive/2011/05 /12/148357.aspx
屏幕截图: http://www.youtube.com/watch?v=w128hJUu3GM

Blog post: http://openxmldeveloper.org/archive/2011/05/12/148357.aspx
Screen cast: http://www.youtube.com/watch?v=w128hJUu3GM

-埃里克

这篇关于打开XML替换Word文件中的文本,并使用MVC返回内存流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆