java制作准考证

结果:

源代码:

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);
            }
        }
    }


}

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值