您好,我想在word插入表格,我查看了API没有看明白。

在此先感谢您。

您好,我想实现下面的word表格样式,我该如何实现。

数据是从excel里获取过来,然后形成word表格,其中word表格 “卷内文件目录” 每页都要显示。

实现效果.docx (101.5 KB)

我使用以下代码无法实现。

Aspose.Words.Document doc = new Aspose.Words.Document();
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

Aspose.Words.Tables.Table table = builder.StartTable();

//插入单元格
builder.InsertCell();

//自动调整
table.AutoFit(Aspose.Words.Tables.AutoFitBehavior.FixedColumnWidths);

builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
builder.Write("This is row 1 cell 1");

builder.InsertCell();
builder.Write("This is row 1 cell 2");

builder.EndRow();

builder.InsertCell();

builder.RowFormat.Height = 100;
builder.RowFormat.HeightRule = Aspose.Words.HeightRule.Exactly;
builder.CellFormat.Orientation = Aspose.Words.TextOrientation.Upward;
builder.Writeln("This is row 2 cell 1");

builder.InsertCell();
builder.CellFormat.Orientation = Aspose.Words.TextOrientation.Downward;
builder.Writeln("This is row 2 cell 2");

builder.EndRow();
builder.EndTable();

doc.Save("AddContentUsingDocumentBuilder.BuildTable.docx");

@fhn123456 您的代码是正确的,但您没有指定单元格宽度。 请看下面的简单代码:

Aspose.Words.Document doc = new Aspose.Words.Document();
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

builder.StartTable();

builder.InsertCell();
builder.CellFormat.Width = 25;
builder.Write("row 1 cell 1");
builder.InsertCell();
builder.CellFormat.Width = 25;
builder.Write("row 1 cell 2");
builder.InsertCell();
builder.CellFormat.Width = 25;
builder.Write("row 1 cell 3");

builder.InsertCell();
builder.CellFormat.Width = 290;
builder.Write("row 1 cell 4");

builder.InsertCell();
builder.CellFormat.Width = 25;
builder.Write("row 1 cell 5");
builder.InsertCell();
builder.CellFormat.Width = 25;
builder.Write("row 1 cell 6");
builder.InsertCell();
builder.CellFormat.Width = 25;
builder.Write("row 1 cell 7");

builder.EndRow();

Aspose.Words.Tables.Table table = builder.EndTable();

table.AutoFit(Aspose.Words.Tables.AutoFitBehavior.FixedColumnWidths);

doc.Save(@"C:\Temp\out.docx");

此外,如果我正确理解您的要求,您可以根据数据构建表格。 因此,在您的情况下,您可以创建模板,然后使用 Mail Merge With RegionsLINQ Reporting Engine 填充数据。

非常感谢您的回复,这几天我查看了API,以下代码并不能实现我想要的格式。

希望您,可以帮助我,非常感谢您。

1,如何合并第一行所有的单元格。
2,如何第一行第二行边框设置成无。

想要实现的效果:卷 内 目 录.docx (10.8 KB)

打印效果图片:333.jpg (13.1 KB)

//文档示例
Aspose.Words.Document doc = new Aspose.Words.Document();
//文档构造器
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
Aspose.Words.Tables.Table table = builder.StartTable();

//7列表头  卷内目录
for (int i = 0; i < 7; i++)
{
    //制作标题行  //插入单元格
    builder.InsertCell();
    builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.None;
}

builder.Write("卷   内   目   录");
builder.RowFormat.Height = 30;
builder.Font.Size = 1000;
builder.Font.Bold = true;
//结束行
builder.EndRow();



//7列表头  编码
for (int i = 0; i < 7; i++)
{
    //制作标题行  //插入单元格
    builder.InsertCell();
}

//设置高度并定义标题行的高度规则。行高
builder.RowFormat.Height = 10;
//标题行的一些特殊功能。
//builder.CellFormat.Shading.BackgroundPatternColor = Color.FromArgb(198, 217, 241);
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;
builder.Font.Size = 10;
builder.Font.Bold = true;
builder.Write("0003-012-0721");
builder.EndRow();


//7列表头  表头
for (int i = 0; i < 7; i++)
{
    //制作标题行  //插入单元格
    builder.InsertCell();
}

