Rockyboys 2019-01-04 13:47 采纳率: 0%
浏览 1534

aspose word 表格单元格行距问题。

Table outerTable = CreateTable(doc, 3, 6, "");
///
///创建几行几列表格 Creates a new table in the document with the given dimensions and text in each cell.
///
private Table CreateTable(Document doc, int rowCount, int cellCount, string cellText)
{
Table table = new Table(doc);
// Create the specified number of rows.
for (int rowId = 1; rowId <= rowCount; rowId++)
{
Row row = new Row(doc);
table.AppendChild(row);

            // Create the specified number of cells for each row.
            for (int cellId = 1; cellId <= cellCount; cellId++)
            {
                Cell cell = new Cell(doc);  
                //需要设置一下这个值
                Paragraph p = new Paragraph(doc);
                p.AppendChild(new Run(doc, "")); 
                p.ParagraphFormat.Style.Font.Size = 10;
                p.ParagraphFormat.Style.Font.Name = "Arial"; 
                p.ParagraphFormat.Alignment = ParagraphAlignment.Center;
               p.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple; // 设置段落 
                p.ParagraphFormat.LineSpacing = 12;                            // 设置段落 默认给的是15.6磅**
                cell.AppendChild(p);   
                //  cell.CellFormat.Shading.BackgroundPatternColor = 颜色 ;
                cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;

// cell.FirstParagraph.AppendChild(new Run(doc, cellText));
row.AppendChild(cell);
}
}
return table;
}

p.ParagraphFormat.Alignment = ParagraphAlignment.Center;
p.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple; // 设置段落
p.ParagraphFormat.LineSpacing = 12; // 设置段落 默认给的是15.6磅
cell.AppendChild(p);

cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.FirstParagraph.AppendChild(new Run(doc,“test”));
row.AppendChild(cell);

p.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple; // 设置段落
p.ParagraphFormat.LineSpacing = 12; // 设置段落 默认给的是15.6磅
为何段落设置不起作用

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-07 15:21
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在Aspose.Words库中,CreateTable()方法的参数是Document对象和一个整数数组。每个元素代表要添加到表格中的行数。在你的示例代码中,你试图将一行设置为3行(即rowCount),但似乎没有正确地执行此操作。

    在你的代码中,你需要确保cellCount等于rowCount才能创建正确的表格结构。例如:

    private void CreateTable(Document doc, int rowCount, int cellCount, string cellText)
    {
        if (cellCount != rowCount) throw new ArgumentException("The number of columns must be equal to the number of rows.");
    
        Table table = new Table(doc);
    
        for (int rowId = 1; rowId <= rowCount; rowId++)
        {
            Row row = new Row(doc);
            table.AppendChild(row);
    
            for (int cellId = 1; cellId <= cellCount; cellId++)
            {
                Cell cell = new Cell(doc);
                paragraph = new Paragraph(doc);
                paragraph.AppendChild(new Run(doc, ""));
                paragraph.ParagraphFormat.Style.Font.Size = 10;
                paragraph.ParagraphFormat.Style.Font.Name = "Arial";
                paragraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                paragraph.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple;
                paragraph.ParagraphFormat.LineSpacing = 12;
                cell.AppendChild(paragraph);
    
                cell.CellFormat.Shading.BackgroundPatternColor = Color.Black;
                cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
    
                cell.FirstParagraph.AppendChild(new Run(doc, cellText));
                
                row.AppendChild(cell);
            }
        }
    }
    

    此外,你需要将row.AppendChild(cell)更改为row.AppendChild(cell.FirstParagraph)以正确地将段落插入到单元格中。这样可以避免对同一行重复添加段落的问题。

    评论

报告相同问题?