Example usage for org.apache.poi.xwpf.usermodel XWPFTable insertNewTableRow

List of usage examples for org.apache.poi.xwpf.usermodel XWPFTable insertNewTableRow

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel XWPFTable insertNewTableRow.

Prototype

public XWPFTableRow insertNewTableRow(int pos) 

Source Link

Document

inserts a new tablerow

Usage

From source file:cn.afterturn.easypoi.word.parse.excel.ExcelEntityParse.java

License:Apache License

private int createCells(int index, Object t, List<ExcelExportEntity> excelParams, XWPFTable table,
        short rowHeight) {
    try {//from  ww  w .  j  a v  a  2s. c o m
        ExcelExportEntity entity;
        XWPFTableRow row = table.insertNewTableRow(index);
        if (rowHeight != -1) {
            row.setHeight(rowHeight);
        }
        int maxHeight = 1, cellNum = 0;
        for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
            entity = excelParams.get(k);
            if (entity.getList() != null) {
                Collection<?> list = (Collection<?>) entity.getMethod().invoke(t, new Object[] {});
                int listC = 0;
                for (Object obj : list) {
                    createListCells(index + listC, cellNum, obj, entity.getList(), table, rowHeight);
                    listC++;
                }
                cellNum += entity.getList().size();
                if (list != null && list.size() > maxHeight) {
                    maxHeight = list.size();
                }
            } else {
                Object value = getCellValue(entity, t);
                if (entity.getType() == 1) {
                    setCellValue(row, value, cellNum++);
                }
            }
        }
        // ????
        cellNum = 0;
        for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
            entity = excelParams.get(k);
            if (entity.getList() != null) {
                cellNum += entity.getList().size();
            } else if (entity.isNeedMerge() && maxHeight > 1) {
                table.setCellMargins(index, index + maxHeight - 1, cellNum, cellNum);
                cellNum++;
            }
        }
        return maxHeight;
    } catch (Exception e) {
        LOGGER.error("excel cell export error ,data is :{}", ReflectionToStringBuilder.toString(t));
        throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e);
    }
}

From source file:cn.afterturn.easypoi.word.parse.excel.ExcelMapParse.java

License:Apache License

/**
 * ?,?/*from  ww w  .j av  a2s . c om*/
 * @param table
 * @param index
 * @param list
 */
public static void parseNextRowAndAddRow(XWPFTable table, int index, List<Object> list) throws Exception {
    XWPFTableRow currentRow = table.getRow(index);
    String[] params = parseCurrentRowGetParams(currentRow);
    String listname = params[0];
    boolean isCreate = !listname.contains(FOREACH_NOT_CREATE);
    listname = listname.replace(FOREACH_NOT_CREATE, EMPTY).replace(FOREACH_AND_SHIFT, EMPTY)
            .replace(FOREACH, EMPTY).replace(START_STR, EMPTY);
    String[] keys = listname.replaceAll("\\s{1,}", " ").trim().split(" ");
    params[0] = keys[1];
    //??-??
    List<XWPFTableCell> tempCellList = new ArrayList<XWPFTableCell>();
    tempCellList.addAll(table.getRow(index).getTableCells());
    int cellIndex = 0;
    Map<String, Object> tempMap = Maps.newHashMap();
    LOGGER.debug("start for each data list :{}", list.size());
    for (Object obj : list) {
        currentRow = isCreate ? table.insertNewTableRow(index++) : table.getRow(index++);
        tempMap.put("t", obj);
        for (cellIndex = 0; cellIndex < currentRow.getTableCells().size(); cellIndex++) {
            String val = eval(params[cellIndex], tempMap).toString();
            currentRow.getTableCells().get(cellIndex).setText("");
            PoiWordStyleUtil.copyCellAndSetValue(tempCellList.get(cellIndex),
                    currentRow.getTableCells().get(cellIndex), val);
        }

        for (; cellIndex < params.length; cellIndex++) {
            String val = eval(params[cellIndex], tempMap).toString();
            PoiWordStyleUtil.copyCellAndSetValue(tempCellList.get(cellIndex), currentRow.createCell(), val);
        }
    }
    table.removeRow(index);

}

From source file:cn.wantedonline.porobot.DataBaseEntity.java

License:Apache License

private static void genDBDictionary(List<DBTableBean> tableBeans) throws IOException {
    XWPFDocument doc = new XWPFDocument();
    String databaseName = "default_database";
    for (DBTableBean bean : tableBeans) {
        databaseName = bean.getDatabaseName();
        XWPFParagraph p0 = doc.createParagraph();
        p0.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun r0 = p0.createRun();// w  w  w.  j a v  a2s .  c  o  m
        r0.setBold(true);
        r0.setText(databaseName);
        r0.setFontSize(16);
        XWPFParagraph p1 = doc.createParagraph();
        p1.setAlignment(ParagraphAlignment.LEFT);
        XWPFRun r1 = p1.createRun();
        r1.setBold(true);
        r1.setText("Table:" + bean.getTableName());
        XWPFTable table = doc.createTable();
        int pos = 0;
        XWPFTableRow row1 = table.insertNewTableRow(pos++);
        XWPFTableCell cell1 = row1.createCell();
        cell1.setText("Field");
        cell1.setColor("FFE4C4");
        XWPFTableCell cell2 = row1.createCell();
        cell2.setText("Type");
        cell2.setColor("FFE4C4");
        XWPFTableCell cell3 = row1.createCell();
        cell3.setText("Comment");
        cell3.setColor("FFE4C4");
        for (int i = 0; i < bean.getColumnSize(); i++) {
            XWPFTableRow row = table.insertNewTableRow(pos++);
            XWPFTableCell cell11 = row.createCell();
            cell11.setText(bean.getColumnName(i));
            XWPFTableCell cell22 = row.createCell();
            cell22.setText(bean.getColumnType(i));
            XWPFTableCell cell33 = row.createCell();
            cell33.setText(bean.getColumnComment(i));
        }
        table.removeRow(pos);
        XWPFParagraph p2 = doc.createParagraph();
        XWPFRun r2 = p2.createRun();
        r2.addBreak();
    }

    FileOutputStream out = new FileOutputStream(databaseName + ".docx");
    doc.write(out);
    out.close();
    doc.close();

}

From source file:offishell.word.WordHeleper.java

License:MIT License

/**
 * <p>//from   w w w  .j  a v  a 2 s.  c  o  m
 * Helper method to clone {@link XWPFTable}.
 * </p>
 * 
 * @param in
 * @param out
 * @param converter
 */
public static void copy(XWPFTable in, XWPFTable out, UnaryOperator<String> converter) {
    // copy context
    CTTbl outCT = out.getCTTbl();

    CTTblPr outPR = outCT.getTblPr() != null ? outCT.getTblPr() : outCT.addNewTblPr();
    outPR.set(in.getCTTbl().getTblPr());

    // copy children
    for (XWPFTableRow inRow : in.getRows()) {
        copy(inRow, out.insertNewTableRow(out.getNumberOfRows()), converter);
    }
}