【问题标题】:Adding text to multiples rows in word with a single bookmark使用单个书签将文本添加到 word 中的多行
【发布时间】:2013-04-03 16:08:26
【问题描述】:

是否可以借助 Bookmarks 和 openXML 在 word 文档中添加几行?

我们有一个用作报告模板的 worddocument。 在该模板中,我们需要添加几个事务行。 问题是行数不是静态的。例如,它可以是 0、1 或 42。

在当前模板(我们可以更改)中,我们添加了 3 个书签 TransactionPart、TransactionPart2 和 TransactionPart3。 树事务部分形成具有三个不同数据内容(ID、描述、金额)的单行

如果我们只有一个事务行,将数据添加到这些书签中没有问题,但是当我们应该添加第二行时该怎么办?没有更多行的书签。

有什么聪明的方法吗?

或者我们应该更改 worddocument 以使行最终出现在表格中?这样会更好地解决问题吗?

【问题讨论】:

    标签: c# openxml


    【解决方案1】:

    我会在一个 3 列的表中放置一个书签,我们称之为“事务”。 像这样

    当您知道表格的设计,但不知道您需要的行数时,最简单的方法是为您拥有的每一行数据添加一行。

    你可以用这样的代码来实现

    //make some data.
                List<String[]> data = new List<string[]>();
    
                for (int i = 0; i < 10; i++)
                    data.Add(new String[] {"this","is","sparta" });
        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open("yourDocument.docx", true))
                    {
                        var mainPart = wordDoc.MainDocumentPart;
                        var bookmarks = mainPart.Document.Body.Descendants<BookmarkStart>();
                        var bookmark = 
                            from n in bookmarks 
                            where n.Name == "transactions" 
                            select n;
    
                        OpenXmlElement elem = bookmark.First().Parent;
                        //isolate tabel
                        while (!(elem is DocumentFormat.OpenXml.Wordprocessing.Table))
                            elem = elem.Parent;
                        var table = elem; //found
                        //save the row you wanna copy in each time you have data.
                        var oldRow = elem.Elements<TableRow>().Last();
                        DocumentFormat.OpenXml.Wordprocessing.TableRow row = (TableRow)oldRow.Clone();
                        //remove old row
                        elem.RemoveChild<TableRow>(oldRow);
                        foreach (String[] s in data)
                        {
                            DocumentFormat.OpenXml.Wordprocessing.TableRow newrow = (TableRow)row.Clone();
                            var cells = newrow.Elements<DocumentFormat.OpenXml.Wordprocessing.TableCell>();
                            //we know we have 3 cells
                            for(int i = 0; i < cells.Count(); i++)
                            {
                                var c = cells.ElementAt(i);
                                var run = c.Elements<Paragraph>().First().Elements<Run>().First();
                                var text = run.Elements<Text>().First();
                                text.Text = s[i];
                            }
                            table.AppendChild(newrow);
                        }
                    }
    

    你最终得到了这个

    我已经在一个非常基本的文档上测试了这段代码,并且知道它可以工作。 祝你好运,如果我能进一步澄清,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-06
      • 2019-04-18
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 2023-04-07
      相关资源
      最近更新 更多