指定的包无效。主要部分不见了

The specified package is invalid. The main part is missing

下面给出了一个合并函数,它旨在合并一个文件夹中的所有 docx 文件并生成合并后的文件。

public  void Merge()
{  
    try
    {

        string sid = Request.QueryString["studid"];
        string stud = sid.ToString();

        string ds = HttpContext.Current.Server.MapPath(("~\StudentBinder") + "\Temp4\");
        if (Directory.Exists(ds))
        {
            DirectoryInfo d = new DirectoryInfo(ds);
            FileInfo[] Files = d.GetFiles("*" + stud + "*.docx");

              // stud added for differentiating b/w users

            string[] filepaths = new string[Files.Length];
            int index = 0;

            foreach (FileInfo file in Files)
            {
                filepaths[index] = file.Name;
                index++;
            }


                using (WordprocessingDocument myDoc = WordprocessingDocument.Open(Server.MapPath(filepaths[0]), true))

                {
                    for (int i = 1; i < filepaths.Length; i++)
                    {
                        MainDocumentPart mainPart = myDoc.MainDocumentPart;
                        string altChunkId = "AltChunkId" + i.ToString();
                        AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);
                        using (FileStream fileStream = File.Open(@filepaths[i], FileMode.Open))
                        {
                            chunk.FeedData(fileStream);
                        }
                        AltChunk altChunk = new AltChunk();
                        altChunk.Id = altChunkId;
                        mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.Elements<DocumentFormat.OpenXml.Wordprocessing.Paragraph>().Last());
                        mainPart.Document.Save();
                        myDoc.Close();
                    }
                }
        }
    }
    catch (Exception ex)
    {
    }
}

但是行

using (WordprocessingDocument myDoc = WordprocessingDocument.Open(Server.MapPath(filepaths[0]), true))

引发错误指定的包无效。主要部分不见了。

我不知道那句话出了什么问题。欢迎任何建议。提前致谢。

你可能 Server.MapPath 两次:在开始时

string ds = HttpContext.Current.Server.MapPath(("~\StudentBinder")+"\Temp4\")

并在

行中
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(Server.MapPath(filepaths[0]), true))

您可以将此行重写为

using (WordprocessingDocument myDoc = WordprocessingDocument.Open(filepaths[0], true))

并且您还需要从 file.FullName 填充文件路径数组,而不是 file.Name:

foreach (FileInfo file in Files)
{
    filepaths[index] = file.FullName;
    index++;
}