//我们不需要指定此单元格的宽度,因为它是从前一个单元格继承的。          
builder.RowFormat.Height = 30;
builder.Font.Size = 20;
builder.Font.Bold = true;
builder.Write("顺\n序\n号");
builder.Write("文号");
builder.Write("责任者");
builder.Write("题名");
builder.Write("日期");
builder.Write("页号");
builder.Write("备注");
builder.EndRow();

//设置表格的左缩进。必须在之后应用表格宽格式
//表格中至少有一行。
table.LeftIndent = 10.0;

doc.Save($"{jnMlSettingsData.jNSavePath}\\666.docx");

//打开保存的word文件夹
System.Diagnostics.Process.Start($"{jnMlSettingsData.jNSavePath}\\666.docx");

@fhn123456

为此,您应该使用水平合并的单元格。 请参阅您的文档以了解如何使用合并的单元格。
https://docs.aspose.com/words/net/working-with-columns-and-rows/#work-with-merged-cells

您可以使用 CellFormat.Borders 应用单元格的边框格式。 要删除边框,您应该将边框类型设置为 LineStyle.None

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Font.Name = "Sim Sun";

Table table = builder.StartTable();

// The first row
builder.InsertCell();
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.CellFormat.Width = 420;
builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.None;
builder.Font.Size = 15;
builder.Font.Bold = true;
builder.Write("卷   内   目   录");
builder.EndRow();

// The second row.
builder.InsertCell();
builder.CellFormat.Width = 420;
builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.None;
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;
builder.Font.Size = 10;
builder.Font.Bold = true;
builder.Write("0003-012-0721");
builder.EndRow();


// The third row
builder.InsertCell();
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.CellFormat.Borders.LineStyle = LineStyle.Single;
builder.CellFormat.Width = 30;
builder.Font.Size = 10.5;
builder.Font.Bold = false;
builder.Write("顺\n序\n号");

builder.InsertCell();
builder.CellFormat.Width = 45;
builder.Write("文号");

builder.InsertCell();
builder.CellFormat.Width = 44;
builder.Write("责任者");

builder.InsertCell();
builder.CellFormat.Width = 150;
builder.Write("题名");

builder.InsertCell();
builder.CellFormat.Width = 64;
builder.Write("日期");

builder.InsertCell();
builder.CellFormat.Width = 40;
builder.Write("页号");

builder.InsertCell();
builder.CellFormat.Width = 47;
builder.Write("备注");
builder.EndRow();

table.LeftIndent = 10.0;

doc.Save(@"C:\Temp\out.docx");

非常感谢您的热心回复和帮助,这正是我想要的。

我尝试让第三行头部显示边框,下面这行代码不生效。

builder.CellFormat.Borders.Top.LineStyle = Aspose.Words.LineStyle.Single;

错误:

// The third row
builder.InsertCell();
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.CellFormat.Borders.LineStyle = LineStyle.Single;

builder.CellFormat.Borders.Top.LineStyle = Aspose.Words.LineStyle.Single;

builder.CellFormat.Width = 30;
builder.Font.Size = 10.5;
builder.Font.Bold = false;
builder.Write("顺\n序\n号");

builder.InsertCell();
builder.CellFormat.Width = 45;
builder.Write("文号");

builder.InsertCell();
builder.CellFormat.Width = 44;
builder.Write("责任者");

builder.InsertCell();
builder.CellFormat.Width = 150;
builder.Write("题名");

builder.InsertCell();
builder.CellFormat.Width = 64;
builder.Write("日期");

builder.InsertCell();
builder.CellFormat.Width = 40;
builder.Write("页号");

builder.InsertCell();
builder.CellFormat.Width = 47;
builder.Write("备注");
builder.EndRow();

@fhn123456 请尝试在表格中指定第二行的底部边框:

// The second row.
builder.InsertCell();
builder.CellFormat.Width = 420;
builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.None;
builder.CellFormat.Borders.Bottom.LineStyle = LineStyle.Single;
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;
builder.Font.Size = 10;
builder.Font.Bold = true;
builder.Write("0003-012-0721");
builder.EndRow();

谢谢您,这是我想要的结果,非常感谢您。

1,您好,我想让表格,第一行,第二行,第三行,作为 每页word的表头,应该如何实现,API有继承表头相关的代码吗,谢谢您。

2,最后一页word 表格,如何用空表格行填充完整。

想要实现:实现效果.docx (101.5 KB)

目前实现:目前代码实现的word.docx (13.7 KB)

