Java Code Examples for org.apache.poi.xwpf.usermodel.XWPFTable#createRow()
The following examples show how to use
org.apache.poi.xwpf.usermodel.XWPFTable#createRow() .
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: ExcelMapParse.java From autopoi with Apache License 2.0 | 6 votes |
/** * 解析下一行,并且生成更多的行 * * @Author JEECG * @date 2013-11-18 * @param table * @param listobj2 */ public static void parseNextRowAndAddRow(XWPFTable table, int index, List<Object> list) throws Exception { XWPFTableRow currentRow = table.getRow(index); String[] params = parseCurrentRowGetParams(currentRow); table.removeRow(index);// 移除这一行 int cellIndex = 0;// 创建完成对象一行好像多了一个cell for (Object obj : list) { currentRow = table.createRow(); for (cellIndex = 0; cellIndex < currentRow.getTableCells().size(); cellIndex++) { currentRow.getTableCells().get(cellIndex).setText(PoiPublicUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0).toString()); } for (; cellIndex < params.length; cellIndex++) { currentRow.createCell().setText(PoiPublicUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0).toString()); } } }
Example 2
Source File: ExcelEntityParse.java From autopoi with Apache License 2.0 | 6 votes |
/** * 创建List之后的各个Cells * * @param styles */ public void createListCells(int index, int cellNum, Object obj, List<ExcelExportEntity> excelParams, XWPFTable table) throws Exception { ExcelExportEntity entity; XWPFTableRow row; if (table.getRow(index) == null) { row = table.createRow(); row.setHeight(getRowHeight(excelParams)); } else { row = table.getRow(index); } for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { entity = excelParams.get(k); Object value = getCellValue(entity, obj); if (entity.getType() == 1) { setCellValue(row, value, cellNum++); } } }
Example 3
Source File: ExcelMapParse.java From jeasypoi with Apache License 2.0 | 6 votes |
/** * 解析下一行,并且生成更多的行 * * @Author JueYue * @date 2013-11-18 * @param table * @param listobj2 */ public static void parseNextRowAndAddRow(XWPFTable table, int index, List<Object> list) throws Exception { XWPFTableRow currentRow = table.getRow(index); String[] params = parseCurrentRowGetParams(currentRow); table.removeRow(index);// 移除这一行 int cellIndex = 0;// 创建完成对象一行好像多了一个cell for (Object obj : list) { currentRow = table.createRow(); for (cellIndex = 0; cellIndex < currentRow.getTableCells().size(); cellIndex++) { currentRow.getTableCells().get(cellIndex).setText(PoiPublicUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0).toString()); } for (; cellIndex < params.length; cellIndex++) { currentRow.createCell().setText(PoiPublicUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0).toString()); } } }
Example 4
Source File: ExcelEntityParse.java From jeasypoi with Apache License 2.0 | 6 votes |
/** * 创建List之后的各个Cells * * @param styles */ public void createListCells(int index, int cellNum, Object obj, List<ExcelExportEntity> excelParams, XWPFTable table) throws Exception { ExcelExportEntity entity; XWPFTableRow row; if (table.getRow(index) == null) { row = table.createRow(); row.setHeight(getRowHeight(excelParams)); } else { row = table.getRow(index); } for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { entity = excelParams.get(k); Object value = getCellValue(entity, obj); if (entity.getType() == 1) { setCellValue(row, value, cellNum++); } } }
Example 5
Source File: ExcelMapParse.java From easypoi with Apache License 2.0 | 6 votes |
/** * 解析下一行,并且生成更多的行 * * @Author JueYue * @date 2013-11-18 * @param table * @param listobj2 */ public static void parseNextRowAndAddRow(XWPFTable table, int index, List<Object> list) throws Exception { XWPFTableRow currentRow = table.getRow(index); String[] params = parseCurrentRowGetParams(currentRow); table.removeRow(index);// 移除这一行 int cellIndex = 0;// 创建完成对象一行好像多了一个cell for (Object obj : list) { currentRow = table.createRow(); for (cellIndex = 0; cellIndex < currentRow.getTableCells().size(); cellIndex++) { currentRow .getTableCells() .get(cellIndex) .setText( PoiPublicUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0) .toString()); } for (; cellIndex < params.length; cellIndex++) { currentRow.createCell().setText( PoiPublicUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0) .toString()); } } }
Example 6
Source File: ExcelEntityParse.java From easypoi with Apache License 2.0 | 6 votes |
/** * 创建List之后的各个Cells * * @param styles */ public void createListCells(int index, int cellNum, Object obj, List<ExcelExportEntity> excelParams, XWPFTable table) throws Exception { ExcelExportEntity entity; XWPFTableRow row; if (table.getRow(index) == null) { row = table.createRow(); row.setHeight(getRowHeight(excelParams)); } else { row = table.getRow(index); } for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { entity = excelParams.get(k); Object value = getCellValue(entity, obj); if (entity.getType() == 1) { setCellValue(row, value, cellNum++); } } }
Example 7
Source File: HeaderFooterBodyContainer.java From poi-tl with Apache License 2.0 | 6 votes |
@Override public XWPFTable insertNewTable(XWPFRun run, int row, int col) { XmlCursor cursor = ((XWPFParagraph) run.getParent()).getCTP().newCursor(); XWPFTable table = insertNewTbl(cursor); // hack for cursor.removeXmlContents(); in XWPFHeaderFooter List<XWPFTableRow> rows = table.getRows(); for (int i = 0; i < rows.size(); i++) { table.removeRow(i); } for (int i = 0; i < row; i++) { XWPFTableRow tabRow = (table.getRow(i) == null) ? table.createRow() : table.getRow(i); for (int k = 0; k < col; k++) { if (tabRow.getCell(k) == null) { tabRow.createCell(); } } } return table; }
Example 8
Source File: CellBodyContainer.java From poi-tl with Apache License 2.0 | 6 votes |
@Override public XWPFTable insertNewTable(XWPFRun run, int row, int col) { XmlCursor cursor = ((XWPFParagraph) run.getParent()).getCTP().newCursor(); XWPFTable table = insertNewTbl(cursor); // hack for cursor.removeXmlContents(); in XWPFTableCell List<XWPFTableRow> rows = table.getRows(); for (int i = 0; i < rows.size(); i++) { table.removeRow(i); } for (int i = 0; i < row; i++) { XWPFTableRow tabRow = (table.getRow(i) == null) ? table.createRow() : table.getRow(i); for (int k = 0; k < col; k++) { if (tabRow.getCell(k) == null) { tabRow.createCell(); } } } return table; }
Example 9
Source File: CellBodyContainnerTest.java From poi-tl with Apache License 2.0 | 6 votes |
@BeforeEach public void init() { XWPFTable xwpfTable = document.createTable(); XWPFTableRow createRow = xwpfTable.createRow(); cell = createRow.createCell(); container = new CellBodyContainer(cell); // cell.addParagraph()有bug,不会更新bodyElements // p-t-p-t-p-t XWPFParagraph insertpos = cell.getParagraphArray(0); container.insertNewParagraph(insertpos.getCTP().newCursor()); container.insertNewTable(insertpos.createRun(), 2, 2); paragraph = container.insertNewParagraph(insertpos.getCTP().newCursor()); table = container.insertNewTable(insertpos.createRun(), 2, 2); container.insertNewParagraph(insertpos.getCTP().newCursor()); container.insertNewTable(insertpos.createRun(), 2, 2); container.removeBodyElement(6); }
Example 10
Source File: ExcelEntityParse.java From autopoi with Apache License 2.0 | 5 votes |
private int createCells(int index, Object t, List<ExcelExportEntity> excelParams, XWPFTable table, short rowHeight) throws Exception { ExcelExportEntity entity; XWPFTableRow row = table.createRow(); 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); 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()) { table.setCellMargins(index, index + maxHeight - 1, cellNum, cellNum); cellNum++; } } return maxHeight; }
Example 11
Source File: ExcelEntityParse.java From jeasypoi with Apache License 2.0 | 5 votes |
private int createCells(int index, Object t, List<ExcelExportEntity> excelParams, XWPFTable table, short rowHeight) throws Exception { ExcelExportEntity entity; XWPFTableRow row = table.createRow(); 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); 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()) { table.setCellMargins(index, index + maxHeight - 1, cellNum, cellNum); cellNum++; } } return maxHeight; }
Example 12
Source File: ExcelEntityParse.java From easypoi with Apache License 2.0 | 5 votes |
private int createCells(int index, Object t, List<ExcelExportEntity> excelParams, XWPFTable table, short rowHeight) throws Exception { ExcelExportEntity entity; XWPFTableRow row = table.createRow(); 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); 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()) { table.setCellMargins(index, index + maxHeight - 1, cellNum, cellNum); cellNum++; } } return maxHeight; }
Example 13
Source File: DocumentBodyContainer.java From poi-tl with Apache License 2.0 | 5 votes |
@Override public XWPFTable insertNewTable(XWPFRun run, int row, int col) { XmlCursor cursor = ((XWPFParagraph) run.getParent()).getCTP().newCursor(); XWPFTable table = insertNewTbl(cursor); for (int i = 0; i < row; i++) { XWPFTableRow tabRow = (table.getRow(i) == null) ? table.createRow() : table.getRow(i); for (int k = 0; k < col; k++) { if (tabRow.getCell(k) == null) { tabRow.createCell(); } } } return table; }
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: M2DocEvaluator.java From M2Doc with Eclipse Public License 1.0 | 4 votes |
/** * Fill a newly created word table with the data from an {@link MTable}. * * @param xwpfTable * The newly created word table * @param mTable * The {@link MTable} that describes the data and styles to insert */ private void fillTable(XWPFTable xwpfTable, MTable mTable) { final boolean removeBorders; if (mTable.getStyleID() != null) { xwpfTable.setStyleID(mTable.getStyleID()); removeBorders = true; if (xwpfTable.getCTTbl().getTblPr() != null && xwpfTable.getCTTbl().getTblPr().isSetTblBorders()) { xwpfTable.getCTTbl().getTblPr().unsetTblBorders(); } } else { removeBorders = false; } // Iterate over the rows for (MRow mRow : mTable.getRows()) { final XWPFTableRow xwpfRow = xwpfTable.createRow(); while (!xwpfRow.getTableCells().isEmpty()) { xwpfRow.removeCell(0); } xwpfRow.getCtRow().getTcList().clear(); // Iterate over the columns for (MCell mCell : mRow.getCells()) { final XWPFTableCell xwpfCell = xwpfRow.createCell(); // Populate cell XWPFParagraph xwpfCellParagraph = xwpfCell.getParagraphs().get(0); xwpfCellParagraph.setSpacingBefore(0); xwpfCellParagraph.setSpacingAfter(0); if (mCell != null && mCell.getHAlignment() != null) { xwpfCellParagraph.setAlignment(getHAllignment(mCell.getHAlignment())); } setCellContent(xwpfCell, mCell); if (removeBorders && xwpfCell.getCTTc().getTcPr() != null && xwpfCell.getCTTc().getTcPr().isSetTcBorders()) { xwpfCell.getCTTc().getTcPr().unsetTcBorders(); } } } }