问题描述
我有一个code,但它是行跨度表。你能帮我将其转换为合并单元格code?
私有静态无效mergeCellVertically(XWPFTable表,诠释山坳,诠释fromRow,诠释toRow){
对于(INT的rowIndex = fromRow; rowIndex位置< = toRow; rowIndex位置++){
XWPFTableCell细胞= table.getRow(rowIndex位置).getCell(COL);
如果(rowIndex位置== fromRow){
//第一个合并的单元格设置与重启合并价值
。cell.getCTTc()addNewTcPr()addNewVMerge()SETVAL(STMerge.RESTART)。
}其他{
//其中加入(合并)细胞中的第一个,都设置有CONTINUE
。cell.getCTTc()addNewTcPr()addNewVMerge()SETVAL(STMerge.CONTINUE)。
}
}
}
的原理是一样的只有 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge
而不是 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge
。
不过,我会与更仔细地在细胞TCPR
元素。您的实际code简单地创建新的 TCPR
元素每次。如果已经有在细胞中这些元素?
例如:
进口的java.io.File;
进口java.io.FileOutputStream中;进口java.math.BigInteger的;进口org.apache.poi.xwpf.usermodel.XWPFDocument;
进口org.apache.poi.xwpf.usermodel.XWPFTable;
进口org.apache.poi.xwpf.usermodel.XWPFTableCell;
进口org.apache.poi.xwpf.usermodel.XWPFParagraph;
进口org.apache.poi.xwpf.usermodel.XWPFRun;进口org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
进口org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
进口org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
进口org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;
进口org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge;
进口org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
进口org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;公共类CreateWordTableMerge { 静态无效mergeCellVertically(XWPFTable表,诠释山坳,诠释fromRow,诠释toRow){
对于(INT的rowIndex = fromRow; rowIndex位置< = toRow; rowIndex位置++){
CTVMerge vmerge = CTVMerge.Factory.newInstance();
如果(rowIndex位置== fromRow){
//第一个合并的单元格设置与重启合并价值
vmerge.setVal(STMerge.RESTART);
}其他{
//其中加入(合并)细胞中的第一个,都设置有CONTINUE
vmerge.setVal(STMerge.CONTINUE);
}
XWPFTableCell细胞= table.getRow(rowIndex位置).getCell(COL);
//尝试获得TCPR。每次不是简单地设置一个新的。
CTTcPr TCPR = cell.getCTTc()getTcPr()。
如果(TCPR!= NULL){
tcPr.setVMerge(vmerge);
}其他{
//只设置一个新的TCPR如果没有一个已
TCPR = CTTcPr.Factory.newInstance();
tcPr.setVMerge(vmerge);
。cell.getCTTc()setTcPr(TCPR);
}
}
} 静态无效mergeCellHorizontally(XWPFTable表,诠释行,诠释fromCol,诠释母育){
对于(INT colIndex = fromCol; colIndex< =母育; colIndex ++){
CTHMerge hmerge = CTHMerge.Factory.newInstance();
如果(colIndex == fromCol){
//第一个合并的单元格设置与重启合并价值
hmerge.setVal(STMerge.RESTART);
}其他{
//其中加入(合并)细胞中的第一个,都设置有CONTINUE
hmerge.setVal(STMerge.CONTINUE);
}
XWPFTableCell细胞= table.getRow(行).getCell(colIndex);
//尝试获得TCPR。每次不是简单地设置一个新的。
CTTcPr TCPR = cell.getCTTc()getTcPr()。
如果(TCPR!= NULL){
tcPr.setHMerge(hmerge);
}其他{
//只设置一个新的TCPR如果没有一个已
TCPR = CTTcPr.Factory.newInstance();
tcPr.setHMerge(hmerge);
。cell.getCTTc()setTcPr(TCPR);
}
}
} 公共静态无效的主要(字串[] args)抛出异常{ XWPFDocument文档=新XWPFDocument(); XWPFParagraph段= document.createParagraph();
XWPFRun运行= paragraph.createRun();
run.setText(表); //创建表
XWPFTable表= document.createTable(3,5); 对于(INT行= 0;排3;;排++){
对于(INT COL = 0;西小于5;西++){
table.getRow(行).getCell(COL).setText(行+行+,列+ COL);
}
} //创建并在所有行中的所有列设置列宽
//大多数例子不设置CTTblWidth但这种类型
//有必要在所有Office版本的工作
对于(INT COL = 0;西小于5;西++){
CTTblWidth tblWidth = CTTblWidth.Factory.newInstance();
tblWidth.setW(BigInteger.valueOf(1000));
tblWidth.setType(STTblWidth.DXA);
对于(INT行= 0;排3;;排++){
CTTcPr TCPR = table.getRow(行).getCell(COL).getCTTc()getTcPr();
如果(TCPR!= NULL){
tcPr.setTcW(tblWidth);
}其他{
TCPR = CTTcPr.Factory.newInstance();
tcPr.setTcW(tblWidth);
table.getRow(行).getCell(COL).getCTTc()setTcPr(TCPR)。
}
}
} 使用方法合并//
mergeCellVertically(表,0,0,1);
mergeCellHorizontally(表,1,2,3);
mergeCellHorizontally(表2,1,4); 段= document.createParagraph(); FileOutputStream中出=新的FileOutputStream(create_table.docx);
文件撰写(出); 的System.out.println(create_table.docx successully写);
}
}
I have a code, but it's for rowspan a table. Can you help me to convert this to colspan code?
private static void mergeCellVertically(XWPFTable table, int col, int fromRow, int toRow){
for(int rowIndex = fromRow; rowIndex <= toRow; rowIndex++){
XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
if(rowIndex == fromRow){
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
}else{
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}
The principle is the same only with org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge
instead of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge
.
But I would be more carefully with the TcPr
elements in the cells. Your actual code simply creates new TcPr
elements every time. What if there are already such elements in the cells?
Example:
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.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
public class CreateWordTableMerge {
static void mergeCellVertically(XWPFTable table, int col, int fromRow, int toRow) {
for(int rowIndex = fromRow; rowIndex <= toRow; rowIndex++){
CTVMerge vmerge = CTVMerge.Factory.newInstance();
if(rowIndex == fromRow){
// The first merged cell is set with RESTART merge value
vmerge.setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
vmerge.setVal(STMerge.CONTINUE);
}
XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
// Try getting the TcPr. Not simply setting an new one every time.
CTTcPr tcPr = cell.getCTTc().getTcPr();
if (tcPr != null) {
tcPr.setVMerge(vmerge);
} else {
// only set an new TcPr if there is not one already
tcPr = CTTcPr.Factory.newInstance();
tcPr.setVMerge(vmerge);
cell.getCTTc().setTcPr(tcPr);
}
}
}
static void mergeCellHorizontally(XWPFTable table, int row, int fromCol, int toCol) {
for(int colIndex = fromCol; colIndex <= toCol; colIndex++){
CTHMerge hmerge = CTHMerge.Factory.newInstance();
if(colIndex == fromCol){
// The first merged cell is set with RESTART merge value
hmerge.setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
hmerge.setVal(STMerge.CONTINUE);
}
XWPFTableCell cell = table.getRow(row).getCell(colIndex);
// Try getting the TcPr. Not simply setting an new one every time.
CTTcPr tcPr = cell.getCTTc().getTcPr();
if (tcPr != null) {
tcPr.setHMerge(hmerge);
} else {
// only set an new TcPr if there is not one already
tcPr = CTTcPr.Factory.newInstance();
tcPr.setHMerge(hmerge);
cell.getCTTc().setTcPr(tcPr);
}
}
}
public static void main(String[] args) throws Exception {
XWPFDocument document= new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The table:");
//create table
XWPFTable table = document.createTable(3,5);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 5; col++) {
table.getRow(row).getCell(col).setText("row " + row + ", col " + col);
}
}
//create and set column widths for all columns in all rows
//most examples don't set the type of the CTTblWidth but this
//is necessary for working in all office versions
for (int col = 0; col < 5; col++) {
CTTblWidth tblWidth = CTTblWidth.Factory.newInstance();
tblWidth.setW(BigInteger.valueOf(1000));
tblWidth.setType(STTblWidth.DXA);
for (int row = 0; row < 3; row++) {
CTTcPr tcPr = table.getRow(row).getCell(col).getCTTc().getTcPr();
if (tcPr != null) {
tcPr.setTcW(tblWidth);
} else {
tcPr = CTTcPr.Factory.newInstance();
tcPr.setTcW(tblWidth);
table.getRow(row).getCell(col).getCTTc().setTcPr(tcPr);
}
}
}
//using the merge methods
mergeCellVertically(table, 0, 0, 1);
mergeCellHorizontally(table, 1, 2, 3);
mergeCellHorizontally(table, 2, 1, 4);
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("create_table.docx");
document.write(out);
System.out.println("create_table.docx written successully");
}
}
这篇关于如何合并单元格的表格中使用Apache POI字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!