结果:
源代码:
public class admissionTicket {
public static void main(String[] args) throws Exception {
try {
createSimpleTable();
}
catch(Exception e) {
System.out.println("Error trying to create simple table.");
throw(e);
}
}
public static void createSimpleTable() throws Exception {
XWPFDocument doc = new XWPFDocument();
try {
XWPFTable table = doc.createTable(10, 4);
table.setTableAlignment(TableRowAlign.CENTER);
//table.getRow(1).getCell(1).setColor("BED4F1");
mergeHorizontal(table,0,0,3);
table.getRow(0).getCell(0).setText("National CET-6 written examination permit");
table.getRow(0).getCell(0).setColor("E3CF57");
mergeHorizontal(table,1,0,3);
table.getRow(1).getCell(0).setText("Examination time:2022/06/12");
table.getRow(1).getCell(0).setColor("FF9912");
table.getRow(2).getCell(0).setText("Admission number:");
table.getRow(2).getCell(0).setColor("FFE384");
table.getRow(2).getCell(1).setText("202005153266");
table.getRow(2).getCell(1).setColor("FFE384");
table.getRow(3).getCell(0).setText("name:");
table.getRow(3).getCell(0).setColor("FFE384");
table.getRow(3).getCell(1).setText("Haishan Zhang:");
table.getRow(3).getCell(1).setColor("FFE384");
table.getRow(4).getCell(0).setText("college:");
table.getRow(4).getCell(0).setColor("FFE384");
table.getRow(4).getCell(1).setText("school of computing");
table.getRow(4).getCell(1).setColor("FFE384");
table.getRow(5).getCell(0).setText("class:");
table.getRow(5).getCell(0).setColor("FFE384");
table.getRow(5).getCell(1).setText("20 computer class 2");
table.getRow(5).getCell(1).setColor("FFE384");
table.getRow(6).getCell(0).setText("major:");
table.getRow(6).getCell(0).setColor("FFE384");
table.getRow(6).getCell(1).setText("the major of computer");
table.getRow(6).getCell(1).setColor("FFE384");
table.getRow(7).getCell(0).setText("Certificate No:");
table.getRow(7).getCell(0).setColor("FFE384");
table.getRow(7).getCell(1).setText("50023820008563214");
table.getRow(7).getCell(1).setColor("FFE384");
table.getRow(8).getCell(0).setText("Test site:");
table.getRow(8).getCell(0).setColor("FFE384");
table.getRow(8).getCell(1).setText("Chongqing Normal University");
table.getRow(8).getCell(1).setColor("FFE384");
mergeHorizontal(table,9,0,3);
table.getRow(9).getCell(0).setText("Centre no:121"+" "+"Seat number:09"+" "+"Student number:202002022");
table.getRow(9).getCell(0).setColor("FF6100");
mergeVertically(table,2,2,8);
mergeVertically(table,3,2,8);
mergeHorizontal(table,2,2,3);
table.getRow(2).getCell(2).setText("personal picture");
table.getRow(2).getCell(2).addParagraph().createRun().addPicture(new FileInputStream("image/p.png"),XWPFDocument.PICTURE_TYPE_BMP,"p.png", Units.toEMU(50), Units.toEMU(50));
OutputStream out = new FileOutputStream("simpleTable.docx");
try {
doc.write(out);
} finally {
out.close();
}
} finally {
doc.close();
}
}
//合并列
public static void mergeVertically(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) {
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
//合并后垂直居中
table.getRow(fromRow).getCell(col).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
}
//合并行
public static void mergeHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
if ( cellIndex == fromCell ) {
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
} else {
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}
}