Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#/openxml将word文档合并为一个文档失败--已关闭--_C#_.net_Openxml - Fatal编程技术网

c#/openxml将word文档合并为一个文档失败--已关闭--

c#/openxml将word文档合并为一个文档失败--已关闭--,c#,.net,openxml,C#,.net,Openxml,我必须将几个word文档与一个小型c#控制台应用程序合并。到现在为止,一直都还不错。这些文档是在arcplan中生成的。生成了大约30个文件,但不知何故有些文档已损坏,但仍向我显示内容。 如果我现在合并所有正确的文件,我的文档就可以了,但是如果我的文件堆中有一个损坏的文件,任何损坏的文件都会生成一个空页面。我当然调试了它,但是我没有看到任何错误,这也解释了空页的原因 论点如下: “C:\temp\Report\u C_01.docx”“C:\temp\Report\u D_01.docx”“C:

我必须将几个word文档与一个小型c#控制台应用程序合并。到现在为止,一直都还不错。这些文档是在arcplan中生成的。生成了大约30个文件,但不知何故有些文档已损坏,但仍向我显示内容。 如果我现在合并所有正确的文件,我的文档就可以了,但是如果我的文件堆中有一个损坏的文件,任何损坏的文件都会生成一个空页面。我当然调试了它,但是我没有看到任何错误,这也解释了空页的原因

论点如下: “C:\temp\Report\u C_01.docx”“C:\temp\Report\u D_01.docx”“C:\temp\Report\u E_01.docx”

这是我的代码:

public static void Merge(params String[] filepaths)
    {
        String pathName = Path.GetDirectoryName(filepaths[0]);
        subfolder = Path.Combine(pathName, "Output\\"); //Wird für den gemergten File benötigt

        if (filepaths != null && filepaths.Length > 1)
        {
            WordprocessingDocument myDoc = WordprocessingDocument.Open(@filepaths[0], true); //Wordfiles werden geöffnet
            MainDocumentPart mainPart = myDoc.MainDocumentPart;

            for (int i = 1; i < filepaths.Length; i++)
            {
                String altChunkId = "AltChunkId" + i;
                AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
                    AlternativeFormatImportPartType.WordprocessingML, altChunkId);
                FileStream fileStream = File.Open(@filepaths[i], FileMode.Open);
                chunk.FeedData(fileStream);

                DocumentFormat.OpenXml.Wordprocessing.AltChunk altChunk = new DocumentFormat.OpenXml.Wordprocessing.AltChunk();
                altChunk.Id = altChunkId;
                //new page, if you like it...
                mainPart.Document.Body.AppendChild(new Paragraph(new Run(new Break() { Type = BreakValues.Page } )));
                //next document
                mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.Elements<Paragraph>().Last());
            }
            mainPart.Document.Save();
            myDoc.Close();

            for (int i = 0; i < 1; i++)
            {
                String fileNameWE = Path.GetFileName(filepaths[i]);
                File.Copy(filepaths[i], subfolder + fileNameWE);
            }

            foreach (String fp in filepaths)
            {
                File.Delete(fp);
            }
        }
        else
        {
            Console.WriteLine("Nur 1 Argument");
        }
    }
公共静态无效合并(参数字符串[]文件路径)
{
字符串pathName=Path.GetDirectoryName(文件路径[0]);
subfolder=Path.Combine(路径名,“Output\\”;//Wird für den gemergten File benötigt
if(filepath!=null&&filepath.Length>1)
{
WordprocessingDocument myDoc=WordprocessingDocument.Open(@filepath[0],true);//Wordfiles-werden-geöffnet
MainDocumentPart mainPart=myDoc.MainDocumentPart;
for(int i=1;i
希望有人能帮助我

致意
克里斯蒂安把它修好了。word似乎无法在一个文档中合并不同的格式。因此,如果你有2个带页脚的文档,其他3个没有页脚的文档,那就行不通了。很明显,有些客户可能会遇到此类问题;至少代码是好的

当你打开一个损坏的docx时,它会给你一个错误吗?我记得还生成了一个日志文件,提供了有关问题的信息。。或者没有错误,docx看起来很奇怪?你应该尽可能指出你的文档被破坏的原因当我在word中打开它时,它给了我错误:“未知错误/word/document.xml行1”当我在生产力工具中查找它时,我看到有3个未知元素,但是,如果将它与未损坏的文件进行比较,我看不到XML树中有任何区别。打开文档后,它看起来很好。就在我将它与我的程序合并后,它进入了一个空页面。好的,我将使用软件包浏览器进行尝试,谢谢,不抱歉,它没有帮助。XML很好,所以我不知道腐败是从哪里来的。