org.apache.poi.xwpf.usermodel.XWPFTableCell Java Examples

The following examples show how to use org.apache.poi.xwpf.usermodel.XWPFTableCell. 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: ExcelEntityParse.java    From autopoi with Apache License 2.0 7 votes vote down vote up
/**
 * 获取表头数据
 * 
 * @param table
 * @param index
 * @return
 */
private Map<String, Integer> getTitleMap(XWPFTable table, int index, int headRows) {
	if (index < headRows) {
		throw new WordExportException(WordExportEnum.EXCEL_NO_HEAD);
	}
	Map<String, Integer> map = new HashMap<String, Integer>();
	String text;
	for (int j = 0; j < headRows; j++) {
		List<XWPFTableCell> cells = table.getRow(index - j - 1).getTableCells();
		for (int i = 0; i < cells.size(); i++) {
			text = cells.get(i).getText();
			if (StringUtils.isEmpty(text)) {
				throw new WordExportException(WordExportEnum.EXCEL_HEAD_HAVA_NULL);
			}
			map.put(text, i);
		}
	}
	return map;
}
 
Example #2
Source File: ParseWord07.java    From easypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 解析这个表格
 * 
 * @Author JueYue
 * @date 2013-11-17
 * @param table
 * @param map
 */
private void parseThisTable(XWPFTable table, Map<String, Object> map) throws Exception {
    XWPFTableRow row;
    List<XWPFTableCell> cells;
    Object listobj;
    ExcelEntityParse excelEntityParse = new ExcelEntityParse();
    for (int i = 0; i < table.getNumberOfRows(); i++) {
        row = table.getRow(i);
        cells = row.getTableCells();
        if (cells.size() == 1) {
            listobj = checkThisTableIsNeedIterator(cells.get(0), map);
            if (listobj == null) {
                parseThisRow(cells, map);
            } else if (listobj instanceof ExcelListEntity) {
                table.removeRow(i);// 删除这一行
                excelEntityParse.parseNextRowAndAddRow(table, i, (ExcelListEntity) listobj);
            } else {
                table.removeRow(i);// 删除这一行
                ExcelMapParse.parseNextRowAndAddRow(table, i, (List) listobj);
            }
        } else {
            parseThisRow(cells, map);
        }
    }
}
 
Example #3
Source File: ExcelEntityParse.java    From jeasypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 获取表头数据
 * 
 * @param table
 * @param index
 * @return
 */
private Map<String, Integer> getTitleMap(XWPFTable table, int index, int headRows) {
	if (index < headRows) {
		throw new WordExportException(WordExportEnum.EXCEL_NO_HEAD);
	}
	Map<String, Integer> map = new HashMap<String, Integer>();
	String text;
	for (int j = 0; j < headRows; j++) {
		List<XWPFTableCell> cells = table.getRow(index - j - 1).getTableCells();
		for (int i = 0; i < cells.size(); i++) {
			text = cells.get(i).getText();
			if (StringUtils.isEmpty(text)) {
				throw new WordExportException(WordExportEnum.EXCEL_HEAD_HAVA_NULL);
			}
			map.put(text, i);
		}
	}
	return map;
}
 
Example #4
Source File: TableTools.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
/**
 * 合并列单元格
 * 
 * @param table
 *            表格对象
 * @param col
 *            列 从0开始
 * @param fromRow
 *            起始行
 * @param toRow
 *            结束行
 */
public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
    if (toRow <= fromRow) return;
    for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
        XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
        CTTcPr tcPr = getTcPr(cell);
        CTVMerge vMerge = tcPr.addNewVMerge();
        if (rowIndex == fromRow) {
            // The first merged cell is set with RESTART merge value
            vMerge.setVal(STMerge.RESTART);
        } else {
            // Cells which join (merge) the first one, are set with CONTINUE
            vMerge.setVal(STMerge.CONTINUE);
        }
    }
}
 
