为什么这段代码会写一个Word无法打开的“损坏文件”(甚至不是“打开并修复”)?

时间:2013-08-01 19:01:17

标签: c# sharepoint-2010

我是新手,请随意跟我说话。 抱歉这么模糊;代码执行并写入损坏的文件。我无法进入损坏的文件来观察任何错误/异常。大小看起来像是要合并的文件的总和。

我用谷歌搜索了这个,并找不到任何我能理解如何实现的内容。

打开时的Word错误是: 由于内容存在问题

,因此无法打开该文件

Word找到了不可读的内容,你想继续吗?

当我点击是时,我再次收到第一个错误&然后我出去了。

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

btw ....我的最终目的是覆盖“模板”(... 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(); }
    }
}
}

1 个答案:

答案 0 :(得分: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 - 我建议首先正确输出原始模板。然后进行一个微小的更改,并在重新添加合并逻辑之前查看输出。