2015-02-10 82 views
-1

我有一个.docx文件,用作导出带有一些客户端数据的文档的基础。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! 
} 

回答

0

一些测试四周后,我设法使这项工作:

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);    
    } 
} 
相关问题