任务是更新存储在 SharePoint 上的
.docx
文件。因此,我使用 https://www.nuget.org/packages/DocumentFormat.OpenXml/ packege。
任何数据都应该存储在本地驱动器上,因此尝试使用
Stream
。
我有一个代码:
Stream docStream = await _httpClient!.GetStreamAsync(uri);
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(docStream, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
Regex regexText = new Regex("OldText");
docText = regexText.Replace(docText, "NewText");
Stream outputDocStream = wordDoc.MainDocumentPart.GetStream(FileMode.Create);
return outputDocStream;
}
代码第 2 行发生异常
DocumentFormat.OpenXml.Packaging.OpenXmlPackageException: 'The stream was not opened for writing.'
有人可以建议如何解决这个问题吗?
HttpClient
返回只读流,因此要对其中的数据进行任何更改,最简单的解决方案是将该流复制到MemoryStream