gpt4 book ai didi

java - 避免具有不同行数的动态表行流向 XWPFDocument(Apache POI) 中的不同页面

转载 作者:行者123 更新时间:2023-12-02 11:59:52 29 4
gpt4 key购买 nike

我已经创建了一个代码,可以使用 Apache POI API 动态创建 Word 文档。该文档预计有一些行数不同的表(列数是固定的)。目前我已将每个表格放置在不同的页面上。我需要知道或者有什么方法可以开始将表格放在另一个表格下面,如果发现分页符,则将该表格移到下一页,然后后续表格继续在下面?我尝试了以下方法(其中文档是 XWPFDocument),但没有成功

XWPFTable table1 = document.createTable();
XWPFTableRow tableRow1 = table1.createRow();
tableRow1.setCantSplitRow(true);

最佳答案

XWPFTableRow.setCantSplitRow仅控制表格行本身是否可以用分页符分隔。

但是你想要的是Keep text together 。所以我们需要使用CTPPrBase.addNewKeepNext。如果设置为ONtrue,则本段不应与下一段分开。另外,我们应该将 CTPPrBase.addNewKeepLines 设置为 ONtrue。这意味着:不要拆分段落本身。

示例:

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;

//import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; // up to Apache POI 4

import java.util.Random;

public class CreateWordTableKeepTogether {

public static void main(String[] args) throws Exception {

int numtables = 5;
int numrows;
Random random = new Random();
int numcols = 5;

XWPFDocument document = new XWPFDocument();

XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The tables:");

for (int t = 0; t < numtables; t++) {

numrows = random.nextInt(10) + 3;

paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("Table " + (t+1));

//don't split the paragraph itself
//paragraph.getCTP().addNewPPr().addNewKeepLines().setVal(STOnOff.ON); // up to Apache POI 4
paragraph.getCTP().addNewPPr().addNewKeepLines().setVal(true); // since Apache POI 5
//keep this paragraph together with the next paragraph
//paragraph.getCTP().getPPr().addNewKeepNext().setVal(STOnOff.ON); // up to Apache POI 4
paragraph.getCTP().getPPr().addNewKeepNext().setVal(true); // since Apache POI 5

XWPFTable table = document.createTable();

for (int r = 0; r < numrows; r++) {
XWPFTableRow row = table.getRow(r);
if (row == null) row = table.createRow();

//don't divide this row
row.setCantSplitRow(true);

for (int c = 0; c < numcols; c++) {
XWPFTableCell cell = row.getCell(c);
if (cell == null) cell = row.createCell();
paragraph = cell.getParagraphArray(0);
if (paragraph == null) paragraph = cell.addParagraph();
run = paragraph.createRun();
run.setText("T" + (t+1) + "R" + (r+1) + "C" + (c+1));

if (c == 0) { //only necessary for column one, since further columns in this row are protected by setCantSplitRow
//paragraph.getCTP().addNewPPr().addNewKeepLines().setVal(STOnOff.ON); // up to Apache POI 4
//paragraph.getCTP().getPPr().addNewKeepNext().setVal(STOnOff.ON); // up to Apache POI 4
paragraph.getCTP().addNewPPr().addNewKeepLines().setVal(true); // since Apache POI 5
paragraph.getCTP().getPPr().addNewKeepNext().setVal(true); // since Apache POI 5
}

}
}

//this is necessary, since it is the only paragraph which allows page breaks
paragraph = document.createParagraph();

}

FileOutputStream out = new FileOutputStream("./CreateWordTableKeepTogether.docx");
document.write(out);
out.close();
document.close();

}
}

关于java - 避免具有不同行数的动态表行流向 XWPFDocument(Apache POI) 中的不同页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47351266/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com