Java Code Examples for org.docx4j.wml.TcPr#getGridSpan()

The following examples show how to use org.docx4j.wml.TcPr#getGridSpan() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public Tc getTcByPosition(List<Tc> tcList, int position) {  
    int k = 0;  
    for (int i = 0, len = tcList.size(); i < len; i++) {  
        Tc tc = tcList.get(i);  
        TcPr trPr = tc.getTcPr();  
        if (trPr != null) {  
            GridSpan gridSpan = trPr.getGridSpan();  
            if (gridSpan != null) {  
                k += gridSpan.getVal().intValue() - 1;  
            }
        }  
        if (k >= position) {  
            return tcList.get(i);  
        }  
        k++;  
    }  
    if (position < tcList.size()) {  
        return tcList.get(position);  
    }  
    return null;  
}
 
Example 2
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 跨列合并
 */
public static void mergeCellsHorizontalByGridSpan(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 = Math.min(tcList.size() - 1, toCell); cellIndex >= fromCell; cellIndex--) {
        Tc tc = tcList.get(cellIndex);
        TcPr tcPr = getTcPr(tc);
        if (cellIndex == fromCell) {
            GridSpan gridSpan = tcPr.getGridSpan();
            if (gridSpan == null) {
                gridSpan = new GridSpan();
                tcPr.setGridSpan(gridSpan);
            }
            gridSpan.setVal(BigInteger.valueOf(Math.min(tcList.size() - 1, toCell) - fromCell + 1));
        } else {
            tr.getContent().remove(cellIndex);
        }
    }
}
 
Example 3
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 得到行的列数
 */
public static int getTcCellSizeWithMergeNum(Tr tr) {
    int cellSize = 1;
    List<Tc> tcList = getTrAllCell(tr);
    if (tcList == null || tcList.size() == 0) {
        return cellSize;
    }
    cellSize = tcList.size();
    for (Tc tc : tcList) {
        TcPr tcPr = getTcPr(tc);
        GridSpan gridSpan = tcPr.getGridSpan();
        if (gridSpan != null) {
            cellSize += gridSpan.getVal().intValue() - 1;
        }
    }
    return cellSize;
}
 
Example 4
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 得到行的列数
 */
public int getTcCellSizeWithMergeNum(Tr tr) {
    int cellSize = 1;
    List<Tc> tcList = getTrAllCell(tr);
    if (tcList == null || tcList.size() == 0) {
        return cellSize;
    }
    cellSize = tcList.size();
    for (Tc tc : tcList) {
        TcPr tcPr = getTcPr(tc);
        GridSpan gridSpan = tcPr.getGridSpan();
        if (gridSpan != null) {
            cellSize += gridSpan.getVal().intValue() - 1;
        }
    }
    return cellSize;
}
 
Example 5
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 *   实现思路:
     主要分在当前行上方插入行和在当前行下方插入行。对首尾2行特殊处理,在有跨行合并情况时,在第一行上面或者在最后一行下面插入是不会跨行的但是可能会跨列。
    对于中间的行,主要参照当前行,如果当前行跨行,则新增行也跨行,如果当前行单元格结束跨行,则新增的上方插入行跨行,下方插入行不跨行,如果当前行单元格开始跨行,则新增的上方插入行不跨行,下发插入行跨行。
    主要思路就是这样,插入的时候需要得到真实位置的单元格,代码如下:
 */
// 按位置得到单元格(考虑跨列合并情况)  
public Tc getTcByPosition(List<Tc> tcList, int position) {  
    int k = 0;  
    for (int i = 0, len = tcList.size(); i < len; i++) {  
        Tc tc = tcList.get(i);  
        TcPr trPr = tc.getTcPr();  
        if (trPr != null) {  
            GridSpan gridSpan = trPr.getGridSpan();  
            if (gridSpan != null) {  
                k += gridSpan.getVal().intValue() - 1;  
            }  
        }  
        if (k >= position) {  
            return tcList.get(i);  
        }  
        k++;  
    }  
    if (position < tcList.size()) {  
        return tcList.get(position);  
    }  
    return null;  
}
 
Example 6
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @Description: 跨列合并
 */
public void mergeCellsHorizontalByGridSpan(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 = Math.min(tcList.size() - 1, toCell); cellIndex >= fromCell; cellIndex--) {
        Tc tc = tcList.get(cellIndex);
        TcPr tcPr = getTcPr(tc);
        if (cellIndex == fromCell) {
            GridSpan gridSpan = tcPr.getGridSpan();
            if (gridSpan == null) {
                gridSpan = new GridSpan();
                tcPr.setGridSpan(gridSpan);
            }
            gridSpan.setVal(BigInteger.valueOf(Math.min(tcList.size() - 1,
                    toCell) - fromCell + 1));
        } else {
            tr.getContent().remove(cellIndex);
        }
    }
}
 
Example 7
Source File: Docx4j_合并单元格_S4_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void mergeCellsHorizontalByGridSpan(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 = Math.min(tcList.size() - 1, toCell); cellIndex >= fromCell; cellIndex--) {  
        Tc tc = tcList.get(cellIndex);  
        TcPr tcPr = getTcPr(tc);  
        if (cellIndex == fromCell) {  
            GridSpan gridSpan = tcPr.getGridSpan();  
            if (gridSpan == null) {  
                gridSpan = new GridSpan();  
                tcPr.setGridSpan(gridSpan);  
            }  
            gridSpan.setVal(BigInteger.valueOf(Math.min(tcList.size() - 1,  
                    toCell) - fromCell + 1));  
        } else {  
            tr.getContent().remove(cellIndex);  
        }  
    }  
}