以下是我向您提供的全部代码,谢谢。

        Aspose.Words.Document doc = new Aspose.Words.Document();
        Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
        builder.Font.Name = "SimSun";

        Aspose.Words.Tables.Table table = builder.StartTable();

        // 第一行  卷内目录
        builder.InsertCell();
        //文字对齐方式
        builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;
        builder.CellFormat.Width = 420;
        builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.None;
        builder.Font.Size = 20;
        builder.Font.Bold = true;
        builder.Write("卷   内   目   录");
        builder.EndRow();

        // 第二行 如果设置第三行边框请在第二行设置
        builder.InsertCell();
        builder.CellFormat.Width = 420;
        builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.None;
        //builder.CellFormat.Borders.Left.LineStyle = Aspose.Words.LineStyle.None;
        //builder.CellFormat.Borders.Right.LineStyle = Aspose.Words.LineStyle.None;
        builder.CellFormat.Borders.Bottom.LineStyle = Aspose.Words.LineStyle.Single;
        builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;
        builder.Font.Size = 13;
        builder.Font.Bold = true;
        builder.Write("0003-012-0721");
        builder.EndRow();

        // 第三行
        builder.InsertCell();
        //设置单元格内容对齐方式  垂直对齐
        builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
        builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;
        builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;
        builder.CellFormat.Borders.Top.LineStyle = Aspose.Words.LineStyle.Single;

        builder.CellFormat.Width = 30;
        builder.Font.Size = 10.5;
        builder.Font.Bold = true;
        builder.Write("顺\n序\n号");

        builder.InsertCell();
        builder.CellFormat.Width = 45;
        builder.Write("文号");

        builder.InsertCell();
        builder.CellFormat.Width = 44;
        builder.Write("责任者");

        builder.InsertCell();
        builder.CellFormat.Width = 150;
        builder.Write("题名");

        builder.InsertCell();
        builder.CellFormat.Width = 64;
        builder.Write("日期");

        builder.InsertCell();
        builder.CellFormat.Width = 40;
        builder.Write("页号");

        builder.InsertCell();
        builder.CellFormat.Width = 47;
        builder.Write("备注");
        builder.EndRow();


        //添加数据
        DataTable dt_temp = new DataTable();

        for (int i = 1; i <= 7; i++)
        {
            dt_temp.Columns.Add($"Columns{i}");
        }

        for (int i = 0; i < 30; i++)
        {
            DataRow dr = dt_temp.NewRow();
            dr["Columns1"] = i;
            dr["Columns2"] = "文号";
            dr["Columns3"] = "责任者";
            dr["Columns4"] = "题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名";
            dr["Columns5"] = "20221113";
            dr["Columns6"] = "132";
            dr["Columns7"] = "666";
            dt_temp.Rows.Add(dr);
        }


        for (int i = 0; i < dt_temp.Rows.Count; i++)
        {

            for (int j = 0; j < 7; j++)
            {
                //添加一个单元格
                builder.InsertCell();
                builder.Font.Bold = false;
                string clee = "";

                switch (j)
                {

                    case 0:
                        builder.CellFormat.Width = 30;
                        clee = dt_temp.Rows[i][j].ToString();
                        break;

                    case 1:
                        builder.CellFormat.Width = 45;
                        clee = dt_temp.Rows[i][j].ToString();

                        break;
                    case 2:
                        builder.CellFormat.Width = 44;
                        clee = dt_temp.Rows[i][j].ToString();
                        break;

                    case 3:
                        builder.CellFormat.Width = 150;
                        clee = dt_temp.Rows[i][j].ToString();
                        break;

                    case 4:
                        builder.CellFormat.Width = 64;
                        clee = dt_temp.Rows[i][j].ToString();
                        break;

                    case 5:
                        builder.CellFormat.Width = 40;
                        clee = dt_temp.Rows[i][j].ToString();
                        break;

                    case 6:
                        builder.CellFormat.Width = 47;
                        clee = dt_temp.Rows[i][j].ToString();
                        break;
 
                }

                builder.Write(clee);

            }

            builder.EndRow();
        }


        table.LeftIndent = 10;

        doc.Save($"{jnMlSettingsData.jNSavePath}\\666.docx");

        //打开保存的word文件夹
        System.Diagnostics.Process.Start($"{jnMlSettingsData.jNSavePath}\\666.docx");

@fhn123456 您可以使用 RowFormat.HeadingFormat 将行标记为标题行。

