简体   繁体   中英

Exception in saving word document

When I execute the below code to save the word file following exception occurs.please help me to correct the issue.thanks.

Cannot get stream with FileMode.Create, FileMode.CreateNew, FileMode.Truncate, FileMode.Append when access is FileAccess.Read.

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);
} 

Right here:

WordprocessingDocument.Open(path, false))

The second argument is isEditable , you passed false. So you've opened it as read-only.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM