1

Is there a way to create a word table with more than 15 columns using Novacode DocX?

If I create a new table with greater than 14 columns, the table doesn't appear. I can get around this by doing something like this:

int addCols = Math.Min(data.colCount, 14);
Table docTable = doc.InsertTable(data.rowCount, addCols);

And then later this:

docTable.InsertColumn();

However, if I try to do this more than once to create a table with 16 or more columns, I have the same issue where the table doesn't appear.

Is there any way around this?

4

2 回答 2

1

图书馆中有类似错误的东西。当您创建一个包含许多列的表时,它变得不可用。原因是,每列都是以其初始宽度创建的。当您创建包含许多列的表格(独立于所使用的方法:InsertTable、AddTable、InsertTableBeforeSelf 等)并且它们的宽度总和超过文档宽度时 - 表格将变得不可用。

我通过在循环中添加列并减小它们的宽度来解决这个问题。我的代码与此类似:

private void CreateSampleTable(DocX document)
{
    int rowsCount = 10;
    int columnsCount = 20;
    int columnWidth = 30;

    Table sampleTable = document.AddTable(rowsCount, 1);
    foreach (Row row in sampleTable.Rows)
    {
        row.Cells[0].Width = columnWidth;
    }

    for (int colIndex = 1; colIndex < columnsCount; colIndex++)
    {
        sampleTable.InsertColumn(colIndex);
        foreach (Row row in sampleTable.Rows)
        {
            row.Cells[colIndex].Width = columnWidth;
        }
    }

    Paragraph par = document.InsertParagraph();
    par.InsertTableBeforeSelf(sampleTable);
}
于 2016-02-19T20:39:44.210 回答
1

这是我解决这个问题的方法。如果您需要许多具有随机大量列的不同表,这可能不起作用,因为这需要大量工作。

using (DocX template = DocX.Load("template.docx"))
{
     Novacode.Table tempTable;
     using (DocX template2 = DocX.Load("template2.docx"))
     {
          tempTable = template2.Tables[0];
     }
     Novacode.Table t1 = doc.InsertTable(tempTable);
     t1.InsertRow();
     t1.InsertRow();
     template.Save();
}

这是一个可能的解决方案。templateDocX您要插入的Tabletemplate2包含 1 行的预制件Tables,并且有任意数量的列。大小(1,15)template2.Table[0]也是如此。然后,Table您可以添加更多(通过在 Microsoft Word 中的文档内创建它们来在代码之外)变得更大:将是一个大小 (1,16)。唯一的问题是,如果您需要处理大量不同数量的列。从头开始构建东西不是一个很好的库。Tablestemplate2template2.Table[1]TableTablesNovacode-dox

希望这可能是您的工作。

于 2015-08-20T21:32:06.380 回答