1

c# と openxml を使用して、いくつかの単語ファイルを編集しようとしています。数字を特定のフレーズに制御して置き換える必要があります。各単語ファイルには、さまざまな量の情報が含まれています。この目的のために OPENXML パワーツールを使用したいと考えています。

通常のopenxmlメソッドを使用して置き換えましたが、非常に信頼性が低く、ゼロ長エラーなどのランダムエラーが発生しました。

コードの一部を次に示します。

private void redact_Replaceall(string wfile)
        {
            try
            {
                using (WordprocessingDocument doc = WordprocessingDocument.Open(wfile, true))
                {
                    var ydoc = doc.MainDocumentPart.GetXDocument();
                    IEnumerable<XElement> content = ydoc.Descendants(W.body);



                    Regex regex = new Regex(@"\d+\.\d{2,3}");
                    int count1 = OpenXmlPowerTools.OpenXmlRegex.Match(content, regex);


                    int count2 = OpenXmlPowerTools.OpenXmlRegex.Replace(content, regex, replace_text, null);

                    statusBar1.Text = "Try 1: Found: " + count1 + ", Replaced: " + count2;


                    doc.MainDocumentPart.PutXDocument();

                }
            }
            catch(Exception e)
            {
                MessageBox.Show("Replace all exprienced error: " + e.Message);
            }

        }

基本的には、段落の内容に基づいてこの編集を行いたいと考えています。IDを使用して段落を取得することはできますが、IDを使用することはできません

IEnumerable<XElement> content = ydoc.Descendants(W.p);

これは通常のopenxmlメソッドを使用した私のアプローチですが、ファイルによっては多くのエラーが発生します.

  foreach (DocumentFormat.OpenXml.Wordprocessing.Paragraph para in bod.Descendants<DocumentFormat.OpenXml.Wordprocessing.Paragraph>())
                                    {

                                        foreach (var run in para.Elements<Run>())
                                        {
                                            foreach (var text in run.Elements<Text>())
                                            {
                                                string temp = text.Text;
                                                int firstlength = first.Length + 1;
                                                int secondlength = second.Length + 1;
                                                if (text.Text.Contains(first) && !(temp.Length > firstlength))
                                                {
                                                    text.Text = text.Text.Replace(first, "DELETED");

                                                }

                                                if (text.Text.Contains(second) && !(temp.Length > secondlength))
                                                {
                                                    text.Text = text.Text.Replace(second, "DELETED");

                                                }
                                            }
                                        }
                                    }

これが最後の新しいアプローチですが、私はそれにこだわっています

   private void redact_Replacebadones(string wfile)
        {
            try
            {
                using (WordprocessingDocument doc = WordprocessingDocument.Open(wfile, true))
                {
                    var ydoc = doc.MainDocumentPart.GetXDocument();
                  /*  from XElement xele in ydoc.Root.Elements();
                    List<string> lhsElements = xele.Elements("lhs")
                               .Select(el => el.Attribute("id").Value)
                               .ToList();
                               */
                    /// XElement
                    IEnumerable<XElement> content = ydoc.Descendants(W.p);

                   foreach (var p in content )

                    {
                        if (p.Value.Contains("each") && !p.Value.Contains("DELETED"))
                        {

                            string to_overwrite = p.Value;
                            Regex regexop = new Regex(@"\d+\.\d{2,3}");

                            regexop.Replace(to_overwrite, "Deleted");

                            p.SetValue(to_overwrite);

                            MessageBox.Show("NAME :" + p.GetParagraphInfo() +" VValue:"+to_overwrite);
                        }

                    }


                    doc.MainDocumentPart.PutXDocument();

                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Replace each exprienced error: " + e.Message);
            }

        } 
4

1 に答える 1