2

docx ファイルに 1 行のテーブルがあり、いくつかの行を追加したいと考えています。最初の既存の行では、GridSpan を 3 に設定します。次の行では、2 つのセルのみを含む行を追加すると、図のような結果が得られます。新しい行幅をテーブル幅に固定するにはどうすればよいですか? 新しい行にセルを1つだけ追加するときも、同じことをしたいです。

ここに画像の説明を入力

私のコード:

private static TableRow AddRow(WordprocessingDocument docx, Table tbl, int cellsQuantity)
        {
            TableRow newRow = new TableRow();

            var firstCell = tbl.Descendants<TableRow>().First()
                  .Descendants<TableCell>().First();

            firstCell.Append(new TableCellProperties(new GridSpan() { Val = 3 }));

            for (int i = 0, max = cellsQuantity; i < max; i++)
            {
                // TableCellProperties tcp = new TableCellProperties(new TableCellWidth() { Width = firstCell.TableCellProperties.TableCellWidth.Width, Type = firstCell.TableCellProperties.TableCellWidth.Type });
                TableCell cell = new TableCell(new Paragraph(new Run(new Text("test"))));
                newRow.AppendChild(cell);
            }

            tbl.Append(newRow);

            return newRow;
        }
4

2 に答える 2

3

わかりました、私はそれを手に入れました:)

 private static TableRow AddRow(WordprocessingDocument docx, Table tbl, int cellsQuantity)
        {
            TableRow newRow = new TableRow();

            var grid = tbl.Descendants<TableGrid>().First();

            var firstCell = tbl.Descendants<TableRow>().First()
                  .Descendants<TableCell>().First();

            firstCell.Append(new TableCellProperties(new GridSpan() { Val = 4 }));

            int ind = 0;
            int[] widthPerColumn = new int[] { 3070, 1536, 1535, 3071 };
            grid.Descendants<GridColumn>().ToList().ForEach(x =>
            {
                x.Width = widthPerColumn[ind++].ToString();
            });

            int[] gridSpan = null;
            switch (cellsQuantity)
            {
                case 4:
                    gridSpan = new int[] { 1, 1, 1, 1 };
                    break;
                case 3:
                    gridSpan = new int[] { 1, 2, 1 };
                    break;
                case 2:
                    gridSpan = new int[] { 2, 2 };
                    break;
                case 1:
                    gridSpan = new int[] { 4 };
                    break;
                default:
                    throw new InvalidOperationException("The cellsQuantity variable must have a value from [1,4] range");
            }

            for (int i = 0, max = cellsQuantity; i < max; i++)
            {
                TableCellProperties tcp = new TableCellProperties(new GridSpan() { Val = gridSpan[i] });
                TableCell cell = new TableCell(tcp, new Paragraph(new Run(new Text("test"))));
                newRow.AppendChild(cell);
            }

            tbl.Append(newRow);

            return newRow;
        }
于 2012-06-25T10:38:26.593 に答える
0
TableCell tCell = new TableCell();

tCell.Append(new TableCellProperties(
    new GridSpan { Val = table.LastChild.ChildElements.Count },
    new Justification { Val = JustificationValues.Right })
);
于 2014-10-07T13:23:46.623 に答える