简体   繁体   English

Open-XML 保存 word 文档产生损坏的文件

[英]Open-XML saving word document produces corrupted file

Although I'm new to the Open-XML world, I've already encountered some troubles/problems using it.虽然我是 Open-XML 世界的新手,但我在使用它时已经遇到了一些麻烦/问题。 The most of them were easily solved but I can't get around this one:其中大部分很容易解决,但我无法解决这个问题:

public class ReportDocument : IDisposable
{
    private MemoryStream stream;
    private WordprocessingDocument document;
    private MainDocumentPart mainPart;

    public byte[] DocumentData
    {
        get 
        {
            this.document.ChangeDocumentType(WordprocessingDocumentType.MacroEnabledDocument);
            byte[] documentData = this.stream.ToArray();
            return documentData;
        }
    }

    public ReportDocument()
    {
        byte[] template = DocumentTemplates.SingleReportTemplate;
        this.stream = new MemoryStream();
        stream.Write(template, 0, template.Length);
        this.document = WordprocessingDocument.Open(stream, true);
        this.mainPart = document.MainDocumentPart;
    }

    public void SetReport(Report report)
    {
        Body body = mainPart.Document.Body;
        var placeholder = body.Descendants<SdtBlock>();
        this.SetPlaceholderTextValue(placeholder, "Company", WebApplication.Service.Properties.Settings.Default.CompanyName);
        this.SetPlaceholderTextValue(placeholder, "Title", String.Format("Status Report for {0} to {1}", report.StartDate.ToShortDateString(),
            report.ReportingInterval.EndDate.ToShortDateString()));
        //this.SetPlaceholderTextValue(placeholder, "Subtitle", String.Format("for {0}", report.ReportingInterval.Project.Name));
        this.SetPlaceholderTextValue(placeholder, "Author", report.TeamMember.User.Username);
        this.SetPlaceholderTextValue(placeholder, "Date", String.Format("for {0}", DateTime.Today.ToShortDateString()));
    }

    private void SetPlaceholderTextValue(IEnumerable<SdtBlock> sdts, string alias, string value)
    {
        SdtContentBlock contentBlock = this.GetContentBlock(sdts, alias);
        Text text = contentBlock.Descendants<Text>().First();
        text.Text = value;
    }

    private SdtContentBlock GetContentBlock(IEnumerable<SdtBlock> sdts, string alias)
    {
        return sdts.First(sdt => sdt.Descendants<SdtAlias>().First().Val.Value == alias).SdtContentBlock;
    }

    public void Dispose()
    {
        this.document.Close();
    }
}

So i create a new document, based on a template it gains through the memory stream and want to write it back to a memory stream when the changes are made.所以我创建了一个新文档,基于它通过 memory stream 获得的模板,并希望在进行更改时将其写回 memory stream。

The big problem is, when i save the resulting byte array the data docx file is corrupted:最大的问题是,当我保存生成的字节数组时,数据 docx 文件已损坏:

The document.xml in.\word is called document2.xml The document.xml.rels in.\word_rels is called document2.xml.rels and it contains I hope some of you can provide good solutions for it. .\word中的document.xml叫做document2.xml .\word_rels中的document.xml.rels叫做document2.xml.rels里面有希望大家提供好的解决方案。

MFG SakeSushiBig MFG清酒寿司大

Change your DocumentData property to this and I think it should work.将您的 DocumentData 属性更改为此,我认为它应该可以工作。 The important thing is to close the document before you read the memorystream.重要的是在读取内存流之前关闭文档。

public byte[] DocumentData
    {
        get 
        {
            this.document.ChangeDocumentType(WordprocessingDocumentType.MacroEnabledDocument);
            this.document.MainDocumentPart.Document.Save();
            this.document.Close();            
            byte[] documentData = this.stream.ToArray();
            return documentData;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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