Example #5
Source File: MiniTableRenderPolicy.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
public static void renderCell(XWPFTableCell cell, CellRenderData cellData, TableStyle rowStyle) {
    TableStyle cellStyle = (null == cellData.getCellStyle() ? rowStyle : cellData.getCellStyle());
    if (null != cellStyle && null != cellStyle.getBackgroundColor()) {
        cell.setColor(cellStyle.getBackgroundColor());
    }

    TextRenderData renderData = cellData.getCellText();
    if (StringUtils.isBlank(renderData.getText())) return;

    CTTc ctTc = cell.getCTTc();
    CTP ctP = (ctTc.sizeOfPArray() == 0) ? ctTc.addNewP() : ctTc.getPArray(0);
    XWPFParagraph par = new XWPFParagraph(ctP, cell);
    StyleUtils.styleTableParagraph(par, cellStyle);

    TextRenderPolicy.Helper.renderTextRun(par.createRun(), renderData);
}
 
Example #6
Source File: ParseWord07.java    From autopoi with Apache License 2.0 6 votes vote down vote up
/**
 * 解析这个表格
 * 
 * @Author JEECG
 * @date 2013-11-17
 * @param table
 * @param map
 */
private void parseThisTable(XWPFTable table, Map<String, Object> map) throws Exception {
	XWPFTableRow row;
	List<XWPFTableCell> cells;
	Object listobj;
	ExcelEntityParse excelEntityParse = new ExcelEntityParse();
	for (int i = 0; i < table.getNumberOfRows(); i++) {
		row = table.getRow(i);
		cells = row.getTableCells();
		if (cells.size() == 1) {
			listobj = checkThisTableIsNeedIterator(cells.get(0), map);
			if (listobj == null) {
				parseThisRow(cells, map);
			} else if (listobj instanceof ExcelListEntity) {
				table.removeRow(i);// 删除这一行
				excelEntityParse.parseNextRowAndAddRow(table, i, (ExcelListEntity) listobj);
			} else {
				table.removeRow(i);// 删除这一行
				ExcelMapParse.parseNextRowAndAddRow(table, i, (List) listobj);
			}
		} else {
			parseThisRow(cells, map);
		}
	}
}
 
Example #7
Source File: ParseWord07.java    From jeasypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 解析这个表格
 * 
 * @Author JueYue
 * @date 2013-11-17
 * @param table
 * @param map
 */
private void parseThisTable(XWPFTable table, Map<String, Object> map) throws Exception {
	XWPFTableRow row;
	List<XWPFTableCell> cells;
	Object listobj;
	ExcelEntityParse excelEntityParse = new ExcelEntityParse();
	for (int i = 0; i < table.getNumberOfRows(); i++) {
		row = table.getRow(i);
		cells = row.getTableCells();
		if (cells.size() == 1) {
			listobj = checkThisTableIsNeedIterator(cells.get(0), map);
			if (listobj == null) {
				parseThisRow(cells, map);
			} else if (listobj instanceof ExcelListEntity) {
				table.removeRow(i);// 删除这一行
				excelEntityParse.parseNextRowAndAddRow(table, i, (ExcelListEntity) listobj);
			} else {
				table.removeRow(i);// 删除这一行
				ExcelMapParse.parseNextRowAndAddRow(table, i, (List) listobj);
			}
		} else {
			parseThisRow(cells, map);
		}
	}
}
 
Example #8
Source File: BookMarks.java    From frpMgr with MIT License 6 votes vote down vote up
/** 
 * 构造函数,用以分析文档,解析出所有的标签
 * @param document  Word OOXML document instance. 
 */
