NPOI.OpenXml4Net.Exceptions.OpenXML4NetRuntimeException:“Fail to save: an error occurs while saving the package : Must support writing
源代码
string fileContent = File.ReadAllText(filePath, Encoding.Default);
Dictionary<string, string> dic = ConvertToKeyValuePairs(fileContent);
File.Copy(srcPath, dstPath, true);
using (FileStream fileStream = new FileStream(dstPath, FileMode.Open, FileAccess.ReadWrite))
{
doc = new XWPFDocument(fileStream);
foreach (var item in dic)
{
if (String.IsNullOrEmpty(item.Value))
{
continue;
}
TextReplace($"{{{item.Key}}}", item.Value);
}
doc.Write(fileStream);
}
不值得为啥不支持写
NPOI.OpenXml4Net.Exceptions.OpenXML4NetRuntimeException:“Fail to save: an error occurs while saving the package : Must support writing
参数名: baseOutputStream”
修改2
Dictionary<string, string> dic = ConvertToKeyValuePairs(fileContent);
File.Copy(srcPath, dstPath, true);
using (FileStream fileStream = new FileStream(srcPath, FileMode.Open, FileAccess.ReadWrite))
{
doc = new XWPFDocument(fileStream);
foreach (var item in dic)
{
if (String.IsNullOrEmpty(item.Value))
{
continue;
}
TextReplace($"{{{item.Key}}}", item.Value);
}
}
using (FileStream fileStreamWrite = new FileStream(dstPath, FileMode.Open, FileAccess.Write))
{
doc.Write(fileStreamWrite);
}
还是不行,输出乱码,感觉是FileMode.Open问题。
终版正常运行,直接创建了一个新的流。
string fileContent = File.ReadAllText(filePath, Encoding.Default);
Dictionary<string, string> dic = ConvertToKeyValuePairs(fileContent);
using (FileStream fileStream = new FileStream(srcPath, FileMode.Open, FileAccess.ReadWrite))
{
doc = new XWPFDocument(fileStream);
foreach (var item in dic)
{
if (String.IsNullOrEmpty(item.Value))
{
continue;
}
TextReplace($"{{{item.Key}}}", item.Value);
}
}
using (FileStream fileStreamWrite = new FileStream(dstPath, FileMode.Create, FileAccess.Write))
{
doc.Write(fileStreamWrite);
}