Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Sep 23, 2022 1:18 am

问题 在这一行 dataRow.setHeightType(TableRowHeightType.Auto); 设置了Auto ,跨页断行 false就失效了

Code: Select all
    //将表格添加到文档中
        section.getTables().add(table);
        //设置表格是否跨页断行
        table.getTableFormat().isBreakAcrossPages(false);
        // 固定列宽
        table.autoFit(AutoFitBehaviorType.Fixed_Column_Widths);
        //设置表格宽度
        table.setPreferredWidth(new PreferredWidth(WidthType.Percentage, (short) 100));
        System.out.println(DateUtil.currentSeconds() + "---1");
        //添加数据到剩余行
        for (int r = 0; r < twoArray.length; r++) {
            TableRow dataRow = table.getRows().get(r);
            dataRow.setHeight(90);
            dataRow.setHeightType(TableRowHeightType.Auto);
            dataRow.getRowFormat().setBackColor(Color.white);
            for (int c = 0; c < twoArray[r].length; c++) {
                dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(twoArray[r][c]);
                range2.getCharacterFormat().setFontName("宋体");
                range2.getCharacterFormat().setFontSize(10.5f);
            }
        }
        System.out.println(DateUtil.currentSeconds() + "---2");

whgao116
 
Posts: 14
Joined: Mon Sep 19, 2022 2:42 am

Fri Sep 23, 2022 7:25 am

您好,

根据您在其他帖子下留下的完整代码,我测试了您的场景。没有重现到您的问题。我把我测试的代码和结果文档放在下面。

Code: Select all
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;

import java.awt.*;
import java.io.IOException;


    public class CreateTable {
        public static void main(String[] args) throws IOException {
            //创建Word文档
            Document document = new Document();
            //添加一个section
            Section section = document.addSection();

            //数据
            String[][] data =
                    {
                            new String[]{"姓名", "性别", "部门", "工号"},
                            new String[]{"Winny", "女", "综合", "0109"},
                            new String[]{"Lois", "女", "综合", "0111"},
                            new String[]{"Jois", "男", "技术", "0110"}
                    };
            // 添加文字
            Paragraph paragraph1 = section.addParagraph();
            paragraph1.appendText("单位:元");
            ParagraphStyle style1 = new ParagraphStyle(document);
            style1.setName("unitStyle");
            style1.getCharacterFormat().setBold(true);
            document.getStyles().add(style1);
            paragraph1.applyStyle("unitStyle");
            paragraph1.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);

            //添加表格
            Table table = section.addTable(true);
            //设置表格的行数和列数
            table.resetCells(data.length + 1, 4);
            //将表格添加到文档中
            section.getTables().add(table);
            //设置表格是否跨页断行
            table.getTableFormat().isBreakAcrossPages(false);
            // 固定列宽
            table.autoFit(AutoFitBehaviorType.Fixed_Column_Widths);
            //设置表格宽度
            table.setPreferredWidth(new PreferredWidth(WidthType.Percentage, (short) 100));

            //添加数据到剩余行
            for (int r = 0; r < data.length; r++) {
                TableRow dataRow = table.getRows().get(r);
                dataRow.setHeight(90);
                dataRow.setHeightType(TableRowHeightType.Auto);
                dataRow.getRowFormat().setBackColor(Color.white);
                for (int c = 0; c < data[r].length; c++) {
                    dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                    TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
                    range2.getCharacterFormat().setFontName("Arial");
                    range2.getCharacterFormat().setFontSize(10f);
                }
            }
            // 添加文字
            Paragraph paragraph2 = section.addParagraph();
            paragraph2.appendText("哈哈:               嗯嗯:");
            ParagraphStyle style2 = new ParagraphStyle(document);
            style2.setName("bottomStyle");
            style2.getCharacterFormat().setBold(true);
            document.getStyles().add(style2);
            paragraph1.applyStyle("bottomStyle");

            //保存文档
            document.saveToFile("output/CreateTable.docx", FileFormat.Docx_2013);

        }
    }


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 951
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.Doc

cron