public BookMarks(XWPFDocument document) {

	//初始化标签缓存
	this._bookmarks = new HashMap<String, BookMark>();

	// 首先解析文档普通段落中的标签 
	this.procParaList(document.getParagraphs());

	//利用繁琐的方法,从所有的表格中得到得到标签,处理比较原始和简单
	List<XWPFTable> tableList = document.getTables();

	for (XWPFTable table : tableList) {
		//得到表格的列信息
		List<XWPFTableRow> rowList = table.getRows();
		for (XWPFTableRow row : rowList) {
			//得到行中的列信息
			List<XWPFTableCell> cellList = row.getTableCells();
			for (XWPFTableCell cell : cellList) {
				//逐个解析标签信息
				//this.procParaList(cell.getParagraphs(), row);
				this.procParaList(cell);
			}
		}
	}
}
 
Example #9
Source File: WordTableCellContentFormula.java    From sun-wordtable-read with Apache License 2.0 6 votes vote down vote up
@Override
public void load(Object cellObj) {
	this.setContentType(ContentTypeEnum.Formula);
	
	if(docType == WordDocType.DOCX) {
		XWPFTableCell cell = (XWPFTableCell) cellObj;
		
		String xml = cell.getCTTc().xmlText();
		String omml = this.extractOml(xml);

		String mml = MathmlUtils.convertOMML2MML(omml);
		String latex = MathmlUtils.convertMML2Latex(mml);
		
		WcFormula formulaContent = new WcFormula();
		formulaContent.setMml(mml);
		formulaContent.setLatex(latex);
		this.setData(formulaContent);
	} else if(docType == WordDocType.DOC) {
		
	}
	
}
 
Example #10
Source File: DynamicTableRenderPolicy.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
@Override
public void render(ElementTemplate eleTemplate, Object data, XWPFTemplate template) {
    RunTemplate runTemplate = (RunTemplate) eleTemplate;
    XWPFRun run = runTemplate.getRun();
    run.setText("", 0);
    try {
        if (!TableTools.isInsideTable(run)) {
            throw new IllegalStateException(
                    "The template tag " + runTemplate.getSource() + " must be inside a table");
        }
        XWPFTableCell cell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody();
        XWPFTable table = cell.getTableRow().getTable();
        render(table, data);
    } catch (Exception e) {
        throw new RenderException("dynamic table error:" + e.getMessage(), e);
    }
}
 
Example #11
Source File: WordTableCellContentOleObject.java    From sun-wordtable-read with Apache License 2.0 6 votes vote down vote up
@Override
public void load(Object cellObj) {
	this.setContentType(ContentTypeEnum.OleObject);
	
	if(docType == WordDocType.DOCX) {
		XWPFTableCell cell = (XWPFTableCell) cellObj;
		String xml = cell.getCTTc().xmlText();
		Document doc = this.buildDocument(xml);
		String embedId = extractOleObjectEmbedId(doc);

		WcOleObject oleObject = this.readOleObject(embedId, cell.getXWPFDocument());
		this.setData(oleObject);		
	} else if(docType == WordDocType.DOC) {
		
	}
}
 
Example #12
Source File: ExcelEntityParse.java    From easypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 获取表头数据
 * 
 * @param table
 * @param index
 * @return
 */
private Map<String, Integer> getTitleMap(XWPFTable table, int index, int headRows) {
    if (index < headRows) {
        throw new WordExportException(WordExportEnum.EXCEL_NO_HEAD);
    }
    Map<String, Integer> map = new HashMap<String, Integer>();
    String text;
    for (int j = 0; j < headRows; j++) {
        List<XWPFTableCell> cells = table.getRow(index - j - 1).getTableCells();
        for (int i = 0; i < cells.size(); i++) {
            text = cells.get(i).getText();
            if (StringUtils.isEmpty(text)) {
                throw new WordExportException(WordExportEnum.EXCEL_HEAD_HAVA_NULL);
            }
            map.put(text, i);
        }
    }
    return map;
}
 