MS Word 文档是流文档,不包含有关文档布局的信息。 但是,使用 Aspose.Words,您可以使用 LayoutCollector 检测分页符。

请看以下修改后的代码:

Aspose.Words.Document doc = new Aspose.Words.Document();
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
builder.Font.Name = "SimSun";

Aspose.Words.Tables.Table table = builder.StartTable();

// 第一行  卷内目录
builder.InsertCell();
builder.RowFormat.HeadingFormat = true;
//文字对齐方式
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;
builder.CellFormat.Width = 420;
builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.None;
builder.Font.Size = 20;
builder.Font.Bold = true;
builder.Write("卷   内   目   录");
builder.EndRow();

// 第二行 如果设置第三行边框请在第二行设置
builder.InsertCell();
builder.RowFormat.HeadingFormat = true;
builder.CellFormat.Width = 420;
builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.None;
//builder.CellFormat.Borders.Left.LineStyle = Aspose.Words.LineStyle.None;
//builder.CellFormat.Borders.Right.LineStyle = Aspose.Words.LineStyle.None;
builder.CellFormat.Borders.Bottom.LineStyle = Aspose.Words.LineStyle.Single;
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;
builder.Font.Size = 13;
builder.Font.Bold = true;
builder.Write("0003-012-0721");
builder.EndRow();

// 第三行
builder.InsertCell();
builder.RowFormat.HeadingFormat = true;
//设置单元格内容对齐方式  垂直对齐
builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;
builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;
builder.CellFormat.Borders.Top.LineStyle = Aspose.Words.LineStyle.Single;

builder.CellFormat.Width = 30;
builder.Font.Size = 10.5;
builder.Font.Bold = true;
builder.Write("顺\n序\n号");

builder.InsertCell();
builder.CellFormat.Width = 45;
builder.Write("文号");

builder.InsertCell();
builder.CellFormat.Width = 44;
builder.Write("责任者");

builder.InsertCell();
builder.CellFormat.Width = 150;
builder.Write("题名");

builder.InsertCell();
builder.CellFormat.Width = 64;
builder.Write("日期");

builder.InsertCell();
builder.CellFormat.Width = 40;
builder.Write("页号");

builder.InsertCell();
builder.CellFormat.Width = 47;
builder.Write("备注");
builder.EndRow();

builder.RowFormat.HeadingFormat = false;

//添加数据
DataTable dt_temp = new DataTable();

for (int i = 1; i <= 7; i++)
{
    dt_temp.Columns.Add($"Columns{i}");
}

for (int i = 0; i < 30; i++)
{
    DataRow dr = dt_temp.NewRow();
    dr["Columns1"] = i;
    dr["Columns2"] = "文号";
    dr["Columns3"] = "责任者";
    dr["Columns4"] = "题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名";
    dr["Columns5"] = "20221113";
    dr["Columns6"] = "132";
    dr["Columns7"] = "666";
    dt_temp.Rows.Add(dr);
}


for (int i = 0; i < dt_temp.Rows.Count; i++)
{

    for (int j = 0; j < 7; j++)
    {
        //添加一个单元格
        builder.InsertCell();
        builder.Font.Bold = false;
        string clee = "";

        switch (j)
        {
            case 0:
                builder.CellFormat.Width = 30;
                clee = dt_temp.Rows[i][j].ToString();
                break;

            case 1:
                builder.CellFormat.Width = 45;
                clee = dt_temp.Rows[i][j].ToString();

                break;
            case 2:
                builder.CellFormat.Width = 44;
                clee = dt_temp.Rows[i][j].ToString();
                break;

            case 3:
                builder.CellFormat.Width = 150;
                clee = dt_temp.Rows[i][j].ToString();
                break;

            case 4:
                builder.CellFormat.Width = 64;
                clee = dt_temp.Rows[i][j].ToString();
                break;

            case 5:
                builder.CellFormat.Width = 40;
                clee = dt_temp.Rows[i][j].ToString();
                break;

            case 6:
                builder.CellFormat.Width = 47;
                clee = dt_temp.Rows[i][j].ToString();
                break;

        }

        builder.Write(clee);

    }

    builder.EndRow();
}


table.LeftIndent = 10;
table.AutoFit(AutoFitBehavior.FixedColumnWidths);
table.AllowAutoFit = false;

// Fill the remaining space on the page with empty rows.
Row emptyRow = (Row)table.LastRow.Clone(true);
emptyRow.GetChildNodes(NodeType.Paragraph, true).Clear();
emptyRow.EnsureMinimum();

