简体   繁体   English

OpenXML MainDocumentPart.Document.Save() 似乎没有保存在.Net 5

[英]OpenXML MainDocumentPart.Document.Save() doesn't seem to save in .Net 5

I've been porting an application from .net framework to .net 5. I'm trying to generate a word document by reading a template file, using Open-XML-PowerTools to replace some text, and saving the new version.我一直在将应用程序从 .net 框架移植到 .net 5。我正在尝试通过读取模板文件、使用 Open-XML-PowerTools 替换一些文本并保存新版本来生成 Word 文档。 I can't get the "Save()" to do anything: Here's some slightly simplified code which demonstrates the problem:我不能让“Save()”做任何事情:这里有一些稍微简化的代码来演示这个问题:

                byte[] content = System.IO.File.ReadAllBytes("c:\\Temp\\myrequesttemplate.docx");
                using (MemoryStream mstr = new MemoryStream())
                {
                    mstr.Write(content, 0, content.Length);
                    using var docTest = WordprocessingDocument.Open(mstr, true);
                    {
                        docTest.ReplaceText("%NAME%", mrcrequest.RequesterName); // At this point, I can see by looking at the document.innerxml that the replacement has been successful.
                        docTest.MainDocumentPart.Document.Save();
                    }
                    using (FileStream fs = new FileStream("c:\\Temp\\TestWordDoc.docx", FileMode.CreateNew))
                    {
                        mstr.WriteTo(fs);
                    }

                }

The file created is just a copy of the old file, without the replacement.创建的文件只是旧文件的副本,没有替换。 I can see that the replacement succeeds using the debugger to look at the innerXML of the MainDocument.我可以看到使用调试器查看 MainDocument 的 innerXML 替换成功。 It just seems to not be writing back to the stream.它似乎没有写回 stream。 I've tried using ".MainDocumentPart.PutXDocument();"我试过使用“.MainDocumentPart.PutXDocument();” instead of ".Save()" - it makes no difference.而不是“.Save()”——没有区别。

I'm using DocumentFormat.OpenXML version 2.12, System.IO.Packaging 5.0.0, and Open-XML-PowerTools 4.4.0我正在使用 DocumentFormat.OpenXML 2.12 版、System.IO.Packaging 5.0.0 和 Open-XML-PowerTools 4.4.0

Any ideas anyone?有什么想法吗? It's driving me nuts.它快把我逼疯了。 Thanks.谢谢。

Finally, I found it: For anyone else facing the same issue.最后,我找到了它:对于其他面临同样问题的人。 You need to put docTest;Close().你需要把 docTest;Close(). after the;Save().在;保存()之后。 - on the old,Net Framework version it didn't need it. - 在旧的、Net Framework 版本上它不需要它。 but in,Net core.但在,网络核心。 you do.你做。

                byte[] content = System.IO.File.ReadAllBytes("c:\\Temp\\myrequesttemplate.docx");
                using (MemoryStream mstr = new MemoryStream())
                {
                    mstr.Write(content, 0, content.Length);
                    using var docTest = WordprocessingDocument.Open(mstr, true);
                    {
                        docTest.ReplaceText("%NAME%", mrcrequest.RequesterName); // At this point, I can see by looking at the document.innerxml that the replacement has been successful.
                        docTest.MainDocumentPart.Document.Save();
                        docTest.Close();
                    }
                    using (FileStream fs = new FileStream("c:\\Temp\\TestWordDoc.docx", FileMode.CreateNew))
                    {
                        mstr.WriteTo(fs);
                    }

                }

By the way, I forgot to mention that docTest.ReplaceText is just an extension method I put in to replace text whilst preserving line breaks - that's not important.顺便说一句,我忘了提到 docTest.ReplaceText 只是我用来替换文本同时保留换行符的扩展方法——这并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM