/** * @param pos - index of the row * @return the row at the position specified or null if no rows is defined or if the position is greather than the max size of rows array */ public XWPFTableRow getRow(int pos) { if (pos >= 0 && pos < ctTbl.sizeOfTrArray()) { //return new XWPFTableRow(ctTbl.getTrArray(pos)); return getRows().get(pos); } return null; }
/** * Return true if a table's header row should be repeated at the top of a * table split across pages. NOTE - Word will not repeat a table row unless * all preceding rows of the table are also repeated. This function returns * false if the row will not be repeated even if the repeat tag is present * for this row. * * @return true if table's header row should be repeated at the top of each * page of table, false otherwise. */ public boolean isRepeatHeader() { boolean repeat = false; for (XWPFTableRow row : table.getRows()) { repeat = row.getRepeat(); if (row == this || !repeat) { break; } } return repeat; }
private void appendTable(XWPFTable table, StringBuilder text) { //this works recursively to pull embedded tables from within cells for (XWPFTableRow row : table.getRows()) { List<ICell> cells = row.getTableICells(); for (int i = 0; i < cells.size(); i++) { ICell cell = cells.get(i); if (cell instanceof XWPFTableCell) { text.append(((XWPFTableCell) cell).getTextRecursively()); } else if (cell instanceof XWPFSDTCell) { text.append(((XWPFSDTCell) cell).getContent().getText()); } if (i < cells.size() - 1) { text.append("\t"); } } text.append('\n'); } }
private void appendTableText(StringBuilder text, XWPFTable table) { //this works recursively to pull embedded tables from tables for (XWPFTableRow row : table.getRows()) { List<ICell> cells = row.getTableICells(); for (int i = 0; i < cells.size(); i++) { ICell cell = cells.get(i); if (cell instanceof XWPFTableCell) { text.append(((XWPFTableCell) cell).getTextRecursively()); } else if (cell instanceof XWPFSDTCell) { text.append(((XWPFSDTCell) cell).getContent().getText()); } if (i < cells.size() - 1) { text.append("\t"); } } text.append('\n'); } }
private void appendBodyElementText(StringBuilder text, IBodyElement e, boolean isLast) { if (e instanceof XWPFParagraph) { text.append(((XWPFParagraph) e).getText()); if (!isLast) { text.append('\t'); } } else if (e instanceof XWPFTable) { XWPFTable eTable = (XWPFTable) e; for (XWPFTableRow row : eTable.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { List<IBodyElement> localBodyElements = cell.getBodyElements(); for (int i = 0; i < localBodyElements.size(); i++) { boolean localIsLast = (i == localBodyElements.size() - 1); appendBodyElementText(text, localBodyElements.get(i), localIsLast); } } } if (!isLast) { text.append('\n'); } } else if (e instanceof XWPFSDT) { text.append(((XWPFSDT) e).getContent().getText()); if (!isLast) { text.append('\t'); } } }
XWPFDocument doc = new XWPFDocument(OPCPackage.open("input.docx")); for (XWPFParagraph p : doc.getParagraphs()) { List<XWPFRun> runs = p.getRuns(); if (runs != null) { for (XWPFRun r : runs) { String text = r.getText(0); if (text != null && text.contains("needle")) { text = text.replace("needle", "haystack"); r.setText(text, 0); } } } } for (XWPFTable tbl : doc.getTables()) { for (XWPFTableRow row : tbl.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { for (XWPFParagraph p : cell.getParagraphs()) { for (XWPFRun r : p.getRuns()) { String text = r.getText(0); if (text.contains("needle")) { text = text.replace("needle", "haystack"); r.setText(text); } } } } } } doc.write(new FileOutputStream("output.docx"));
private void init() { List<XWPFTable> tables = this.getTables(); if (null == tables) return; else allTables.addAll(tables); List<XWPFTableRow> rows = null; List<XWPFTableCell> cells = null; List<XWPFTable> cellTables = null; for (XWPFTable table : tables) { rows = table.getRows(); if (null == rows) continue; for (XWPFTableRow row : rows) { cells = row.getTableCells(); if (null == cells) continue; for (XWPFTableCell cell : cells) { cellTables = cell.getTables(); if (null != cellTables) allTables.addAll(cellTables); } } } }
void visitTable(XWPFTable table) { if (null == table) return; List<XWPFTableRow> rows = table.getRows(); if (null == rows) return; for (XWPFTableRow row : rows) { List<XWPFTableCell> cells = row.getTableCells(); if (null == cells) continue; for (XWPFTableCell cell : cells) { visitParagraphs(cell.getParagraphs()); visitTables(cell.getTables()); } } }
private void extractTable(XWPFTable table, XWPFListManager listManager, XHTMLContentHandler xhtml) throws SAXException, XmlException, IOException { xhtml.startElement("table"); xhtml.startElement("tbody"); for (XWPFTableRow row : table.getRows()) { xhtml.startElement("tr"); for (ICell cell : row.getTableICells()) { xhtml.startElement("td"); if (cell instanceof XWPFTableCell) { extractIBodyText((XWPFTableCell) cell, listManager, xhtml); } else if (cell instanceof XWPFSDTCell) { xhtml.characters(((XWPFSDTCell) cell).getContent().getText()); } xhtml.endElement("td"); } xhtml.endElement("tr"); } xhtml.endElement("tbody"); xhtml.endElement("table"); }
/** * @param pos - index of the row * @return the row at the position specified or null if no rows is defined or if the position is greather than the max size of rows array */ public XWPFTableRow getRow(int pos) { if (pos >= 0 && pos < ctTbl.sizeOfTrArray()) { //return new XWPFTableRow(ctTbl.getTrArray(pos)); return getRows().get(pos); } return null; }
protected void visitTableBody( XWPFTable table, T tableContainer ) throws Exception { // Proces Row List<XWPFTableRow> rows = table.getRows(); for ( XWPFTableRow row : rows ) { visitTableRow( row, tableContainer ); } }
/** * @param pos - index of the row * @return the row at the position specified or null if no rows is defined or if the position is greather than the max size of rows array */ public XWPFTableRow getRow(int pos) { if (pos >= 0 && pos < ctTbl.sizeOfTrArray()) { //return new XWPFTableRow(ctTbl.getTrArray(pos)); return getRows().get(pos); } return null; }
private void replaceInTable(XWPFDocument document, String searchText, String replacement) { for (XWPFTable tbl : document.getTables()) { for (XWPFTableRow row : tbl.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { for (XWPFParagraph paragraph : cell.getParagraphs()) { replaceInParagraph(paragraph, searchText, replacement); } } } } }
@Override public void onTable(Object t) { XWPFTable table = ((XWPFTable) t); for (XWPFTableRow xwpfTableRow : table.getRows()) { for (XWPFTableCell xwpfTableCell : xwpfTableRow.getTableCells()) { for (XWPFParagraph xwpfParagraph : xwpfTableCell.getParagraphs()) { String text = getParagraphText(xwpfParagraph); runner.pushExpression(text, xwpfTableCell, xwpfParagraph); } } } }
@Override public void onTable(Object t) { XWPFTable table = ((XWPFTable) t); for (XWPFTableRow xwpfTableRow : table.getRows()) { for (XWPFTableCell xwpfTableCell : xwpfTableRow.getTableCells()) { for (XWPFParagraph xwpfParagraph : xwpfTableCell.getParagraphs()) { String text = getParagraphText(xwpfParagraph); runner.pushExpression(text, xwpfTableCell, xwpfParagraph); } } } }
private void copyTableRow(final XWPFTable posTbl, final int rowCounter) { XWPFTableRow rowToCopy = posTbl.getRow(1); CTRow row = posTbl.getCTTbl().insertNewTr(rowCounter); row.set(rowToCopy.getCtRow()); XWPFTableRow copyRow = new XWPFTableRow(row, posTbl); posTbl.getRows().add(rowCounter, copyRow); }
/** * 换行操作 * * @param cell 触发此操作的单元格 */ public void nextRow(XWPFTableCell cell) { if (nowRow == null) { firstRow = nowRow = cell.getTableRow(); rowIndex = firstRow.getTable().getRows().indexOf(firstRow); } else { nowRow = nowRow.getTable().insertNewTableRow(rowIndex++); nowRow.setHeight(firstRow.getHeight()); } }