// To detect where page ends we will use LayoutCollector.
LayoutCollector collector = new LayoutCollector(doc);
int lastRowPageIndex = collector.GetEndPageIndex(table.LastRow);
while (collector.GetEndPageIndex(table.LastRow) == lastRowPageIndex)
{
    table.AppendChild(emptyRow.Clone(true));
    // Reset collector.
    collector = new LayoutCollector(doc);
}
// Remove the last redundant row.
table.LastRow.Remove();

doc.Save(@"C:\Temp\out.docx");

谢谢您的回复。
1,我在使用LayoutCollector检测分页符,输出的结果并不完全是我想要的,我希望每行的高度是40,可以自定义每行高度,因为新添加的空行高度很小,和前面非空行,匹配不完美,我该如何自定义空行的高度。

2,word多出了一页空白页。

测试保存:word.docx (9.5 KB)

多出了空页:65.jpg (231.9 KB)

在此非常感谢您的帮助。

         //用空行填充页面上的剩余空间。
        Aspose.Words.Tables.Row emptyRow = (Aspose.Words.Tables.Row)table.LastRow.Clone(true);
        emptyRow.GetChildNodes(Aspose.Words.NodeType.Paragraph, true).Clear();
        emptyRow.EnsureMinimum();

        //为了检测页面的结束位置,我们将使用LayoutCollector。
        Aspose.Words.Layout.LayoutCollector collector = new Aspose.Words.Layout.LayoutCollector(doc);
        int lastRowPageIndex = collector.GetEndPageIndex(table.LastRow);
        while (collector.GetEndPageIndex(table.LastRow) == lastRowPageIndex)
        {
            table.AppendChild(emptyRow.Clone(true));
            //重置收集器。
            collector = new Aspose.Words.Layout.LayoutCollector(doc);
        }
        //删除最后一个冗余行。
        table.LastRow.Remove();

你好,我使用了emptyRow.RowFormat.Height = 40;可以实现空白行的高度,请问我使用的正确吗?

        //用空行填充页面上的剩余空间。
        Aspose.Words.Tables.Row emptyRow =(Aspose.Words.Tables.Row)table.LastRow.Clone(true);
        //获取子节点
        emptyRow.GetChildNodes(Aspose.Words.NodeType.Paragraph, true).Clear();
        emptyRow.RowFormat.Height = 40;
        //emptyRow.EnsureMinimum();

@fhn123456

  1. 是的,您正确使用了 RowFormat.Height。

  2. 表格不能是文档中的最后一个节点,所以表格后面总是有一个空段落。 因此,您可以像下面这样修改您的代码,以避免最后出现空白页面。

// To detect where page ends we will use LayoutCollector.
LayoutCollector collector = new LayoutCollector(doc);
int lastRowPageIndex = collector.GetEndPageIndex(table.NextSibling);
while (collector.GetEndPageIndex(table.NextSibling) == lastRowPageIndex)
{
    table.AppendChild(emptyRow.Clone(true));
    // Reset collector.
    collector = new LayoutCollector(doc);
}
// Remove the last redundant row.
table.LastRow.Remove();

谢谢您的回复,和帮助。

1,生成的word文件,当我打印它的时候,word文件和打印后的文件边距不一致,相差很大,我该如何实现word和打印保持一致的边距。

table边距.jpg (187.9 KB)

生成的word文件:output.docx (8.3 KB)

当我打印它:Print.pdf (40.0 KB)

使用以下代码可以调整word边距,但是我想知道为什么word和打印后的文件,边距相差很大的原因。

        //builder.PageSetup.TopMargin = 30;
        //builder.PageSetup.BottomMargin = 30;
        //builder.PageSetup.LeftMargin = 80;
        //builder.PageSetup.RightMargin = 60;

2,word表格,单元格设置的是固定宽度,当某一单元格文字过多,导致固定单元格的宽度发生了改变。
我该如何保持单元格的宽度不变,如果文字过多只改变行高。

@fhn123456

  1. 看起来您的打印机以 A4 (595.3x841.9pt) 页面大小打印,但 MS Word 默认使用 Letter (612x792pt) 页面大小。 这就是为什么你会看到差异。 您可以使用 PageSetup.PaperSize 属性指定页面大小

  2. 您可以禁用 Table.AllowAutoFit 选项来实现这一点。

