如何使所有行具有相同的大小?

时间:2016-01-26 23:39:12

标签: c# ms-word openxml

我是openxml的新手,我正在努力学习基础知识。我一直在尝试创建具有不同属性的表但无法传递问题。我需要在表中使所有行具有相同的宽度,忽略它们具有的列数。 这是我的代码:

 public static Table createTable(String[][] data)
     {
         Table table = new Table();

         table.AppendChild<TableProperties>(createTableProps());
         for (var i = 0; i < data.Length; i++)
         {
             var tr = new TableRow();
             for (var j = 0; j < data[i].Length; j++)
             {
                 int size = 1200 / data[i].Length;
                 var tc = new TableCell();

                 tc.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = size.ToString()}));


                Paragraph para = new Paragraph(runTools.getRun(16, "Tahoma", new Text(data[i][j])));

                 tc.Append(para);

                 // Assume you want columns that are automatically sized.
                /* TableCellWidth tcw = new TableCellWidth{Type = TableWidthUnitValues.Auto };
                 tcw.Width = new StringValue("500");
                 tc.Append(new TableCellProperties(tcw));*/

                 tr.Append(tc);
             }
             table.Append(tr);
         }
         return table;
     }

这就是我需要的:

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要在顶部列中添加data[i].Length == 1,以使其跨越2列。调整色谱柱的宽度不起作用,因为细胞不能比色谱柱的其余部分宽。

来自documentation for GridSpan

  

此属性允许单元格具有合并的外观,因为它们跨越表格中其他单元格的垂直边界

在您的代码中,您可以添加for (var i = 0; i < data.Length; i++) { var tr = new TableRow(); for (var j = 0; j < data[i].Length; j++) { var tc = new TableCell(); tc.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Auto })); Paragraph para = new Paragraph(runTools.getRun(16, "Tahoma", new Text(data[i][j]))); if (data[i].Length == 1) { //add a GridSpan with a value of 2 so this cell spans across 2 columns tc.TableCellProperties.AppendChild(new GridSpan() { Val = 2 }); //center justify the text if (para.ParagraphProperties == null) para.ParagraphProperties = new ParagraphProperties(); para.ParagraphProperties.Justification = new Justification() { Val = JustificationValues.Center }; } tc.Append(para); tr.Append(tc); } table.Append(tr); } 的检查 - 如果是,则可以向该单元格添加跨度以跨越2个单元格。例如:

select * from mytable where c = 1 and d = 1 limit 2
union all
select * from mytable where c = 1 and d = 0
order by d desc;