1

次のコードを試しています。fileName (多くのセクションを含む docx ファイル) を受け取り、各セクションを反復してセクション名を取得しようとします。問題は、読めない docx ファイルになってしまうことです。エラーにはなりませんが、セクション内の要素の取得に何か問題があると思います。

public void Split(string fileName) {
            using (WordprocessingDocument myDoc =
                WordprocessingDocument.Open(fileName, true)) {
                string curCliCode = "";
                MainDocumentPart mdp = myDoc.MainDocumentPart;

                foreach (var element in mdp.Document.Body.ChildElements) {
                    if (element.Descendants().OfType<SectionProperties>().Count() == 1) {
                        //get the name of the section from the footer
                        var footer = (FooterPart) mdp.GetPartById(
                                                      element.Descendants().OfType<SectionProperties>().First().OfType
                                                          <FooterReference>().First().
                                                          Id.Value);
                        foreach (Paragraph p in footer.Footer.ChildElements.OfType<Paragraph>()) {
                            if (p.InnerText != "") {
                                curCliCode = p.InnerText;
                            }
                        }
                        if (curCliCode != "") {
                            var forFile = new List<OpenXmlElement>();
                            var els = element.ElementsBefore();
                            if (els != null) {
                                foreach (var e in els) {
                                    if (e != null) {
                                        forFile.Add(e);
                                    }
                                }
                                for (int i = 0; i < els.Count(); i++) {
                                    els.ElementAt(i).Remove();
                                }
                            }
                            Create(curCliCode, forFile);
                        }
                    }
                }

            }
        }
        private void Create(string cliCode,IEnumerable<OpenXmlElement> docParts) {
            var parts = from e in docParts select e.Clone();
            const string template = @"\Test\toSplit\blank.docx";
            string destination = string.Format(@"\Test\{0}.docx", cliCode);
            File.Copy(template, destination,true);
            /* Create the package and main document part */
            using (WordprocessingDocument myDoc =
                WordprocessingDocument.Open(destination, true)) {
                MainDocumentPart mainPart = myDoc.MainDocumentPart;
                /* Create the contents */
                foreach(var part in parts) {
                    mainPart.Document.Body.Append((OpenXmlElement)part);
                }

                /* Save the results and close */
                mainPart.Document.Save();
                myDoc.Close();
            }
        }

問題が何であるか (または、あるドキュメントから別のドキュメントにセクションを適切にコピーする方法) を知っている人はいますか?

4

2 に答える 2

1

私はこの分野でいくつかの作業を行いましたが、非常に貴重であるとわかったのは、既知の適切なファイルと将来のファイルとの比較です。エラーは通常かなり明白です。

私がすることは、動作することがわかっているファイルを取得し、すべてのセクションをテンプレートにコピーすることです。理論的には、2 つのファイルは同一である必要があります。docx ファイル内の document.xml との差分を実行すると、違いがわかります。

ところで、docx が実際には zip であることをご存知だと思います。拡張子を「zip」に変更すると、フォーマットを構成する実際の xml ファイルを取得できます。

diff ツールに関しては、Scooter Software の Beyond Compare を使用しています。

于 2011-01-11T17:36:50.757 に答える
1

あなたが行っていることに沿ったアプローチは、単純なドキュメント (つまり、画像、ハイパーリンク、コメントなどを含まないドキュメント) に対してのみ機能します。これらのより複雑なドキュメントを処理するには、http://blogs.msdn.com/b/ericwhite/archive/2009/02/05/move-insert-delete-paragraphs-in-word-processing-documents-usingをご覧ください。 -the-open-xml-sdk.aspxおよび結果の DocumentBuilder API (CodePlex の PowerTools for Open XML プロジェクトの一部)。

DocumentBuilder を使用して docx をセクションに分割するには、最初に、sectPr 要素を含む段落のインデックスを見つける必要があります。

于 2011-01-11T22:45:18.363 に答える