繁体   English   中英

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

[英]Insert text into Table in a word processing document (Open XML )

我有 header 和页脚部分的文档文件。 在页脚部分,我有一张桌子。

所以现在我正在尝试将文本插入表格单元格。 但是每当我尝试使用此代码时,它将是 append 段落类型并更改表格的高度和重量。

  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"))));
                           
                        }
                    }
                }
            }
        }

我可以在单元格中插入文本吗

cell1.Append(new Paragraph(new Run(new Text("TEXT"))));,

或者应该是什么方法?

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

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

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

         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;
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM