【问题标题】:Aspose C# DOCX ignoring table Cell width and heightAspose C# DOCX 忽略表格单元格宽度和高度
【发布时间】:2017-11-08 17:03:52
【问题描述】:

我正在尝试指定表格的宽度和高度。

最终目标是第一列占 90% 左右,最后一列占剩余 10%。 我尝试了许多不同的组合,但 word 似乎忽略了它。

我的文档生成器代码在这里:

        var table = docBuilder.StartTable();
        docBuilder.InsertCell();
        docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(90);
        docBuilder.CellFormat.ClearFormatting();
        docBuilder.Font.Bold = true;
        docBuilder.Font.Name = Stylings.TITLEFONT;
        docBuilder.Font.Size = Stylings.TITLESIZE1;
        docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
        docBuilder.Write("Description");

        var cell = docBuilder.InsertCell();
        docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10);
        docBuilder.CellFormat.FitText = false;            
        docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
        docBuilder.Font.Name = Stylings.TITLEFONT;
        docBuilder.Font.Size = Stylings.TITLESIZE1;
        docBuilder.Font.Bold = true;
        docBuilder.Write("Amount (inc GST)");
        docBuilder.EndRow();

        docBuilder.InsertCell();
        docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(90);
        docBuilder.Font.Bold = false;
        docBuilder.Font.Name = Stylings.NORMALFONT;
        docBuilder.Font.Size = Stylings.NORMALSIZE1;
        docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
        docBuilder.Write(description);

        docBuilder.InsertCell();
        docBuilder.RowFormat.HeightRule = HeightRule.AtLeast;
        docBuilder.RowFormat.Height = 5;
        docBuilder.CellFormat.FitText = false;
        docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10);
        docBuilder.Font.Bold = false;
        docBuilder.Font.Name = Stylings.NORMALFONT;
        docBuilder.Font.Size = Stylings.NORMALSIZE1;
        docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
        docBuilder.Write(total.ToString("C"));
        docBuilder.EndRow();

        docBuilder.InsertCell();
        docBuilder.RowFormat.HeightRule = HeightRule.Auto;
        docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(90);

        docBuilder.InsertCell();
        docBuilder.CellFormat.FitText = false;
        docBuilder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10);
        docBuilder.Font.Bold = false;
        docBuilder.Font.Name = Stylings.NORMALFONT;
        docBuilder.Font.Size = Stylings.NORMALSIZE1;
        docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
        docBuilder.Write(total.ToString("C"));
        docBuilder.EndRow();

        //Table Formatting
        table.AutoFit(AutoFitBehavior.FixedColumnWidths);
        table.PreferredWidth = PreferredWidth.FromPercent(100);
        docBuilder.EndTable();

【问题讨论】:

    标签: aspose.words


    【解决方案1】:

    请使用以下示例代码 sn-p 创建具有不同相对大小的单元格的表格。

    我与 Aspose 合作,担任开发人员传道者。

    // Insert a table row made up two cells which have different preferred widths.
    Table table = builder.StartTable();
    
    // Insert a relative ( 90 percent) sized cell.
    builder.InsertCell();
    builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(90);
    builder.CellFormat.ClearFormatting();
    builder.Font.Bold = true;
    builder.Font.Name = "Arial";
    builder.Font.Size = 10;
    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.Writeln("Description");
    
    // Insert a relative ( 10 percent) sized cell.
    builder.InsertCell();
    builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10);
    builder.CellFormat.ClearFormatting();
    builder.Font.Bold = true;
    builder.Font.Name = "Arial";
    builder.Font.Size = 10;
    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.Writeln("Amount (inc GST)");
    builder.EndRow();
    
    // Insert a relative ( 90 percent) sized cell.
    builder.InsertCell();
    builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(90);
    builder.CellFormat.ClearFormatting();
    builder.Font.Bold = true;
    builder.Font.Name = "Arial";
    builder.Font.Size = 10;
    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.Writeln("Aspose.Words for .NET 17.10");
    
    // Insert a relative ( 10 percent) sized cell.
    builder.InsertCell();
    builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10);
    builder.CellFormat.ClearFormatting();
    builder.Font.Bold = true;
    builder.Font.Name = "Arial";
    builder.Font.Size = 10;
    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.Writeln("1000.00");
    builder.EndRow();
    

    【讨论】:

    • 是builder.CellFormat.ClearFormatting();那是在做这项工作吗?
    • 否,设置单元格的 PreferredWidth 会产生预期的结果。