首先谢谢您的回复和帮助。

如果您,无法定位到问题所在,我将整理我的所有代码,提供给您。

但我在正常使用中出现了致命bug,如下:
1,当我向固定每个单元格高度,table.AllowAutoFit = false;
效果图:0003-012-0739.docx (11.5 KB)

2,当我正常使用中,LayoutCollector检测分页符,并不能(完全)计算出需要填补的空行。
效果图:0003-012-07402.docx (11.3 KB)

多出来的空行:5666.jpg (45.8 KB)

//用空行填充页面上的剩余空间。
Aspose.Words.Tables.Row emptyRow = (Aspose.Words.Tables.Row)table.LastRow.Clone(true);
//获取子节点
emptyRow.GetChildNodes(Aspose.Words.NodeType.Paragraph, true).Clear();
emptyRow.RowFormat.Height = 50;

//为了检测页面的结束位置,我们将使用LayoutCollector。
Aspose.Words.Layout.LayoutCollector collector = new Aspose.Words.Layout.LayoutCollector(doc);

//下一个同级 不会多出一页空白页
int lastRowPageIndex = collector.GetEndPageIndex(table.NextSibling);

//获取结束页面索引 如果总共3页 
while (collector.GetEndPageIndex(table.NextSibling) == lastRowPageIndex)//不会出现一页空白页
{
    //克隆
    table.AppendChild(emptyRow.Clone(true));
    //重置收集器。
    collector = new Aspose.Words.Layout.LayoutCollector(doc);
    oo++;
}

@fhn123456

  1. 请在构建表格后尝试设置以下选项。
table.AutoFit(AutoFitBehavior.FixedColumnWidths);
table.AllowAutoFit = false;

  1. 不幸的是,我无法在我这边重现该问题。 以下代码在我这边产生了正确的输出:
Aspose.Words.Document doc = new Aspose.Words.Document();
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
builder.Font.Name = "SimSun";

Aspose.Words.Tables.Table table = builder.StartTable();

// 第一行  卷内目录
builder.InsertCell();
builder.RowFormat.HeadingFormat = true;
//文字对齐方式
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;
builder.CellFormat.Width = 420;
builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.None;
builder.Font.Size = 20;
builder.Font.Bold = true;
builder.Write("卷   内   目   录");
builder.EndRow();

// 第二行 如果设置第三行边框请在第二行设置
builder.InsertCell();
builder.RowFormat.HeadingFormat = true;
builder.CellFormat.Width = 420;
builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.None;
//builder.CellFormat.Borders.Left.LineStyle = Aspose.Words.LineStyle.None;
//builder.CellFormat.Borders.Right.LineStyle = Aspose.Words.LineStyle.None;
builder.CellFormat.Borders.Bottom.LineStyle = Aspose.Words.LineStyle.Single;
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;
builder.Font.Size = 13;
builder.Font.Bold = true;
builder.Write("0003-012-0721");
builder.EndRow();

// 第三行
builder.InsertCell();
builder.RowFormat.HeadingFormat = true;
//设置单元格内容对齐方式  垂直对齐
builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;
builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;
builder.CellFormat.Borders.Top.LineStyle = Aspose.Words.LineStyle.Single;

builder.CellFormat.Width = 30;
builder.Font.Size = 10.5;
builder.Font.Bold = true;
builder.Write("顺\n序\n号");

builder.InsertCell();
builder.CellFormat.Width = 45;
builder.Write("文号");

builder.InsertCell();
builder.CellFormat.Width = 44;
builder.Write("责任者");

builder.InsertCell();
builder.CellFormat.Width = 150;
builder.Write("题名");

builder.InsertCell();
builder.CellFormat.Width = 64;
builder.Write("日期");

builder.InsertCell();
builder.CellFormat.Width = 40;
builder.Write("页号");

builder.InsertCell();
builder.CellFormat.Width = 47;
builder.Write("备注");
builder.EndRow();

builder.RowFormat.HeadingFormat = false;

//添加数据
DataTable dt_temp = new DataTable();

for (int i = 1; i <= 7; i++)
{
    dt_temp.Columns.Add($"Columns{i}");
}

for (int i = 0; i < 30; i++)
{
    DataRow dr = dt_temp.NewRow();
    dr["Columns1"] = i;
    dr["Columns2"] = "文号";
    dr["Columns3"] = "责任者";
    dr["Columns4"] = "题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名题名";
    dr["Columns5"] = "20221113";
    dr["Columns6"] = "132";
    dr["Columns7"] = "666";
    dt_temp.Rows.Add(dr);
}


