OpenXml:如何更改.docx文件并使用而不保存?

时间:2015-02-10 19:04:24

标签: c# openxml

我有一个.docx文件,用作导出包含一些客户端数据的文档的基础。

我需要打开文档,根据客户端更改内容,导出文件,不保存

我该怎么做?

// - Opening the document
WordprocessingDocument _document = null;
try
{
    // - Setting to false else it'll save the document as I change it
    _document = WordprocessingDocument.Open(filePath, false);
    _isOpen = true;
}
catch (Exception ex) { }

// - Doing some changes
foreach (Text element in _document.MainDocumentPart.Document.Body.Descendants<Text>())
{
    if (element.Text.Contains("#Client1#"))
    {
        element.Text = element.Text.Replace("#Client1#", "Bananas");
    }
}

using (StreamReader stream = new StreamReader(_document.MainDocumentPart.GetStream()))
{
    // - This stream is unchanged!
}

1 个答案:

答案 0 :(得分:1)

经过一些测试后,我设法使其成功:

public void Main(string[] args)
{
    // - Don't open the file directly, but from a stream and remember it.
    string fileName = "myDoc.docx";
    byte[] fileByteArray = File.ReadAllBytes(fileName);

    // - Don't forget to dispose this if you're using a manager-like class
    MemoryStream mStream = new MemoryStream(fileByteArray);

    // - Now this doesn't reflect the same document in your HD, but a copy in memory
    WordprocessingDocument document = WordprocessingDocument.Open(mStream, true);

    // - Changing the document
    ReplaceText("#Client1#", "Bananas");

    // - This won't overwrite your original file! 
    // - And this is required to "commit" the changes to the document
    document.MainDocumentPart.Document.Save();

    // - When you want to use the stream, you'll have to reset the position
    long current = mStream.Position;
    mStream.Position = 0;

    // - Use the stream (Response doesn't exist in this console program, but this is the idea)
    Response.AppendHeader("Content-Disposition", "attachment;filename=myDoc.docx");
    Response.ContentType = "application/vnd.ms-word.document";
    mStream.CopyTo(Response.OutputStream);
    Response.End();

    mStream.Position = current;

    // - If you want to overwrite the original too
    using (WordprocessingDocument document2 = WordprocessingDocument.Open(fileName, true, new OpenSettings() { AutoSave = false }))
    { 
        document2.MainDocumentPart.Document.Save(document.MainDocumentPart);            
    }
}