Search in sources :

Example 1 with HMerge

use of org.docx4j.wml.TcPrInner.HMerge in project docx4j-template by vindell.

the class Docx4J_例子2 method createNormalCell.

public void createNormalCell(ObjectFactory factory, Tr tr, String content, RPr rpr, JcEnumeration jcEnumeration, boolean hasBgColor, String backgroudColor, boolean isHMerger, String mergeVal) {
    Tc tableCell = factory.createTc();
    P p = factory.createP();
    setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
    Text t = factory.createText();
    t.setValue(content);
    R run = factory.createR();
    // 设置表格内容字体样式
    run.setRPr(rpr);
    TcPr tcPr = tableCell.getTcPr();
    if (tcPr == null) {
        tcPr = factory.createTcPr();
    }
    CTVerticalJc valign = factory.createCTVerticalJc();
    valign.setVal(STVerticalJc.CENTER);
    tcPr.setVAlign(valign);
    if (isHMerger) {
        HMerge merge = new HMerge();
        if (mergeVal != null) {
            merge.setVal(mergeVal);
        }
        tcPr.setHMerge(merge);
    } else {
        VMerge merge = new VMerge();
        if (mergeVal != null) {
            merge.setVal(mergeVal);
        }
        tcPr.setVMerge(merge);
    }
    run.getContent().add(t);
    p.getContent().add(run);
    tableCell.getContent().add(p);
    if (hasBgColor) {
        CTShd shd = tcPr.getShd();
        if (shd == null) {
            shd = factory.createCTShd();
        }
        shd.setColor("auto");
        shd.setFill(backgroudColor);
        tcPr.setShd(shd);
    }
    tableCell.setTcPr(tcPr);
    tr.getContent().add(tableCell);
}
Also used : P(org.docx4j.wml.P) R(org.docx4j.wml.R) TcPr(org.docx4j.wml.TcPr) HMerge(org.docx4j.wml.TcPrInner.HMerge) CTShd(org.docx4j.wml.CTShd) Text(org.docx4j.wml.Text) CTVerticalJc(org.docx4j.wml.CTVerticalJc) Tc(org.docx4j.wml.Tc) VMerge(org.docx4j.wml.TcPrInner.VMerge)

Example 2 with HMerge

use of org.docx4j.wml.TcPrInner.HMerge in project docx4j-template by vindell.

the class Docx4j_合并单元格_S4_Test method mergeCellsHorizontal.

/**
 * @Description: 跨列合并
 */
public void mergeCellsHorizontal(Tbl tbl, int row, int fromCell, int toCell) {
    if (row < 0 || fromCell < 0 || toCell < 0) {
        return;
    }
    List<Tr> trList = getTblAllTr(tbl);
    if (row > trList.size()) {
        return;
    }
    Tr tr = trList.get(row);
    List<Tc> tcList = getTrAllCell(tr);
    for (int cellIndex = fromCell, len = Math.min(tcList.size() - 1, toCell); cellIndex <= len; cellIndex++) {
        Tc tc = tcList.get(cellIndex);
        TcPr tcPr = getTcPr(tc);
        HMerge hMerge = tcPr.getHMerge();
        if (hMerge == null) {
            hMerge = new HMerge();
            tcPr.setHMerge(hMerge);
        }
        if (cellIndex == fromCell) {
            hMerge.setVal("restart");
        } else {
            hMerge.setVal("continue");
        }
    }
}
Also used : TcPr(org.docx4j.wml.TcPr) HMerge(org.docx4j.wml.TcPrInner.HMerge) Tr(org.docx4j.wml.Tr) Tc(org.docx4j.wml.Tc)

Example 3 with HMerge

use of org.docx4j.wml.TcPrInner.HMerge in project docx4j-template by vindell.

the class Docx4j_工具类_S3_Test method mergeCellsHorizontal.

/**
 * @Description: 跨列合并
 */
public void mergeCellsHorizontal(Tbl tbl, int row, int fromCell, int toCell) {
    if (row < 0 || fromCell < 0 || toCell < 0) {
        return;
    }
    List<Tr> trList = getTblAllTr(tbl);
    if (row > trList.size()) {
        return;
    }
    Tr tr = trList.get(row);
    List<Tc> tcList = getTrAllCell(tr);
    for (int cellIndex = fromCell, len = Math.min(tcList.size() - 1, toCell); cellIndex <= len; cellIndex++) {
        Tc tc = tcList.get(cellIndex);
        TcPr tcPr = getTcPr(tc);
        HMerge hMerge = tcPr.getHMerge();
        if (hMerge == null) {
            hMerge = new HMerge();
            tcPr.setHMerge(hMerge);
        }
        if (cellIndex == fromCell) {
            hMerge.setVal("restart");
        } else {
            hMerge.setVal("continue");
        }
    }
}
Also used : TcPr(org.docx4j.wml.TcPr) HMerge(org.docx4j.wml.TcPrInner.HMerge) Tr(org.docx4j.wml.Tr) Tc(org.docx4j.wml.Tc)

Aggregations

Tc (org.docx4j.wml.Tc)3 TcPr (org.docx4j.wml.TcPr)3 HMerge (org.docx4j.wml.TcPrInner.HMerge)3 Tr (org.docx4j.wml.Tr)2 CTShd (org.docx4j.wml.CTShd)1 CTVerticalJc (org.docx4j.wml.CTVerticalJc)1 P (org.docx4j.wml.P)1 R (org.docx4j.wml.R)1 VMerge (org.docx4j.wml.TcPrInner.VMerge)1 Text (org.docx4j.wml.Text)1