Example #13
Source File: WordDocxTableParser.java    From SnowGraph with Apache License 2.0 6 votes vote down vote up
private static int getGridSpan(XWPFTableCell cell) {
    if (cell == null)
        return -1;
    CTTcPr tcPr = cell.getCTTc().getTcPr();

    if (tcPr == null)
        return 1;

    CTDecimalNumber number = tcPr.getGridSpan();

    if (number == null) {
        return 1;
    }
    else {
        return number.getVal().intValue();
    }
}
 
Example #14
Source File: AbstractBodyParser.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Gets the {@link CTSdtBlock} at the given sdtIndex in the given {@link IBody}.
 * 
 * @param body
 *            the {@link IBody}
 * @param sdtIndex
 *            the index in internal sdt list
 * @return the {@link CTSdtBlock} at the given sdtIndex in the given {@link IBody}
 */
private static CTSdtBlock getCTSdtBlock(IBody body, int sdtIndex) {
    final CTSdtBlock res;

    if (body instanceof XWPFDocument) {
        res = ((XWPFDocument) body).getDocument().getBody().getSdtArray(sdtIndex);
    } else if (body instanceof XWPFHeaderFooter) {
        res = ((XWPFHeaderFooter) body)._getHdrFtr().getSdtArray(sdtIndex);
    } else if (body instanceof XWPFFootnote) {
        res = ((XWPFFootnote) body).getCTFtnEdn().getSdtArray(sdtIndex);
    } else if (body instanceof XWPFTableCell) {
        res = ((XWPFTableCell) body).getCTTc().getSdtArray(sdtIndex);
    } else {
        throw new IllegalStateException("can't insert control in " + body.getClass().getCanonicalName());
    }

    return res;
}
 
Example #15
Source File: AbstractBodyParser.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parses a {@link Table}.
 * 
 * @param wtable
 *            the table to parse
 * @return the created object
 * @throws DocumentParserException
 *             if a problem occurs while parsing.
 */
protected Table parseTable(XWPFTable wtable) throws DocumentParserException {
    if (wtable == null) {
        throw new IllegalArgumentException("parseTable can't be called on a null argument.");
    }
    Table table = (Table) EcoreUtil.create(TemplatePackage.Literals.TABLE);
    table.setTable(wtable);
    for (XWPFTableRow tablerow : wtable.getRows()) {
        Row row = (Row) EcoreUtil.create(TemplatePackage.Literals.ROW);
        table.getRows().add(row);
        row.setTableRow(tablerow);
        for (XWPFTableCell tableCell : tablerow.getTableCells()) {
            Cell cell = (Cell) EcoreUtil.create(TemplatePackage.Literals.CELL);
            row.getCells().add(cell);
            cell.setTableCell(tableCell);
            AbstractBodyParser parser = getNewParser(tableCell);
            cell.setBody(parser.parseBlock(null, TokenType.EOF));
        }
    }
    return table;
}
 
Example #16
Source File: RawCopier.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Updates bookmarks for the given output {@link XWPFTable} according to its input {@link XWPFTable}.
 * 
 * @param bookmarkManager
 *            the {@link BookmarkManager}
 * @param outputTable
 *            the output {@link XWPFTable}
 * @param inputTable
 *            the input {@link XWPFTable}
 */
