Java Code Examples for org.apache.poi.xwpf.usermodel.XWPFTableRow#getTableCells()
The following examples show how to use
org.apache.poi.xwpf.usermodel.XWPFTableRow#getTableCells() .
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: BookMarks.java From frpMgr with MIT License | 6 votes |
/** * 构造函数,用以分析文档,解析出所有的标签 * @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 2
Source File: ParseWord07.java From autopoi with Apache License 2.0 | 6 votes |
/** * 解析这个表格 * * @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 3
Source File: ParseWord07.java From jeasypoi with Apache License 2.0 | 6 votes |
/** * 解析这个表格 * * @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 4
Source File: RawCopier.java From M2Doc with Eclipse Public License 1.0 | 6 votes |
/** * 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 5
Source File: AbstractBodyParser.java From M2Doc with Eclipse Public License 1.0 | 6 votes |
/** * 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 6
Source File: ParseWord07.java From easypoi with Apache License 2.0 | 6 votes |
/** * 解析这个表格 * * @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 7
Source File: WordExport.java From frpMgr with MIT License | 5 votes |
public void replaceText(Map<String, String> bookmarkMap, String bookMarkName) { //首先得到标签 BookMark bookMark = bookMarks.getBookmark(bookMarkName); //获得书签标记的表格 XWPFTable table = bookMark.getContainerTable(); //获得所有的表 //Iterator<XWPFTable> it = document.getTablesIterator(); if (table != null) { //得到该表的所有行 int rcount = table.getNumberOfRows(); for (int i = 0; i < rcount; i++) { XWPFTableRow row = table.getRow(i); //获到改行的所有单元格 List<XWPFTableCell> cells = row.getTableCells(); for (XWPFTableCell c : cells) { for (Entry<String, String> e : bookmarkMap.entrySet()) { if (c.getText().equals(e.getKey())) { //删掉单元格内容 c.removeParagraph(0); //给单元格赋值 c.setText(e.getValue()); } } } } } }
Example 8
Source File: ExcelMapParse.java From autopoi with Apache License 2.0 | 5 votes |
/** * 解析参数行,获取参数列表 * * @Author JEECG * @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; }
Example 9
Source File: ExcelMapParse.java From jeasypoi with Apache License 2.0 | 5 votes |
/** * 解析参数行,获取参数列表 * * @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; }
Example 10
Source File: SingleWordXTableParser.java From sun-wordtable-read with Apache License 2.0 | 5 votes |
/** * 解析word中表格行 * @param row * @param realRowIndex */ private void parseRow(XWPFTableRow row, int realRowIndex) { List<XWPFTableCell> cells = row.getTableCells(); int numCells = cells.size(); int logicColumnIndex = 0; int logicRowIndex = realRowIndex; //逻辑行号与实际行号一样 for (int realColumnIndex = 0; realColumnIndex < numCells; realColumnIndex++) { XWPFTableCell cell = row.getCell(realColumnIndex); //skipColumn是否跳过多个单元格, 当列合并时候 int skipColumn = parseCell(cell, realRowIndex, realColumnIndex, logicRowIndex, logicColumnIndex); logicColumnIndex = logicColumnIndex + skipColumn + 1; } }
Example 11
Source File: ExcelMapParse.java From easypoi with Apache License 2.0 | 5 votes |
/** * 解析参数行,获取参数列表 * * @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; }
Example 12
Source File: NiceXWPFDocument.java From poi-tl with Apache License 2.0 | 5 votes |
private void readTables(List<XWPFTable> tables) { allTables.addAll(tables); for (XWPFTable table : tables) { List<XWPFTableRow> rows = table.getRows(); if (null == rows) continue;; for (XWPFTableRow row : rows) { List<XWPFTableCell> cells = row.getTableCells(); if (null == cells) continue; for (XWPFTableCell cell : cells) { initAllElement(cell); } } } }
Example 13
Source File: TemplateResolver.java From poi-tl with Apache License 2.0 | 5 votes |
@Override public List<MetaTemplate> resolveBodyElements(List<IBodyElement> bodyElements) { List<MetaTemplate> metaTemplates = new ArrayList<>(); if (null == bodyElements) return metaTemplates; // current iterable templates state Deque<BlockTemplate> stack = new LinkedList<BlockTemplate>(); for (IBodyElement element : bodyElements) { if (element == null) continue; if (element.getElementType() == BodyElementType.PARAGRAPH) { XWPFParagraph paragraph = (XWPFParagraph) element; RunningRunParagraph runningRun = new RunningRunParagraph(paragraph, templatePattern); List<XWPFRun> refactorRuns = runningRun.refactorRun(); if (null == refactorRuns) continue; Collections.reverse(refactorRuns); resolveXWPFRuns(refactorRuns, metaTemplates, stack); } else if (element.getElementType() == BodyElementType.TABLE) { XWPFTable table = (XWPFTable) element; List<XWPFTableRow> rows = table.getRows(); if (null == rows) continue; for (XWPFTableRow row : rows) { List<XWPFTableCell> cells = row.getTableCells(); if (null == cells) continue; cells.forEach(cell -> { List<MetaTemplate> visitBodyElements = resolveBodyElements(cell.getBodyElements()); if (stack.isEmpty()) { metaTemplates.addAll(visitBodyElements); } else { stack.peek().getTemplates().addAll(visitBodyElements); } }); } } } checkStack(stack); return metaTemplates; }
Example 14
Source File: WordExport.java From frpMgr with MIT License | 4 votes |
public void fillTableAtBookMark(String bookMarkName, List<Map<String, String>> content) { //rowNum来比较标签在表格的哪一行 int rowNum = 0; //首先得到标签 BookMark bookMark = bookMarks.getBookmark(bookMarkName); Map<String, String> columnMap = new HashMap<String, String>(); Map<String, Node> styleNode = new HashMap<String, Node>(); //标签是否处于表格内 if (bookMark.isInTable()) { //获得标签对应的Table对象和Row对象 XWPFTable table = bookMark.getContainerTable(); XWPFTableRow row = bookMark.getContainerTableRow(); // CTRow ctRow = row.getCtRow(); List<XWPFTableCell> rowCell = row.getTableCells(); for (int i = 0; i < rowCell.size(); i++) { columnMap.put(i + "", rowCell.get(i).getText().trim()); //System.out.println(rowCell.get(i).getParagraphs().get(0).createRun().getFontSize()); //System.out.println(rowCell.get(i).getParagraphs().get(0).getCTP()); //System.out.println(rowCell.get(i).getParagraphs().get(0).getStyle()); //获取该单元格段落的xml,得到根节点 Node node1 = rowCell.get(i).getParagraphs().get(0).getCTP().getDomNode(); //遍历根节点的所有子节点 for (int x = 0; x < node1.getChildNodes().getLength(); x++) { if (node1.getChildNodes().item(x).getNodeName().equals(BookMark.RUN_NODE_NAME)) { Node node2 = node1.getChildNodes().item(x); //遍历所有节点为"w:r"的所有自己点,找到节点名为"w:rPr"的节点 for (int y = 0; y < node2.getChildNodes().getLength(); y++) { if (node2.getChildNodes().item(y).getNodeName().endsWith(BookMark.STYLE_NODE_NAME)) { //将节点为"w:rPr"的节点(字体格式)存到HashMap中 styleNode.put(i + "", node2.getChildNodes().item(y)); } } } else { continue; } } } //循环对比,找到该行所处的位置,删除改行 for (int i = 0; i < table.getNumberOfRows(); i++) { if (table.getRow(i).equals(row)) { rowNum = i; break; } } table.removeRow(rowNum); for (int i = 0; i < content.size(); i++) { //创建新的一行,单元格数是表的第一行的单元格数, //后面添加数据时,要判断单元格数是否一致 XWPFTableRow tableRow = table.createRow(); CTTrPr trPr = tableRow.getCtRow().addNewTrPr(); CTHeight ht = trPr.addNewTrHeight(); ht.setVal(BigInteger.valueOf(360)); } //得到表格行数 int rcount = table.getNumberOfRows(); for (int i = rowNum; i < rcount; i++) { XWPFTableRow newRow = table.getRow(i); //判断newRow的单元格数是不是该书签所在行的单元格数 if (newRow.getTableCells().size() != rowCell.size()) { //计算newRow和书签所在行单元格数差的绝对值 //如果newRow的单元格数多于书签所在行的单元格数,不能通过此方法来处理,可以通过表格中文本的替换来完成 //如果newRow的单元格数少于书签所在行的单元格数,要将少的单元格补上 int sub = Math.abs(newRow.getTableCells().size() - rowCell.size()); //将缺少的单元格补上 for (int j = 0; j < sub; j++) { newRow.addNewTableCell(); } } List<XWPFTableCell> cells = newRow.getTableCells(); for (int j = 0; j < cells.size(); j++) { XWPFParagraph para = cells.get(j).getParagraphs().get(0); XWPFRun run = para.createRun(); if (content.get(i - rowNum).get(columnMap.get(j + "")) != null) { //改变单元格的值,标题栏不用改变单元格的值 run.setText(content.get(i - rowNum).get(columnMap.get(j + "")) + ""); //将单元格段落的字体格式设为原来单元格的字体格式 run.getCTR().getDomNode().insertBefore(styleNode.get(j + "").cloneNode(true), run.getCTR().getDomNode().getFirstChild()); } para.setAlignment(ParagraphAlignment.CENTER); } } } }
Example 15
Source File: SingleWordXTableParser.java From sun-wordtable-read with Apache License 2.0 | 4 votes |
/** * 解析Docx的表格,将表格相关数据映射到表格映射对象中, 用于后面的操作 * @return */ public WordTable parse() { List<XWPFTableRow> rows; List<XWPFTableCell> cells; rows = xwpfTable.getRows(); int realMaxRowCount = rows.size(); // table.setRealMaxRowCount(rows.size()); //计算最大列数 int realMaxColumnCount = 0; for (XWPFTableRow row : rows) { //获取行对应的单元格 cells = row.getTableCells(); int _columnCountOnRow = 0; for (XWPFTableCell cell : cells) { CTTcPr tt = cell.getCTTc().getTcPr(); if(tt.getGridSpan()!=null) { _columnCountOnRow += tt.getGridSpan().getVal().intValue(); } else { _columnCountOnRow += 1; } } if (_columnCountOnRow > realMaxColumnCount) { realMaxColumnCount = _columnCountOnRow; } } //table.setRealMaxColumnCount(columnCount); _tableMemoryMapping = new WordTableMemoryMapping(realMaxRowCount, realMaxColumnCount); _tableMemoryMapping.setVisitor(context.getVisitor()); for (int i = 0; i < realMaxRowCount; i++) { parseRow(rows.get(i), i); } //printTableMemoryMap(); // wordTableMap = new WordTableMap(); // wordTableMap.setTableMemoryMap(_tableMemoryMap); return context.transfer(_tableMemoryMapping); }
Example 16
Source File: WordDocxTableParser.java From SnowGraph with Apache License 2.0 | 4 votes |
public static TableInfo parseWordTable(XWPFTable table, String caption) { if (table == null) { return null; } TableInfo tableInfo = new TableInfo(); tableInfo.setTableCaption(caption); tableInfo.setRowSize(table.getNumberOfRows()); List<XWPFTableRow> rows = table.getRows(); int rowNum = 0; // 遍历所有行 for (XWPFTableRow row : rows) { TableRowInfo rowInfo = new TableRowInfo(); if (rowInfo != null) tableInfo.addSubDocumentElement(rowInfo);// 添加到父节点:table rowNum++; rowInfo.setRowNum(rowNum); List<XWPFTableCell> cells = row.getTableCells(); int colNum = 0; // 遍历所有列(单元格) for (XWPFTableCell cell : cells) { TableCellInfo cellInfo = null; int startColNum = colNum + 1; int columnSpan = getGridSpan(cell); colNum += columnSpan; // 判断是否跨行,以及是否是跨行单元格的第一行 if (isVMerge(cell) && !isVMergeRestart(cell)) { // 是跨行单元格的非首行位置,获取到这个合并单元格的对象实例 TableRowInfo prevRowInfo = rowInfo.getPreviousRow(); cellInfo = prevRowInfo.getCellAtColumn(startColNum); cellInfo.setEndRowNum(rowNum);// 更新该单元格的尾行值 } else { // 跨行首格以及其他所有情况。 cellInfo = new TableCellInfo(); cellInfo.setStartRowNum(rowNum); cellInfo.setEndRowNum(rowNum); cellInfo.setStartColNum(startColNum); cellInfo.setEndColNum(colNum); PlainTextInfo plainTextInfo = new PlainTextInfo(cell.getText().trim()); //plainTextInfo.setEnglishText(); cellInfo.addSubDocumentElement(plainTextInfo); } // 添加到父节点:row if (cellInfo != null) rowInfo.addSubDocumentElement(cellInfo); } // table的colsize未初始化时,要根据计算出的colnum,也就是最后一行的编号,来初始化。 if (tableInfo.getColumnSize() == -1) tableInfo.setColumnSize(colNum); } //第二级数据 tableInfo.setTableParts(WTTablePartParser.parseTableToParts(tableInfo)); return tableInfo; }