使用Open XML SDK修复Word中的单元格宽度

时间:2012-06-25 09:49:55

标签: openxml xmltable wordprocessingml

我有一个docx文件中有一行的表,我想添加一些行。对于第一个现有的行,我将GridSpan设置为3.在下一个我想要添加一行只有两个单元格,然后我得到图片上的结果。如何将新行宽修复为表宽?当我在新行中只添加一个单元格时,我想做同样的事情。

enter image description here

我的代码:

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

2 个答案:

答案 0 :(得分: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;
        }

答案 1 :(得分:0)

TableCell tCell = new TableCell();

tCell.Append(new TableCellProperties(
    new GridSpan { Val = table.LastChild.ChildElements.Count },
    new Justification { Val = JustificationValues.Right })
);