private void updateBookmarks(BookmarkManager bookmarkManager, final XWPFTable outputTable,
        final XWPFTable inputTable) {
    final List<XWPFTableRow> inputRows = inputTable.getRows();
    final List<XWPFTableRow> outputRows = outputTable.getRows();
    for (int rowIndex = 0; rowIndex < inputRows.size(); rowIndex++) {
        final XWPFTableRow inputRow = inputRows.get(rowIndex);
        final XWPFTableRow outputRow = outputRows.get(rowIndex);
        final List<XWPFTableCell> inputCells = inputRow.getTableCells();
        final List<XWPFTableCell> outputCells = outputRow.getTableCells();
        for (int cellIndex = 0; cellIndex < inputCells.size(); cellIndex++) {
            final XWPFTableCell inputCell = inputCells.get(cellIndex);
            final XWPFTableCell outputCell = outputCells.get(cellIndex);
            final List<IBodyElement> inputBodyElements = inputCell.getBodyElements();
            final List<IBodyElement> outputBodyElements = outputCell.getBodyElements();
            for (int bodyElementIndex = 0; bodyElementIndex < inputBodyElements.size(); bodyElementIndex++) {
                final IBodyElement inputBodyElement = inputBodyElements.get(bodyElementIndex);
                if (inputBodyElement instanceof XWPFParagraph) {
                    final IBodyElement outputBodyElement = outputBodyElements.get(bodyElementIndex);
                    updateBookmarks(bookmarkManager, ((XWPFParagraph) outputBodyElement).getCTP(),
                            ((XWPFParagraph) inputBodyElement).getCTP());
                }
            }
        }
    }
}
 
Example #17
Source File: M2DocEvaluator.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates a {@link XWPFParagraph} in the given {@link IBody}.
 * 
 * @param body
 *            the {@link IBody}
 * @return the created {@link XWPFParagraph}
 */
private XWPFParagraph createParagraph(IBody body) {
    final XWPFParagraph res;

    if (body instanceof XWPFTableCell) {
        XWPFTableCell cell = (XWPFTableCell) body;
        res = cell.addParagraph();
    } else if (body instanceof XWPFDocument) {
        res = ((XWPFDocument) body).createParagraph();
    } else if (body instanceof XWPFHeaderFooter) {
        res = ((XWPFHeaderFooter) body).createParagraph();
    } else {
        throw new UnsupportedOperationException("unkown IBody type :" + body.getClass());
    }

    return res;
}
 
Example #18
Source File: RawCopier.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Create New Paragraph.
 * 
 * @param document
 *            document
 * @return new paragraph
 */
private XWPFParagraph createNewParagraph(IBody document) {
    final XWPFParagraph res;

    if (document instanceof XWPFTableCell) {
        XWPFTableCell cell = (XWPFTableCell) document;
        res = cell.addParagraph();
    } else if (document instanceof XWPFDocument) {
        res = ((XWPFDocument) document).createParagraph();
    } else if (document instanceof XWPFHeaderFooter) {
        res = ((XWPFHeaderFooter) document).createParagraph();
    } else {
        throw new UnsupportedOperationException("unkown IBody type :" + document.getClass());
    }

    return res;
}
 
Example #19
Source File: RawCopier.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Copies the given {@link CTSdtBlock} in the given output {@link IBody}.
 * 
 * @param outputBody
 *            the output {@link IBody}
 * @param block
 *            the {@link CTSdtBlock} to insert
 */
private void copyCTSdtBlock(IBody outputBody, CTSdtBlock block) {
    final CTSdtBlock stdBlock;
    if (outputBody instanceof XWPFDocument) {
        stdBlock = ((XWPFDocument) outputBody).getDocument().getBody().addNewSdt();
    } else if (outputBody instanceof XWPFHeaderFooter) {
        stdBlock = ((XWPFHeaderFooter) outputBody)._getHdrFtr().addNewSdt();
    } else if (outputBody instanceof XWPFFootnote) {
        stdBlock = ((XWPFFootnote) outputBody).getCTFtnEdn().addNewSdt();
    } else if (outputBody instanceof XWPFTableCell) {
        stdBlock = ((XWPFTableCell) outputBody).getCTTc().addNewSdt();
    } else {
        throw new IllegalStateException("can't insert control in " + outputBody.getClass().getCanonicalName());
    }
    stdBlock.set(block.copy());
    new XWPFSDT(stdBlock, outputBody); // this do the insertion
}
 
