0

我是新手,有空私聊我。很抱歉这么含糊;代码执行并写入损坏的文件。我无法进入损坏的文件来观察任何错误/异常。大小确实看起来像是要合并的文件的总和。

我已经用谷歌搜索了鼻涕,找不到任何我能理解如何实施的东西。

打开的Word错误是:文件无法打开,因为内容有问题

Word 发现无法阅读的内容,您要继续吗?

当我单击“是”时,我再次收到第一个错误,然后我就退出了。

这些文档具有同名的内容控件,但我已将其修改为仅添加 1 个文档,以查看是否存在重复内容类型的问题;相同的损坏文件结果。

顺便说一句....我的最终意图是覆盖“模板”(...Aggregate Report.dotx)。但我无法在任何地方保存有效文件,所以...... :-/

using System;   
using System.IO;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Word = DocumentFormat.OpenXml.Wordprocessing;


namespace BobsDocMerger.VisualWebPart1
{
[ToolboxItemAttribute(false)]
public class VisualWebPart1 : WebPart
{
    // Visual Studio might automatically update this path when you change the Visual Web Part project item.
    private const string _ascxPath = @"~/_CONTROLTEMPLATES/BobsDocMerger/VisualWebPart1/VisualWebPart1UserControl.ascx";

    protected override void CreateChildControls()
    {
        System.Web.UI.Control control = this.Page.LoadControl(_ascxPath);
        Controls.Add(control);
        base.CreateChildControls();
        Button btnSubmit = new Button();
        btnSubmit.Text = "Assemble Documents";
        btnSubmit.Click += new EventHandler(btnSubmit_Click);
        Controls.Add(btnSubmit);
    }

    void btnSubmit_Click(object sender, EventArgs e)
    {
        SPFolder folder = SPContext.Current.ListItem.Folder;
        char[] splitter = { '/' };
        string[] folderName = folder.Name.Split(splitter);
        string filePrefix = @"Weekly/" + folderName[0] + "/" + folderName[0];

        SPFile template = folder.Files[filePrefix + " - Aggregate Report.dotx"];
        SPFile file;
        byte[] byteArray = template.OpenBinary();

        using (MemoryStream mem = new MemoryStream())
        {
            mem.Write(byteArray, 0, (int)byteArray.Length);

            using (WordprocessingDocument myDoc = WordprocessingDocument.Open(mem, true))
            {
                MainDocumentPart mainPart = myDoc.MainDocumentPart;
                //Loop thru content controls
                foreach (Word.SdtElement sdt in mainPart.Document.Descendants<Word.SdtElement>().ToList())
                {
                    Word.SdtAlias alias = sdt.Descendants<Word.SdtAlias>().FirstOrDefault();
                    if (alias != null)
                    {
                        //The 2 tags in the Report are AggregateHeader and AggregateBody
                        string sdtTitle = alias.Val.Value;
                        string sdtTag = sdt.GetFirstChild<SdtProperties>().GetFirstChild<Tag>().Val;
                        if (sdtTitle == "Merge")
                        {
                            for (int i = 0; i < folder.Files.Count; i++)
                            {
                                file = folder.Files[i];
                                //Do all files that are NOT the Aggregate Report
                                if (file.Name.IndexOf("Aggregate Report") == -1)
                                {
                                    if (i == folder.Files.Count-1)
                                    {
                                        AddAltChunk(mainPart, sdt, file, true);
                                    }
                                    else
                                    {
                                        AddAltChunk(mainPart, sdt, file, false);
                                    }
                                }
                            }
                        }
                    }
                }

                HttpResponse resp = HttpContext.Current.Response;
                resp.ClearContent();
                resp.ClearHeaders();
                resp.AddHeader("Content-Disposition", "attachment; filename=Assembled Document.docx");
                //resp.ContentEncoding = System.Text.Encoding.UTF8;
                resp.ContentType = "application/msword";
                resp.OutputStream.Write(mem.ToArray(), 0, (int)mem.Length);
                resp.Flush();
                resp.Close();
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
        }
    }

    protected int id = 1;

    void AddAltChunk(MainDocumentPart mainPart, Word.SdtElement sdt, SPFile filename,bool LastPass)
    {
        string altChunkId = "AltChunkId" + id;
        id++;
        byte[] byteArray = filename.OpenBinary();

        AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
        AlternativeFormatImportPartType.WordprocessingML, altChunkId);

        using (MemoryStream mem = new MemoryStream())
        {
            mem.Write(byteArray, 0, (int)byteArray.Length);
            mem.Seek(0, SeekOrigin.Begin);
            chunk.FeedData(mem);
        }

        Word.AltChunk altChunk = new Word.AltChunk();
        altChunk.Id = altChunkId;
        //Replace content control with altChunk information 

        DocumentFormat.OpenXml.OpenXmlElement parent = sdt.Parent;
        parent.InsertBefore(altChunk, sdt);
        if (LastPass) { sdt.Remove(); }
    }
}
}
4

1 回答 1

1

看起来您没有.Seek()在主内存流上正确调用,而且您似乎可能同时将单个内存流用于输入和输出。(也许它是正确的,但是当它是混合模式时让我很困惑)

我假设您无法访问原始文件名和文件系统:

using(Stream t = template.OpenBinaryStream())
{
    using (WordprocessingDocument myDoc = WordprocessingDocument.Open(t, true))
    {
        using (XmlWriter writer = XmlWriter.Create(resp.OutputStream))
        {
            // TODO re-add merge logic here once it works

            HttpResponse resp = HttpContext.Current.Response;
            resp.ClearContent();
            resp.ClearHeaders();
            resp.AddHeader("Content-Disposition", 
                "attachment; filename=Assembled Document.docx");
            //resp.ContentEncoding = System.Text.Encoding.UTF8;
            resp.ContentType = "application/msword";
            // resp.OutputStream.Write(mem.ToArray(), 0, (int)mem.Length);
/* new */   myDoc.MainDocumentPart.Document.WriteTo(writer);
            resp.Flush();
            resp.Close();
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
    }
}

PS - 我建议先获取一个原始模板以正确输出。然后在重新添加合并逻辑之前进行一个微小的更改并查看该输出。

于 2013-08-01T20:23:50.900 回答