代码之家  ›  专栏  ›  技术社区  ›  BR75

不带空行的OpenXML/Xceed插入表

  •  0
  • BR75  · 技术社区  · 7 年前

    在插入表之后,我为一个空行在表之前和之后添加了\n,但是不知为什么Xceed库在表之后添加了三个,在表之前添加了两个。

    using (DocX document = DocX.Load(@"C:\Users\rk\Desktop\test.docx"))
                {
                    var IntTableSoftwareLocation = document.FindAll("Table").FirstOrDefault();
    
    
                    document.ReplaceText("Table", "");
    
                    var tableSoftware = document.InsertTable(IntTableSoftwareLocation, 3, 5);
    
                    tableSoftware.InsertParagraphBeforeSelf("\n");
                    tableSoftware.InsertParagraphAfterSelf("\n");
    
                    tableSoftware.SetBorder(TableBorderType.InsideH, new Border());
                    tableSoftware.SetBorder(TableBorderType.InsideV, new Border());
                    tableSoftware.SetBorder(TableBorderType.Left, new Border());
                    tableSoftware.SetBorder(TableBorderType.Bottom, new Border());
                    tableSoftware.SetBorder(TableBorderType.Top, new Border());
                    tableSoftware.SetBorder(TableBorderType.Right, new Border());
    
                    //Header
                    tableSoftware.Rows[0].Cells[0].Paragraphs[0].Append("Col1").Bold().Font("Arial").FontSize(11d);
                    tableSoftware.Rows[0].Cells[1].Paragraphs[0].Append("Col2").Bold().Font("Arial").FontSize(11d);
                    tableSoftware.Rows[0].Cells[2].Paragraphs[0].Append("Col3").Bold().Font("Arial").FontSize(11d);
                    tableSoftware.Rows[0].Cells[3].Paragraphs[0].Append("Col4.").Bold().Font("Arial").FontSize(11d);
                    tableSoftware.Rows[0].Cells[4].Paragraphs[0].Append("Col5").Bold().Font("Arial").FontSize(11d);
    
                    //Content
                    string TextToInsert = "Some Text";
    
                    //Column width
                    for (int i = 0; i < tableSoftware.RowCount; i++)
                    {
                        for (int x = 0; x < tableSoftware.ColumnCount; x++)
                        {
                            #region set column width
                            if (x == 0)
                            {
                                tableSoftware.Rows[i].Cells[x].Width = 28.3; // 1cm
                            }
                            else if (x == 1)
                            {
                                tableSoftware.Rows[i].Cells[x].Width = 318;
                            }
                            else if (x == 2)
                            {
                                tableSoftware.Rows[i].Cells[x].Width = 50;
                            }
                            else if (x == 3)
                            {
                                tableSoftware.Rows[i].Cells[x].Width = 28.3;
                            }
                            else if (x == 4)
                            {
                                tableSoftware.Rows[i].Cells[x].Width = 64;
                            }
                            #endregion
                        }
                    }
    
                    tableSoftware.Rows[2].Cells[1].Paragraphs[0].Append(TextToInsert + "\n").FontSize(11d).Bold().Font("Arial");
    
                    tableSoftware.Rows[2].Cells[2].Paragraphs[0].Append("User").Font("Arial").Alignment = Alignment.center;
                    tableSoftware.Rows[2].Cells[2].VerticalAlignment = VerticalAlignment.Center;
    
                    tableSoftware.Rows[2].Cells[3].Paragraphs[0].Append("1").Font("Arial").Alignment = Alignment.center;
                    tableSoftware.Rows[2].Cells[3].VerticalAlignment = VerticalAlignment.Center;
    
                    tableSoftware.Rows[2].Cells[4].Paragraphs[0].Append("2.199,00 €").Font("Arial").Alignment = Alignment.right;
                    tableSoftware.Rows[2].Cells[4].VerticalAlignment = VerticalAlignment.Center;
    
                    document.Save();
                } 
    

    我的docx文档就是这样的:

    laksjdf
    Table
    alskdfjs
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   BR75    7 年前

    好的,应该这样做:

    //Find the Paragraph by keyword
    var paraTable = document.Paragraphs.FirstOrDefault(x => x.Text.Contains("Table"));
    
    // Remove the Keyword
    paraTable.RemoveText(0);
    
    //Insert the table into Paragraph
    var table = paraTable.InsertTableAfterSelf(3, 5);
    

    再也没有奇怪的空行了