Example #20
Source File: M2DocEvaluator.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public XWPFParagraph caseContentControl(ContentControl contentControl) {
    final CTSdtBlock sdtBlock;

    if (generatedDocument instanceof XWPFDocument) {
        sdtBlock = ((XWPFDocument) generatedDocument).getDocument().getBody().addNewSdt();
    } else if (generatedDocument instanceof XWPFHeaderFooter) {
        sdtBlock = ((XWPFHeaderFooter) generatedDocument)._getHdrFtr().addNewSdt();
    } else if (generatedDocument instanceof XWPFFootnote) {
        sdtBlock = ((XWPFFootnote) generatedDocument).getCTFtnEdn().addNewSdt();
    } else if (generatedDocument instanceof XWPFTableCell) {
        sdtBlock = ((XWPFTableCell) generatedDocument).getCTTc().addNewSdt();
    } else {
        throw new IllegalStateException(
                "can't insert control in " + generatedDocument.getClass().getCanonicalName());
    }

    sdtBlock.set(contentControl.getBlock().copy());
    new XWPFSDT(sdtBlock, generatedDocument); // this do the insertion

    return currentGeneratedParagraph;
}
 
Example #21
Source File: DocPrSupport.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public static void updateDocPrId(XWPFTable table) {
    List<XWPFTableRow> rows = table.getRows();
    rows.forEach(row -> {
        List<XWPFTableCell> cells = row.getTableCells();
        cells.forEach(cell -> {
            cell.getParagraphs().forEach(DocPrSupport::updateDocPrId);
            cell.getTables().forEach(DocPrSupport::updateDocPrId);
        });
    });
}
 
Example #22
Source File: MiniTableRenderPolicy.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public static void renderNoDataTable(XWPFRun run, MiniTableRenderData tableData) {
    int row = 2, col = tableData.getHeader().size();
    BodyContainer bodyContainer = BodyContainerFactory.getBodyContainer(run);
    XWPFTable table = bodyContainer.insertNewTable(run, row, col);
    TableTools.initBasicTable(table, col, tableData.getWidth(), tableData.getStyle());

    Helper.renderRow(table, 0, tableData.getHeader());

    TableTools.mergeCellsHorizonal(table, 1, 0, col - 1);
    XWPFTableCell cell = table.getRow(1).getCell(0);
    cell.setText(tableData.getNoDatadesc());
}
 
Example #23
Source File: WordDocxTableParser.java    From SnowGraph with Apache License 2.0 5 votes vote down vote up
private static boolean isVMerge(XWPFTableCell cell) {
    if (cell == null)
        return false;
    CTTcPr tcPr = cell.getCTTc().getTcPr();
    if (tcPr == null)
        return false;

    return tcPr.isSetVMerge();
}
 
Example #24
Source File: TemplateResolver.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
<T> RunTemplate parseTemplateFactory(String text, T obj) {
    logger.debug("Resolve where text: {}, and create ElementTemplate", text);
    if (templatePattern.matcher(text).matches()) {
        String tag = gramerPattern.matcher(text).replaceAll("").trim();
        if (obj.getClass() == XWPFRun.class) {
            return (RunTemplate) runTemplateFactory.createRunTemplate(tag, (XWPFRun) obj);
        } else if (obj.getClass() == XWPFTableCell.class)
            // return CellTemplate.create(symbol, tagName, (XWPFTableCell)
            // obj);
            return null;
    }
    return null;
}
 
Example #25
Source File: TableTools.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
/**
 * 合并行单元格
 * 
 * @param table
 *            表格对象
 * @param row
 *            行 从0开始
 * @param fromCol
 *            起始列
 * @param toCol
 *            结束列
 */
public static void mergeCellsHorizonal(XWPFTable table, int row, int fromCol, int toCol) {
    if (toCol <= fromCol) return;
    XWPFTableCell cell = table.getRow(row).getCell(fromCol);
    CTTcPr tcPr = getTcPr(cell);
    XWPFTableRow rowTable = table.getRow(row);
    for (int colIndex = fromCol + 1; colIndex <= toCol; colIndex++) {
        rowTable.getCtRow().removeTc(fromCol + 1);
        rowTable.removeCell(fromCol + 1);
    }

    tcPr.addNewGridSpan();
    tcPr.getGridSpan().setVal(BigInteger.valueOf((long) (toCol - fromCol + 1)));
}
 
