在MVC上的Microsoft站点上使用WordprocessingDocument后收到消息“MEMORY STREAM is not expANDABLE” [英] Got a message “ MEMORY STREAM IS NOT EXPANDABLE” after using WordprocessingDocument base on Microsoft site on MVC
本文介绍了在MVC上的Microsoft站点上使用WordprocessingDocument后收到消息“MEMORY STREAM is not expANDABLE”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我基于"搜索和替换文档部分中的文本(Open XML SDK)"。在Microsoft站点上。我已经意识到文件下载到我的驱动器后代码出了问题。
Currently, I was base on "Search and replace text in a document part (Open XML SDK)" on the Microsoft site. I've realized that the code got an issue after the file has downloaded to my drive.
所以我打开了那个文件并收到了一条消息
So I opened that file and got a message
< p style ="margin-bottom:0px; padding-right:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; font-family:inherit; vertical-align:基线;明确:bo th">
内存流在sw.Write(docText)不可扩展;
MEMORY STREAM IS NOT EXPANDABLE at sw.Write(docText);
如何解决这个问题?
How to fix that?
在GenerateDocxHelper类中:
In GenerateDocxHelper class:
private readonly MemoryStream _mem;
private Dictionary<string, string> _dicData;
public GenerateDocxHelper(string path)
{
_mem = new MemoryStream(System.IO.File.ReadAllBytes(path));
_dicData = new Dictionary<string, string>();
}
public MemoryStream ReplaceTextInWord()
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(_mem, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
foreach (var data in _dicData)
{
docText = docText.Replace(data.Key, data.Value);
}
using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
}
_mem.Seek(0, SeekOrigin.Begin);
推荐答案
您应该创建MemoryStream容量值为0表示其可扩展。
You should create MemoryStream with capacity value as 0 which means its expandable.
例如
_mem = new MemoryStream(0);
这篇关于在MVC上的Microsoft站点上使用WordprocessingDocument后收到消息“MEMORY STREAM is not expANDABLE”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文