保存word文档的例外情况

0 .net c# openxml

当我执行以下代码以保存word文件后发生异常.请帮助我纠正问题.谢谢.

当访问是FileAccess.Read时,无法使用FileMode.Create,FileMode.CreateNew,FileMode.Truncate,FileMode.Append获取流.

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(path, false))
{
    string docText = null;
    using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
    {
        docText = sr.ReadToEnd();
    }

    //Regex regexText = new Regex("<var_Date>");
    docText = docText.Replace("<var_Date>", DateTime.Now.ToString("MMM dd,yyyy"));

    using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
        sw.Write(docText);
} 
Run Code Online (Sandbox Code Playgroud)

Mat*_*and 5

就在这儿:

WordprocessingDocument.Open(path, false))
Run Code Online (Sandbox Code Playgroud)

第二个参数是isEditable,你传递了假.所以你以只读方式打开它.

参考:https://msdn.microsoft.com/en-us/library/cc562234.aspx