如何在 Java 的 POI 中使用 XWPFTable 合并单元格(或应用 colspan)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15984896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to merge cells (or apply colspan) using XWPFTable in POI in Java?
提问by Weddy
Creating a table in poi was quite easy but it has very limited tutorials and I cannot find one that can create a simple merged cell in a table in generating a docx file.
在 poi 中创建表格非常简单,但它的教程非常有限,我找不到可以在生成 docx 文件时在表格中创建简单合并单元格的教程。
回答by ARK
If you have created table, row inside a table and cell inside a row, you can add gridSpan to cell properties:
如果您创建了表格、表格内的行和行内的单元格,您可以将 gridSpan 添加到单元格属性:
if (cell.getCTTc().getTcPr() == null) cell.getCTTc().addNewTcPr();
if (cell.getCTTc().getTcPr().getGridSpan() == null) cell.getCTTc().getTcPr().addNewGridSpan();
cell.getCTTc().getTcPr().getGridSpan().setVal(2);
Note: cell is org.apache.poi.xwpf.usermodel.XWPFTableCell.
注意:单元格是 org.apache.poi.xwpf.usermodel.XWPFTableCell。
回答by Narayana Nagireddi
Creating a separate XWPFTable
for each table row will work and should be perfectly fine. All the tables are merged behind the scenes to one table in the final word document. You will need all of these jars, poi-3.9.jar
, poi-ooxml-3.9.jar
and poi-ooxml-schemas-3.9.jar
XWPFTable
为每个表格行创建一个单独的行将工作并且应该完全没问题。所有表格在幕后合并到最终 Word 文档中的一张表格中。您将需要所有这些罐子poi-3.9.jar
,poi-ooxml-3.9.jar
和poi-ooxml-schemas-3.9.jar
XWPFTable table1 = document.createTable(1,1); // This is your row 1
XWPFTable table2 = document.createTable(1,3); // This is your row 2
// Now it's time to span each column of table1 and table2 to a span of your choice
// lets say 6 is the total span required assuming there's some row with 6 columns.
spanCellsAcrossRow(table1, 0, 0, 6);
spanCellsAcrossRow(table2, 0, 0, 2);
spanCellsAcrossRow(table2, 0, 1, 2);
spanCellsAcrossRow(table2, 0, 2, 2);
private void spanCellsAcrossRow(XWPFTable table, int rowNum, int colNum, int span) {
XWPFTableCell cell = table.getRow(rowNum).getCell(colNum);
cell.getCTTc().getTcPr().addNewGridSpan();
cell.getCTTc().getTcPr().getGridSpan().setVal(BigInteger.valueOf((long)span));
}
回答by Jose1755
To merge horizontally/vertically you need to create 2 CTHMerge and use the setVal:
要水平/垂直合并,您需要创建 2 CTHMerge 并使用 setVal:
- one for the cells that you will remain (STMerge.RESTART);
- a second one for the merged cells (STMerge.CONTINUE);
- 一个用于您将保留的单元格(STMerge.RESTART);
- 合并单元格的第二个(STMerge.CONTINUE);
a) example for a horizontalmerge for 2x2 table (image with example):
a) 2x2 表的水平合并示例(带有示例的图像):
|___________|___________| --> |___________ ___________|
|___________|___________| --> |___________ ___________|
|_______|_______| --> |___________ ___________|
|_______|_______| --> |___________ ___________|
// First Row
CTHMerge hMerge = CTHMerge.Factory.newInstance();
vmerge.setVal(STMerge.RESTART);
table.getRow(0).getCell(0).getCTTc().getTcPr().setHMerge(hMerge);
table.getRow(1).getCell(0).getCTTc().getTcPr().setHMerge(hMerge);
// Secound Row cell will be merged/"deleted"
CTHMerge hMerge1 = CTHMerge.Factory.newInstance();
hMerge.setVal(STMerge.CONTINUE);
table.getRow(0).getCell(1).getCTTc().getTcPr().setHMerge(hMerge1);
table.getRow(1).getCell(1).getCTTc().getTcPr().setHMerge(hMerge1);
b) example of a verticalmerge (image with example)
b)垂直合并示例(带有示例的图像)
// First Row
CTVMerge vmerge = CTVMerge.Factory.newInstance();
vmerge.setVal(STMerge.RESTART);
table.getRow(0).getCell(0).getCTTc().getTcPr().setVMerge(vmerge);
table.getRow(0).getCell(1).getCTTc().getTcPr().setVMerge(vmerge);
// Secound Row cell will be merged
CTVMerge vmerge1 = CTVMerge.Factory.newInstance();
vmerge.setVal(STMerge.CONTINUE);
table.getRow(1).getCell(0).getCTTc().getTcPr().setVMerge(vmerge1);
table.getRow(1).getCell(1).getCTTc().getTcPr().setVMerge(vmerge1);