【问题标题】:The specified package is invalid. The main part is missing指定的包无效。主要部分不见了
【发布时间】:2017-05-31 01:56:20
【问题描述】:

下面给出的是一个合并函数,旨在合并文件夹中的所有 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))

引发错误指定的包无效。缺少主要部分。

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

【问题讨论】:

    标签: c# asp.net openxml


    【解决方案1】:

    您可能会执行两次 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++;
    }
    

    【讨论】:

    • 如果我们从该位置删除 server.mappath ,则会引发异常 - 访问路径 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\11.0\IEP4TEMP- 1310-2.docx' 被拒绝。
    • 尝试将 HttpContext.Current.Server.MapPath(("~\\StudentBinder") + "\\Temp4\\" 更改为 Server.MapPath("~\\StudentBinder\\Temp4\\" ) 也许?
    • 但是为了早点解决这个问题,我遵循了stackoverflow.com/questions/9108399/…中所说的内容
    • 那么,如果你的文件 C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\11.0\IEP4TEMP-1310-2.docx 存在,那只是权限问题
    • yeah.tried server.mappath 但结果相同。 @Heorhiy Pavlovych
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 2014-10-25
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    相关资源
    最近更新 更多