for (int i = 0; i < dt_temp.Rows.Count; i++)
{

    for (int j = 0; j < 7; j++)
    {
        //添加一个单元格
        builder.InsertCell();
        builder.Font.Bold = false;
        string clee = "";

        switch (j)
        {
            case 0:
                builder.CellFormat.Width = 30;
                clee = dt_temp.Rows[i][j].ToString();
                break;

            case 1:
                builder.CellFormat.Width = 45;
                clee = dt_temp.Rows[i][j].ToString();

                break;
            case 2:
                builder.CellFormat.Width = 44;
                clee = dt_temp.Rows[i][j].ToString();
                break;

            case 3:
                builder.CellFormat.Width = 150;
                clee = dt_temp.Rows[i][j].ToString();
                break;

            case 4:
                builder.CellFormat.Width = 64;
                clee = dt_temp.Rows[i][j].ToString();
                break;

            case 5:
                builder.CellFormat.Width = 40;
                clee = dt_temp.Rows[i][j].ToString();
                break;

            case 6:
                builder.CellFormat.Width = 47;
                clee = dt_temp.Rows[i][j].ToString();
                break;

        }

        builder.Write(clee);

    }

    builder.EndRow();
}


table.LeftIndent = 10;
table.AutoFit(AutoFitBehavior.FixedColumnWidths);
table.AllowAutoFit = false;

// Fill the remaining space on the page with empty rows.
Row emptyRow = (Row)table.LastRow.Clone(true);
emptyRow.GetChildNodes(NodeType.Paragraph, true).Clear();
emptyRow.EnsureMinimum();
emptyRow.RowFormat.Height = 40;
emptyRow.RowFormat.HeightRule = HeightRule.AtLeast;

// To detect where page ends we will use LayoutCollector.
LayoutCollector collector = new LayoutCollector(doc);
int lastRowPageIndex = collector.GetEndPageIndex(table.NextSibling);
while (collector.GetEndPageIndex(table.NextSibling) == lastRowPageIndex)
{
    table.AppendChild(emptyRow.Clone(true));
    // Reset collector.
    collector = new LayoutCollector(doc);
}
// Remove the last redundant row.
table.LastRow.Remove();

doc.Save(@"C:\Temp\out.docx");

out.docx (9.9 KB)

感谢这些天您的热心帮助。

我将附上我的所有项目,您可以还原问题,下载下来可以直接启动它。

项目使用框架:winform+.net 4.72

AsposeToWordTableTest.7z (4.4 MB)

@fhn123456 如我所见,您使用的是旧的 18.11 版本的 Aspose.Words。 使用最新的 22.11 版本的 Aspose.Words 无法重现该问题。 所以我建议您更新到 NuGet 上可用的最新版本。

感谢您的回复以及帮助!

你好,我已经将Aspose.Words升级成为22.11.0.0版本,当我运行它,发现它更不兼容。

1,当我使用:
//自动调整 固定柱宽度
table.AutoFit(Aspose.Words.Tables.AutoFitBehavior.FixedColumnWidths);
table.AllowAutoFit = true;

输出文件:
0003-012-0739.docx (23.4 KB)
0003-012-0740.docx (23.0 KB)

2,当我使用:
//自动调整 固定柱宽度
table.AutoFit(Aspose.Words.Tables.AutoFitBehavior.FixedColumnWidths);
table.AllowAutoFit = false;
输出文件:
0003-012-0739.docx (23.4 KB)
0003-012-0740.docx (23.1 KB)

3,在使用中,我发现18.11更兼容,只是在填充空行时会出现计算错误。

又没其他api可以替换LayoutCollector,检测分页符。

@fhn123456 在构建表之后和添加空行之前添加以下行会在我这边为您的项目生成正确的输出:

builder.EndTable();

table.LeftIndent = 10;
table.AutoFit(Aspose.Words.Tables.AutoFitBehavior.FixedColumnWidths);
table.AllowAutoFit = false;

0003-012-0739.docx (11.6 KB)
0003-012-0740.docx (11.4 KB)

不,除了使用 LayoutCollector 之外,没有其他方法可以检测分页符。

好吧,那就先这样吧,谢谢每个工程师的帮助,非常感谢你们。

1 Like