【问题标题】:Populate .docx second table using OpenXml SDK WordprocessingDocument使用 OpenXml SDK WordprocessingDocument 填充 .docx 第二个表
【发布时间】:2019-12-31 05:00:32
【问题描述】:

我试图从控制器填充 .docx 文件中的 2 个表。填充第一个表没有问题。但我似乎找不到填充第二个的方法。 这是我的控制器代码。

        using (WordprocessingDocument sourceDoc = WordprocessingDocument.Open(Server.MapPath("~/doc/temp.docx"), false))
        using (var resultDoc = WordprocessingDocument.Create(stream, DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true))
        { 

            //Declare a TABLE class(This will get you first table of docx)
            Table table = body.Elements<Table>().First();

            //Accessing 1st row of the table
            TableRow row = table.Elements<TableRow>().ElementAt(0);
            //Accessing 2nd cell of that row
            TableCell cell = row.Elements<TableCell>().ElementAt(0);

            Paragraph p = cell.Elements<Paragraph>().First();
            Run r = p.Elements<Run>().First();
            Text text1 = r.Elements<Text>().First();
            //replacing text in that CELL
            text1.Text = text1.Text.Replace("xTitlex", "Title");

            //Accessing 2nd ROW of That table
            row = table.Elements<TableRow>().ElementAt(1);
            //Accessing 2nd CELL of that row
            cell = row.Elements<TableCell>().ElementAt(1);

            p = cell.Elements<Paragraph>().First();
            r = p.Elements<Run>().First();
            text1 = r.Elements<Text>().First();
            //Replacing text in that CELL
            text1.Text = text1.Text.Replace("xVal1x", "Item1");

            //This dont seem to work
            //Declare a 2nd TABLE class(This will get you second table of docx)
            Table table2 = body.Elements<Table>().Last();

            TableRow row2 = table2.Elements<TableRow>().ElementAt(0);
            ////Accessing 2nd cell of that row
            TableCell cell2 = row2.Elements<TableCell>().ElementAt(1);

            Paragraph p2 = cell.Elements<Paragraph>().Last();
            Run r2 = p.Elements<Run>().Last();
            Text text12 = r.Elements<Text>().Last();
            ////replacing text in that CELL
            text12.Text = text1.Text.Replace("xVal2x", "Item2");
}
    stream.Seek(0, SeekOrigin.Begin);

这就是 temp.docx 的样子。

【问题讨论】:

    标签: c# model-view-controller ms-word openxml openxml-sdk


    【解决方案1】:

    首先,您的示例中似乎缺少一些代码。例如,我看不到您如何初始化 body 变量。所以让我们假设你初始化body 基本上如下:

    Body body = sourceDoc.MainDocumentPart.Document.Body;
    

    接下来,查看您的代码,您混淆了一些变量,因此引用了错误的表。具体来说,您的代码中的以下几行正确选择了最后一个(在本例中为第二个)表,然后是第一行,最后是该行的第二个单元格。

    //This dont seem to work
    //Declare a 2nd TABLE class(This will get you second table of docx)
    Table table2 = body.Elements<Table>().Last();
    
    TableRow row2 = table2.Elements<TableRow>().ElementAt(0);
    ////Accessing 2nd cell of that row
    TableCell cell2 = row2.Elements<TableCell>().ElementAt(1);
    

    然后,但是,您根据第一个表的 cellp 和 @ 初始化下一个 ParagraphRunText 实例 p2r2text12 987654333@ 引用,完全忽略 cell2p2r2

    Paragraph p2 = cell.Elements<Paragraph>().Last();
    Run r2 = p.Elements<Run>().Last();
    Text text12 = r.Elements<Text>().Last();
    

    这意味着text12 引用了第一个表中包含的Text 实例。因此,最终,替换 "xVal2x" 没有任何效果,因为 Text 实例不包含该字符串。

    【讨论】:

      【解决方案2】:

      我得到了某人的帮助。这就是解决方案。

                     //Declare a 2nd TABLE class(This will get you second table of docx)
                      Table table2 = body.Elements<Table>().ElementAt(1);
      
                      //Access first row
                      row = table2.Elements<TableRow>().ElementAt(0);
                      //Accessing 2nd cell of that row
                      cell = row.Elements<TableCell>().ElementAt(1);
      
                      p = cell.Elements<Paragraph>().First();
                      r = p.Elements<Run>().First();
                      text1 = r.Elements<Text>().First();
                      //Replacing text in that CELL
                      text1.Text = text1.Text.Replace("xVal2x", "Item2");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-14
        • 1970-01-01
        • 2014-02-04
        • 1970-01-01
        • 1970-01-01
        • 2023-01-04
        • 2019-02-07
        相关资源
        最近更新 更多