在Word中创建表格时,位于页面底部的表格如果超出页面边幅大小会自动跨页布局到下一页,此时表格行数据会断行。为保证文档布局的合理性及美观性,可设置表格是否禁止跨页断行。您可以使用以下步骤:
1.选择您想要禁止跨页断行的表格。
2.在 Word 菜单栏中,找到 "布局" 或 "表格布局" 选项卡,这取决于您使用的 Word 版本。
3.在选项卡上,找到 "属性" 或 "表格属性",点击打开表格属性对话框。
4.在表格属性对话框中,切换到 "行" 选项卡。
5.在 "行" 选项卡中,找到 "允许跨页" 选项勾选框,并确保它未被选中。这将禁止表格在页面之间断行。
6.点击 "确定" 或 "应用" 保存所做的更改。
上述操作是在MS Word中的通用操作,下面将通过Spire.Doc for Java 来设置,分两种情况:
1.通过设置TableFormat.IsBreakAcrossPages属性选择是否跨页断行
2.保持表格内容在同一页面
方法1:设置属性禁止跨页断行
import com.spire.doc.*;
public class PreventPagebreak {
public static void main(String[]args){
//加载测试文档
Document doc= new Document("test.docx");
//获取表格
Table table = doc.getSections().get(0).getTables().get(0);
//设置表格是否跨页断行
table.getTableFormat().isBreakAcrossPages(false);
//保存文档
doc.saveToFile("result.docx",FileFormat.Docx_2013);
}
}
方法2:保持表格内容在同一页面
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
public class PreventPagebreak {
public static void main(String[]args){
//加载测试文档
Document doc= new Document("test.docx");
//获取表格
Table table = doc.getSections().get(0).getTables().get(0);
//遍历表格单元格
for (int i = 0;i< table.getRows().getCount();i++) {
TableRow rows = table.getRows().get(i);
for (int j = 0; j< rows.getCells().getCount(); j++){
for (int z= 0; z < rows.getCells().get(j).getParagraphs().getCount();z++){
Paragraph p = rows.getCells().get(j).getParagraphs().get(z);
p.getFormat().setKeepFollow(true);//设置表格内容在同一页显示
}
}
}
//保存文档
doc.saveToFile("result1.docx",FileFormat.Docx_2013);
}
}
关于Spire.XLS for Java对Word中表格禁止跨页断行的解决办法如上,希望能帮助你解决问题。更多问题可前往官网查询。