现在,要将Word文档读入MemoryStream,在内存中处理该Word文档,并得到一致的MemoryStream,您将必须执行以下操作: // Get a MemoryStream.// In this example, the MemoryStream is created by reading a file stored// in the file system. Depending on the Stream you "receive", it makes// sense to copy the Stream to a MemoryStream before processing.MemoryStream stream = ReadAllBytesToMemoryStream(@"C:\Original.docx");// Open the Word document on the MemoryStream.using (WordprocessingDocument wpd = WordprocessingDocument.Open(stream, true){ MainDocumentPart mdp = wpd.MainDocumentPart; Document document = mdp.Document; // Manipulate document ...}// After having closed the WordprocessingDocument (by leaving the using statement),// you can use the MemoryStream for whatever comes next, e.g., to write it to a// file stored in the file system.File.WriteAllBytes(@"C:\New.docx", stream.GetBuffer()); 请注意,只要您的下一个操作依赖于MemoryStream.Position属性(例如CopyTo,CopyToAsync),就必须通过调用stream.Seek(0, SeekOrigin.Begin)来重置stream.Position属性.离开using语句后,流的位置将等于其长度.We are trying to manipulate a word document to remove a paragraph based on certain conditions. But the word file produced always ends up being corrupted when we try to open it with the error:The below code corrupts the file but if we remove the line:Document document = mdp.Document;The the file is saved and opens without issue. Is there an obvious issue that I am missing? var readAllBytes = File.ReadAllBytes(@"C:\Original.docx"); using (var stream = new MemoryStream(readAllBytes)) { using (WordprocessingDocument wpd = WordprocessingDocument.Open(stream, true)) { MainDocumentPart mdp = wpd.MainDocumentPart; Document document = mdp.Document; }}File.WriteAllBytes(@"C:\New.docx", readAllBytes);UPDATE:using (WordprocessingDocument wpd = WordprocessingDocument.Open(@"C:\Original.docx", true)) { MainDocumentPart mdp = wpd.MainDocumentPart; Document document = mdp.Document; document.Save(); }Running the code above on a physical file we can still open Original.docx without the error so it seems limited to modifying a stream. 解决方案 Here's a method that reads a document into a MemoryStream:public static MemoryStream ReadAllBytesToMemoryStream(string path){ byte[] buffer = File.ReadAllBytes(path); var destStream = new MemoryStream(buffer.Length); destStream.Write(buffer, 0, buffer.Length); destStream.Seek(0, SeekOrigin.Begin); return destStream;}Note how the MemoryStream is instantiated. I am passing the capacity rather than the buffer (as in your own code). Why is that?When using MemoryStream() or MemoryStream(int), you are creating a resizable MemoryStream instance, which you will want in case you make changes to your document. When using MemoryStream(byte[]) (as in your code), the MemoryStream instance is not resizable, which will be problematic unless you don't make any changes to your document or your changes will only ever make it shrink in size.Now, to read a Word document into a MemoryStream, manipulate that Word document in memory, and end up with a consistent MemoryStream, you will have to do the following:// Get a MemoryStream.// In this example, the MemoryStream is created by reading a file stored// in the file system. Depending on the Stream you "receive", it makes// sense to copy the Stream to a MemoryStream before processing.MemoryStream stream = ReadAllBytesToMemoryStream(@"C:\Original.docx");// Open the Word document on the MemoryStream.using (WordprocessingDocument wpd = WordprocessingDocument.Open(stream, true){ MainDocumentPart mdp = wpd.MainDocumentPart; Document document = mdp.Document; // Manipulate document ...}// After having closed the WordprocessingDocument (by leaving the using statement),// you can use the MemoryStream for whatever comes next, e.g., to write it to a// file stored in the file system.File.WriteAllBytes(@"C:\New.docx", stream.GetBuffer());Note that you will have to reset the stream.Position property by calling stream.Seek(0, SeekOrigin.Begin) whenever your next action depends on that MemoryStream.Position property (e.g., CopyTo, CopyToAsync). Right after having left the using statement, the stream's position will be equal to its length. 这篇关于Word OpenXml Word发现不可读的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-18 11:38