从 db 加载二进制文件并将其处理为 openxml

发布于 2024-11-09 01:15:17 字数 1229 浏览 3 评论 0原文

我有这样的方法从存储为二进制的数据库加载文档文件,然后用参数替换 customxml 部分。

不知何故,当我将字节转换为 MemoryStream 然后处理它时,它不起作用,我的自定义 xml 部分不会被替换。但如果我使用 FileStream 并从磁盘读取相同的文件,那么它就完美替换了!

MemoryStream 有什么问题?我也无法将 MemoryStream 转换为 FileStream 或创建 Stream 实例等。 有什么建议吗?

  private static Stream LoadContent(byte[] content, XmlDocument parameters)
    {
        //FileStream works perfectly
        //Stream fileStream = new FileStream(@"C:\temp\test.docx", FileMode.Open);

        Stream documentStream = new MemoryStream();
        documentStream.Write(content, 0, content.Length);

        //Processes word file, replace custom xml parts with parameters
        using (WordprocessingDocument document = WordprocessingDocument.Open(documentStream, true))
        {
            MainDocumentPart mainPart = document.MainDocumentPart;
            Stream partStream = mainPart.CustomXmlParts.First().GetStream();

            using (XmlWriter xmlWriter = XmlWriter.Create(partStream, new XmlWriterSettings { CloseOutput = false }))
            {
                parameters.WriteTo(xmlWriter);
                xmlWriter.Flush();
            }

            mainPart.Document.Save();
        }

        return documentStream;
    }

I have such method to load document file from db that is stored as binary and then replace customxml parts with parameters.

Somehow when i convert byte into MemoryStream then process it doesn't work, my custom xml parts are not replaced. But if i use FileStream and read same file from disk then it replaced perfectly!

What is wrong with MemoryStream? i can't also cast MemoryStream to FileStream or create instrance of Stream or etc..
Any suggestion?

  private static Stream LoadContent(byte[] content, XmlDocument parameters)
    {
        //FileStream works perfectly
        //Stream fileStream = new FileStream(@"C:\temp\test.docx", FileMode.Open);

        Stream documentStream = new MemoryStream();
        documentStream.Write(content, 0, content.Length);

        //Processes word file, replace custom xml parts with parameters
        using (WordprocessingDocument document = WordprocessingDocument.Open(documentStream, true))
        {
            MainDocumentPart mainPart = document.MainDocumentPart;
            Stream partStream = mainPart.CustomXmlParts.First().GetStream();

            using (XmlWriter xmlWriter = XmlWriter.Create(partStream, new XmlWriterSettings { CloseOutput = false }))
            {
                parameters.WriteTo(xmlWriter);
                xmlWriter.Flush();
            }

            mainPart.Document.Save();
        }

        return documentStream;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的快感 2024-11-16 01:15:17

在将数据写入内存流之后并再次读取之前,您可能想要尝试将内存流的“位置”属性设置为 0。

或者,您也可以将字节数组传递给内存流的构造函数,而不是调用 writer。

编辑

根据 MSDN,我看到“Doc​​ument.Save”方法将刷新流以允许正确保存。 (http://msdn.microsoft.com/en-us/library/cc840441。 aspx )。
但是,MemoryStream 不会对刷新执行任何操作( http://msdn .microsoft.com/en-us/library/system.io.memorystream.flush.aspx)。

您可以尝试创建一个新的 MemoryStream,然后将其作为参数传递给“Document.Save”方法。

You might want to try to set the 'Position' propery of the memorystream to 0 after writing data to it and before reading it again.

Alernatively you can also pass the byte array to the constructor of the memorystream instead of calling writer.

edit

I see according to MSDN that the 'Document.Save' method will flush the stream to allow propper saving. ( http://msdn.microsoft.com/en-us/library/cc840441.aspx ).
However MemoryStream wont do anything on flush ( http://msdn.microsoft.com/en-us/library/system.io.memorystream.flush.aspx ).

You could try to create a new MemoryStream and then pass that as a parameter to the 'Document.Save' method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文