C# 获取存在特定文本字符串的表的格式,并创建具有相同格式的新表

C# 获取存在特定文本字符串的表的格式,并创建具有相同格式的新表,c#,openxml,document,C#,Openxml,Document,在C#中使用OpenXML,我们需要: 在Word文档中查找特定的文本字符串(此文本将始终存在于表格单元格中) 获取文本的格式以及文本所在的表 创建具有相同文本和表格格式的新表格,同时从嵌套列表中为单元格引入文本值 这是我目前拥有的代码和我不确定如何做到的地方: using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileWordFile, true)) { MainDocumentPart mainPa

在C#中使用OpenXML,我们需要:

  • 在Word文档中查找特定的文本字符串(此文本将始终存在于表格单元格中
  • 获取文本的格式以及文本所在的表
  • 创建具有相同文本和表格格式的新表格,同时从嵌套列表中为单元格引入文本值
  • 这是我目前拥有的代码和我不确定如何做到的地方:

    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileWordFile, true))
    {
        MainDocumentPart mainPart = wordDoc.MainDocumentPart;
        Body body = mainPart.Document.Body;
        IEnumerable paragraphs = body.Elements<Paragraph>();
        Paragraph targetParagraph = null;
        //Comment 1: Loop through paragraphs and search for a specific string of text in word document
        foreach (Paragraph paragraph in paragraphs) {
            if(paragraph.Elements<Run>().Any()) {
                Run run = paragraph.Elements<Run>().First();
                if(run.Elements<Text>().Any()) {
                    Text text = run.Elements<Text>().First();
                    if (text.Text.Equals("MY SEARCH STRING")) {
                        targetParagraph = paragraph;
                        // Comment 2: How can I get the formatting of the table that contains this text??
    
                    }
                }
            }
        }
    
        //Comment 3: Create table with same formatting as where the text was found
        Table table1 = new Table();
        TableProperties tableProperties1 = new TableProperties();
        //Comment 4: How can I set these properties to be the same as the one found at "Comment 2"??
    
    
        wordDoc.Close();
        wordDoc.Dispose();
    }
    
    使用(WordprocessingDocument wordDoc=WordprocessingDocument.Open(fileWordFile,true))
    {
    MainDocumentPart mainPart=wordDoc.MainDocumentPart;
    Body Body=mainPart.Document.Body;
    IEnumerable段落=body.Elements();
    段落目标段落=空;
    //注释1:循环浏览段落并在word文档中搜索特定的文本字符串
    foreach(段落中的段落){
    if(段落.Elements().Any()){
    Run=段落.Elements().First();
    if(run.Elements().Any()){
    Text Text=run.Elements().First();
    if(text.text.Equals(“我的搜索字符串”)){
    目标段落=段落;
    //注释2:如何获取包含此文本的表的格式??
    }
    }
    }
    }
    //注释3:使用与找到文本的位置相同的格式创建表
    表1=新表();
    TableProperties tableProperties1=新的TableProperties();
    //注释4:如何将这些属性设置为与“注释2”中的属性相同??
    wordDoc.Close();
    Dispose();
    }
    
    如果要查找表单元格中的文本元素,可以使用LINQ查询快速到达,而无需使用嵌套循环堆

    // Find the first text element matching the search string 
    // where the text is inside a table cell.
    var textElement = body.Descendants<Text>()
                          .FirstOrDefault(t => t.Text == searchString && 
                                               t.Ancestors<TableCell>().Any());
    
    希望这有帮助

    if (textElement != null)
    {
        // get the table containing the matched text element and clone it
        Table table = textElement.Ancestors<Table>().First();
        Table tableCopy = (Table)table.CloneNode(deep: true);
    
        // do stuff with copied table (see below)
    }
    
        // find the table cell containing the search string in the copied table
        var targetCell = tableCopy.Descendants<Text>()
                                  .First(t => t.InnerText == searchString)
                                  .Ancestors<TableCell>()
                                  .First();
    
        // get the properties from the first paragraph in the target cell (so we can copy them)
        var paraProps = targetCell.Descendants<ParagraphProperties>().First();
    
        // now add new stuff to the target cell
        List<string> stuffToAdd = new List<string> { "foo", "bar", "baz", "quux" };
        foreach (string item in stuffToAdd)
        {
            // for each item, clone the paragraph properties, then add a new paragraph
            var propsCopy = (ParagraphProperties)paraProps.CloneNode(deep: true);
            targetCell.AppendChild(new Paragraph(propsCopy, new Run(new Text(item))));
        }
    
        body.AppendChild(tableCopy);