首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在字处理文档的表格中插入文本(Open XML )

在字处理文档的表格中插入文本(Open XML )
EN

Stack Overflow用户
提问于 2020-12-11 21:56:05
回答 1查看 355关注 0票数 0

我有一个带有页眉和页脚的文档文件。在页脚部分,我有一个表。

因此,现在我尝试在表格单元格中插入文本。但每当我尝试通过这段代码,它将是追加段落类型,并改变表格的高度和重量。

代码语言:javascript
运行
复制
  using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(ORiFilepath, true))
        {
         var docPart = wordDoc.MainDocumentPart;
            MainDocumentPart mainPart = wordDoc.MainDocumentPart;
            if (docPart.FooterParts.Count() > 0)
            {
                //List<Table> table = docPart.FooterParts..Elements<Table>().ToList();
                foreach (FooterPart footer in docPart.FooterParts)
                {
                    List<Table> table = footer.Footer.Elements<Table>().ToList();
                    if (table.Count > 0)
                    {
                        var footertable = table[0];
                        TableRow row1 = footertable.Elements<TableRow>().ElementAt(1);
                        string text1 = cell1.InnerText;
                        if (text1 == "")
                        {
                            cell1.Append(new Paragraph(new Run(new Text("TEXT"))));
                           
                        }
                    }
                }
            }
        }

是否可以使用将文本插入到单元格中

代码语言:javascript
运行
复制
cell1.Append(new Paragraph(new Run(new Text("TEXT"))));,

或者应该是什么方法?

EN

回答 1

Stack Overflow用户

发布于 2020-12-21 17:12:10

下面是我用来向单元格添加文本的方法。希望它是有用的。

CreateCellPropertiesCreateParagraph方法只是将相应的对象(TableCellPropertiesParagraphProperties)封装在OpenXML SDK中,您实际上不需要编写文本。

如果您仍然有问题,请告诉我:

代码语言:javascript
运行
复制
         public TableCell CreateCell(Shading shading, int width, int gridSpan, 
            TableCellBorders borders, TableVerticalAlignmentValues valign,
            JustificationValues justification, Color color, string text, 
            VericalMergeOptions mergeOptions)

        {
            // Set up Table Cell and format it.
            TableCell cell = new TableCell();
            

            TableCellProperties cellProperties = CreateCellProperties(shading, width, gridSpan, borders, valign, mergeOptions);
            ParagraphProperties props = CreateParagraphProperties(justification, color);
            
            // Set cell and paragraph
            Run run = CreateRun(color, text);
            Paragraph paragraph = new Paragraph(new List<OpenXmlElement>
            {
                props,
                run
            });

            // Append the run and the properties
            cell.Append(new List<OpenXmlElement>
            {
                cellProperties,
                paragraph
            });
            return cell;
        }

        protected Run CreateRun(Color color, string text = "")
        {

            Run run = new Run(new List<OpenXmlElement>{
                new RunProperties(color.CloneNode(true)),
            });

            if (text != "") run.Append(new Text(text));

            return run;
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65252682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档