问题描述
我有一个 Java 应用程序并创建了一个 MS Word 文档,该文档包含两列部分。我需要在此列中插入很多表。 我不受 Apache POI 的限制,我需要任何 Java 解决方案,但在 Apache POI 中,我正在以这种方式创建两列文档:
XWPFDocument document = new XWPFDocument();
// https://stackoverflow.com/questions/41166883/how-to-add-continuous-section-break-to-xwpfdocument/41188851#41188851
// https://stackoverflow.com/questions/14578288/apache-poi-multiple-columns-in-a-word-docx-document
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("One column on top. ");
CTSectPr ctSectPr = paragraph.getCTP().addNewPPr().addNewSectPr();
CTColumns ctColumns = ctSectPr.addNewCols();
ctColumns.setNum(BigInteger.valueOf(1));
paragraph = document.createParagraph();
run=paragraph.createRun();
run.setText("тестируем перетекание текста между колонками,"+
"тестируем перетекание текста между колонками,"+ "тестируем перетекание текста между колонками,");
run.addBreak(BreakType.PAGE);
run.setText("2тестируем перетекание текста между колонками ");
ctSectPr = paragraph.getCTP().addNewPPr().addNewSectPr();
ctSectPr.addNewType().setVal(STSectionMark.CONTINUOUS);
ctColumns = ctSectPr.addNewCols();
ctColumns.setNum(BigInteger.valueOf(2));
ctColumns.setEqualWidth(STOnOff.OFF);
CTColumn ctColumn = ctColumns.addNewCol();
ctColumn.setW(BigInteger.valueOf(4500));
ctColumn.setSpace(BigInteger.valueOf(300));
ctColumn = ctColumns.addNewCol();
ctColumn.setW(BigInteger.valueOf(4500));
主题是在 XWPFRun 中插入很多小表(或其他方法)请告诉我如何将表插入到列中?现在这项工作由宏完成,我需要摆脱这一点,在 JVM 中完成所有工作。谢谢!
解决方法
您的代码仅使用一个 XWPFRun
分成两列。 XWPFRun
不能包含表。但是部分可以包含所有文档正文元素,而不仅仅是文本运行。这也包括 XWPFTable
。
节包含具有节设置的两个段落之间或从具有节设置的最后一个段落到正文结尾的所有正文元素。那里的部分设置向上有效。段落中的部分设置对该段落上方的正文元素有效。并且body中的section设置对body元素从body末尾向上到最后一段有section设置有效。
在两列部分的左右列中有表格的完整示例:
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.Borders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTColumns;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTColumn;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocGrid;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocGrid;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STSectionMark;
public class Word2ColumnPageWithTables {
public static void main(String[] args) throws Exception {
XWPFDocument document= new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("One column on top. Lorem ipsum dolor sit amet,consetetur sadipscing elitr,sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,sed diam voluptua.");
paragraph = document.createParagraph();
//paragraph with section settings for one column section above
paragraph = document.createParagraph();
CTSectPr ctSectPr = paragraph.getCTP().addNewPPr().addNewSectPr();
CTColumns ctColumns = ctSectPr.addNewCols();
ctColumns.setNum(BigInteger.valueOf(1));
//left column
paragraph = document.createParagraph();
run=paragraph.createRun();
run.setText("The left side");
paragraph = document.createParagraph();
run=paragraph.createRun();
run.setText("Lorem ipsum dolor sit amet,sed diam voluptua. ");
paragraph = document.createParagraph();
run=paragraph.createRun();
run.setText("Table on left column:");
XWPFTable table = document.createTable(1,2);
table.getRow(0).getCell(0).setText("Cell A1");
table.getRow(0).getCell(1).setText("Cell B1");
paragraph = document.createParagraph();
// right column
//paragraph with column break
paragraph = document.createParagraph();
run = paragraph.createRun();
run.addBreak(BreakType.COLUMN);
run=paragraph.createRun();
run.setText("The right side");
paragraph = document.createParagraph();
run=paragraph.createRun();
run.setText("At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,no sea takimata sanctus est Lorem ipsum dolor sit amet. ");
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("Table on right column:");
table = document.createTable(1,2);
table.getRow(0).getCell(0).setText("Cell A1");
table.getRow(0).getCell(1).setText("Cell B1");
paragraph = document.createParagraph();
//paragraph with section break continuous for two column section above
paragraph = document.createParagraph();
ctSectPr = paragraph.getCTP().addNewPPr().addNewSectPr();
ctSectPr.addNewType().setVal(STSectionMark.CONTINUOUS);
ctColumns = ctSectPr.addNewCols();
ctColumns.setNum(BigInteger.valueOf(2));
CTColumn ctColumn = ctColumns.addNewCol();
ctColumn.setSpace(BigInteger.valueOf(300));
ctColumn = ctColumns.addNewCol();
paragraph = document.createParagraph();
run=paragraph.createRun();
run.setText("One column on bottom");
paragraph = document.createParagraph();
run=paragraph.createRun();
run.setText("Lorem ipsum dolor sit amet,sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,no sea takimata sanctus est Lorem ipsum dolor sit amet.");
//section setting continuous for one column section above
CTDocument1 ctDocument = document.getDocument();
CTBody ctBody = ctDocument.getBody();
ctSectPr = ctBody.addNewSectPr();
ctSectPr.addNewType().setVal(STSectionMark.CONTINUOUS);
ctColumns = ctSectPr.addNewCols();
ctColumns.setNum(BigInteger.valueOf(1));
FileOutputStream out = new FileOutputStream("Word2ColumnPageWithTables.docx");
document.write(out);
out.close();
document.close();
}
}