Example #26
Source File: IfTemplateRenderTest.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("serial")
@Test
public void testIfFalse() throws Exception {
    Map<String, Object> datas = new HashMap<String, Object>() {
        {
            put("title", "poi-tl");
            put("isShowTitle", true);
            put("showUser", false);
            put("showDate", false);
        }
    };

    XWPFTemplate template = XWPFTemplate.compile("src/test/resources/template/iterable_if1.docx");
    template.render(datas);

    XWPFDocument document = XWPFTestSupport.readNewDocument(template);
    XWPFParagraph paragraph = document.getParagraphArray(0);
    assertEquals(paragraph.getText(), "Hi, poi-tl");

    XWPFTable table = document.getTableArray(0);
    XWPFTableCell cell = table.getRow(1).getCell(0);
    assertEquals(cell.getText(), "Hi, poi-tl");

    XWPFHeader header = document.getHeaderArray(0);
    paragraph = header.getParagraphArray(0);
    assertEquals(paragraph.getText(), "Hi, poi-tl");
}
 
Example #27
Source File: IfTemplateRenderTest.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("serial")
@Test
public void testIfTrue() throws Exception {
    Map<String, Object> datas = new HashMap<String, Object>() {
        {
            put("title", "poi-tl");
            put("isShowTitle", true);
            put("showUser", new HashMap<String, Object>() {
                {
                    put("user", "Sayi");
                    put("showDate", new HashMap<String, Object>() {
                        {
                            put("date", "2020-02-10");
                        }
                    });
                }
            });
        }
    };

    XWPFTemplate template = XWPFTemplate.compile("src/test/resources/template/iterable_if1.docx");
    template.render(datas);

    XWPFDocument document = XWPFTestSupport.readNewDocument(template);
    XWPFTable table = document.getTableArray(0);
    XWPFTableCell cell = table.getRow(1).getCell(0);
    XWPFHeader header = document.getHeaderArray(0);

    testParagraph(document);
    testParagraph(cell);
    testParagraph(header);
}
 
Example #28
Source File: ETLWordDocumentGenerator.java    From WhiteRabbit with Apache License 2.0 5 votes vote down vote up
private static void setTextAndHeaderShading(XWPFTableCell cell, String text) {
	cell.setText(text);
	
	cell.setColor("AAAAFF");
	// CTShd ctshd = cell.getCTTc().addNewTcPr().addNewShd();
	// ctshd.setColor("FFFFFF");
	// ctshd.setVal(STShd.CLEAR);
	// ctshd.setFill("6666BB");
	
}
 
Example #29
Source File: ETLWordDocumentGenerator.java    From WhiteRabbit with Apache License 2.0 5 votes vote down vote up
private static void createCellParagraph(XWPFTableCell cell, String text) {
	if (text.equals("")) {
		return;
	}
	cell.removeParagraph(0);
	for(String line: text.split("\n")) {
		addToParagraph(cell.addParagraph(), line);			
	}
}
 
Example #30
Source File: ExcelMapParse.java    From easypoi with Apache License 2.0 5 votes vote down vote up
/**
 * 解析参数行,获取参数列表
 * 
 * @Author JueYue
 * @date 2013-11-18
 * @param currentRow
 * @return
 */
private static String[] parseCurrentRowGetParams(XWPFTableRow currentRow) {
    List<XWPFTableCell> cells = currentRow.getTableCells();
    String[] params = new String[cells.size()];
    String text;
    for (int i = 0; i < cells.size(); i++) {
        text = cells.get(i).getText();
        params[i] = text == null ? "" : text.trim().replace("{{", "").replace("}}", "");
    }
    return params;
}