org.docx4j.wml.Tr Java Examples
The following examples show how to use
org.docx4j.wml.Tr.
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: TableWithMergedCells.java From docx4j-template with Apache License 2.0 | 8 votes |
/** * 我们创建一个单元格和单元格属性对象. * 也创建了一个纵向合并对象. 如果合并值不为null, 将它设置到合并对象中. 然后将该对象添加到 * 单元格属性并将属性添加到单元格中. 最后设置单元格内容并将单元格添加到行中. * * 如果合并值为'restart', 表明要开始一个新行. 如果为null, 继续按前面的行处理, 也就是合并单元格. */ private static void addMergedCell(Tr row, String content, String vMergeVal) { Tc tableCell = factory.createTc(); TcPr tableCellProperties = new TcPr(); VMerge merge = new VMerge(); if(vMergeVal != null){ merge.setVal(vMergeVal); } tableCellProperties.setVMerge(merge); tableCell.setTcPr(tableCellProperties); if(content != null) { tableCell.getContent().add( wordMLPackage.getMainDocumentPart(). createParagraphOfText(content)); } row.getContent().add(tableCell); }
Example #2
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 7 votes |
public static void replaceTable(String[] placeholders, List<Map<String, String>> textToAdd, WordprocessingMLPackage template) throws Docx4JException, JAXBException { List<Tbl> tables = getTargetElements(template.getMainDocumentPart(), Tbl.class); // 1. find the table Tbl tempTable = getTable(tables, placeholders[0]); List<Tr> rows = getTargetElements(tempTable, Tr.class); // first row is header, second row is content if (rows.size() == 2) { // this is our template row Tr templateRow = (Tr) rows.get(1); for (Map<String, String> replacements : textToAdd) { // 2 and 3 are done in this method addRowToTable(tempTable, templateRow, replacements); } // 4. remove the template row tempTable.getContent().remove(templateRow); } }
Example #3
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description: 得到行的列数 */ public static int getTcCellSizeWithMergeNum(Tr tr) { int cellSize = 1; List<Tc> tcList = getTrAllCell(tr); if (tcList == null || tcList.size() == 0) { return cellSize; } cellSize = tcList.size(); for (Tc tc : tcList) { TcPr tcPr = getTcPr(tc); GridSpan gridSpan = tcPr.getGridSpan(); if (gridSpan != null) { cellSize += gridSpan.getVal().intValue() - 1; } } return cellSize; }
Example #4
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description: 跨列合并 */ public static void mergeCellsHorizontal(Tbl tbl, int row, int fromCell, int toCell) { if (row < 0 || fromCell < 0 || toCell < 0) { return; } List<Tr> trList = getTblAllTr(tbl); if (row > trList.size()) { return; } Tr tr = trList.get(row); List<Tc> tcList = getTrAllCell(tr); for (int cellIndex = fromCell, len = Math.min(tcList.size() - 1, toCell); cellIndex <= len; cellIndex++) { Tc tc = tcList.get(cellIndex); TcPr tcPr = getTcPr(tc); HMerge hMerge = tcPr.getHMerge(); if (hMerge == null) { hMerge = new HMerge(); tcPr.setHMerge(hMerge); } if (cellIndex == fromCell) { hMerge.setVal("restart"); } else { hMerge.setVal("continue"); } } }
Example #5
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description: 得到表格所有的行 */ public static List<Tr> getTblAllTr(Tbl tbl) { List<Object> objList = getAllElementFromObject(tbl, Tr.class); List<Tr> trList = new ArrayList<Tr>(); if (objList == null) { return trList; } for (Object obj : objList) { if (obj instanceof Tr) { Tr tr = (Tr) obj; trList.add(tr); } } return trList; }
Example #6
Source File: Docx4j_工具类_S3_Test.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description: 删除指定行 删除后行数减一 */ public boolean removeTrByIndex(Tbl tbl, int index) { boolean flag = false; if (index < 0) { return flag; } List<Object> objList = tbl.getContent(); if (objList == null) { return flag; } int k = -1; for (int i = 0, len = objList.size(); i < len; i++) { Object obj = XmlUtils.unwrap(objList.get(i)); if (obj instanceof Tr) { k++; if (k == index) { tbl.getContent().remove(i); flag = true; break; } } } return flag; }
Example #7
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description: 删除指定行 删除后行数减一 */ public static boolean removeTrByIndex(Tbl tbl, int index) { boolean flag = false; if (index < 0) { return flag; } List<Object> objList = tbl.getContent(); if (objList == null) { return flag; } int k = -1; for (int i = 0, len = objList.size(); i < len; i++) { Object obj = XmlUtils.unwrap(objList.get(i)); if (obj instanceof Tr) { k++; if (k == index) { tbl.getContent().remove(i); flag = true; break; } } } return flag; }
Example #8
Source File: TableWithStyledContent.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * 跟前面的做的一样, 我们再一次创建了一个表格, 并添加了三个单元格, 其中有两个 * 单元带有样式. 在新方法中我们传进表格行, 单元格内容, 是否为粗体及字体大小作 * 为参数. 你需要注意, 因为the Office Open specification规范定义这个属性是半个 * 点(half-point)大小, 因此字体大小需要是你想在Word中显示大小的两倍, */ public static void main (String[] args) throws Docx4JException { wordMLPackage = WordprocessingMLPackage.createPackage(); factory = Context.getWmlObjectFactory(); Tbl table = factory.createTbl(); Tr tableRow = factory.createTr(); addRegularTableCell(tableRow, "Normal text"); addStyledTableCell(tableRow, "Bold text", true, null); addStyledTableCell(tableRow, "Bold large text", true, "40"); table.getContent().add(tableRow); addBorders(table); wordMLPackage.getMainDocumentPart().addObject(table); wordMLPackage.save(new java.io.File("src/main/files/HelloWord6.docx") ); }
Example #9
Source File: SettingColumnWidthForTable.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * 创建一个带边框的表格并添加一行. 然后添加两个带内容的单元格并给定宽度. */ public static void main (String[] args) throws Docx4JException { wordMLPackage = WordprocessingMLPackage.createPackage(); factory = Context.getWmlObjectFactory(); Tbl table = factory.createTbl(); addBorders(table); Tr tr = factory.createTr(); addTableCellWithWidth(tr, "Field 1", 2500); addTableCellWithWidth(tr, "Field 2", 0); table.getContent().add(tr); wordMLPackage.getMainDocumentPart().addObject(table); wordMLPackage.save(new java.io.File("src/main/HelloWord133.docx") ); }
Example #10
Source File: RepeatProcessor.java From docx-stamper with MIT License | 6 votes |
private void repeatRows(final WordprocessingMLPackage document) { for (TableRowCoordinates rCoords : tableRowsToRepeat.keySet()) { List<Object> expressionContexts = tableRowsToRepeat.get(rCoords); int index = rCoords.getIndex(); for (final Object expressionContext : expressionContexts) { Tr rowClone = XmlUtils.deepCopy(rCoords.getRow()); DocumentWalker walker = new BaseDocumentWalker(rowClone) { @Override protected void onParagraph(P paragraph) { placeholderReplacer.resolveExpressionsForParagraph(paragraph, expressionContext, document); } }; walker.walk(); rCoords.getParentTableCoordinates().getTable().getContent().add(++index, rowClone); } rCoords.getParentTableCoordinates().getTable().getContent().remove(rCoords.getRow()); } }
Example #11
Source File: WordprocessingMLPackageRender.java From docx4j-template with Apache License 2.0 | 6 votes |
public void addMergedCell(Tr row, String content, String vMergeVal) { Tc tableCell = factory.createTc(); TcPr tableCellProperties = new TcPr(); VMerge merge = new VMerge(); if(vMergeVal != null){ merge.setVal(vMergeVal); } tableCellProperties.setVMerge(merge); tableCell.setTcPr(tableCellProperties); if(content != null) { tableCell.getContent().add(wmlPackage.getMainDocumentPart(). createParagraphOfText(content)); } row.getContent().add(tableCell); }
Example #12
Source File: Docx4j_工具类_S3_Test.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description: 得到表格所有的行 */ public List<Tr> getTblAllTr(Tbl tbl) { List<Object> objList = getAllElementFromObject(tbl, Tr.class); List<Tr> trList = new ArrayList<Tr>(); if (objList == null) { return trList; } for (Object obj : objList) { if (obj instanceof Tr) { Tr tr = (Tr) obj; trList.add(tr); } } return trList; }
Example #13
Source File: ConditionalDisplayOfParagraphsTest.java From docx-stamper with MIT License | 6 votes |
private void paragraphsInNestedTablesAreRemoved(WordprocessingMLPackage document) { final List<Tbl> tables = new ArrayList<>(); CoordinatesWalker walker = new BaseCoordinatesWalker(document) { @Override protected void onTable(TableCoordinates tableCoordinates) { tables.add(tableCoordinates.getTable()); } }; walker.walk(); Tbl nestedTable = tables.get(1); Tc cell = (Tc) ((JAXBElement) ((Tr) nestedTable.getContent().get(1)).getContent().get(0)).getValue(); P p1 = (P) cell.getContent().get(0); Assert.assertEquals(1, cell.getContent().size()); Assert.assertEquals("This paragraph stays untouched.", new ParagraphWrapper(p1).getText()); }
Example #14
Source File: TableWithBorders.java From docx4j-template with Apache License 2.0 | 5 votes |
private static Tbl createTableWithContent() { Tbl table = factory.createTbl(); Tr tableRow = factory.createTr(); addTableCell(tableRow, "Field 1"); addTableCell(tableRow, "Field 2"); table.getContent().add(tableRow); return table; }
Example #15
Source File: Docx4J_简单例子2.java From docx4j-template with Apache License 2.0 | 5 votes |
public void setTableTrHeight(ObjectFactory factory,Tr tr,String heigth){ TrPr trPr=tr.getTrPr(); if(trPr==null){ trPr=factory.createTrPr(); } CTHeight ctHeight=new CTHeight(); ctHeight.setVal(new BigInteger(heigth)); TrHeight trHeight=new TrHeight(ctHeight); trHeight.set(trPr); tr.setTrPr(trPr); }
Example #16
Source File: Docx4J_例子2.java From docx4j-template with Apache License 2.0 | 5 votes |
public void addTableTitleCell(ObjectFactory factory, WordprocessingMLPackage wordMLPackage, Tbl table, List<String> titleList, RPr rpr, JcEnumeration jcEnumeration, boolean hasBgColor, String backgroudColor) { Tr firstTr = factory.createTr(); Tr secordTr = factory.createTr(); setTableTrHeight(factory, firstTr, "200"); setTableTrHeight(factory, secordTr, "200"); table.getContent().add(firstTr); table.getContent().add(secordTr); for (String str : titleList) { if (str.indexOf("|") == -1) { createNormalCell(factory, firstTr, str, rpr, jcEnumeration, hasBgColor, backgroudColor, false, "restart"); createNormalCell(factory, secordTr, "", rpr, jcEnumeration, hasBgColor, backgroudColor, false, null); } else { String[] cols = str.split("\\|"); createNormalCell(factory, firstTr, cols[0], rpr, jcEnumeration, hasBgColor, backgroudColor, true, "restart"); for (int i = 1; i < cols.length - 1; i++) { createNormalCell(factory, firstTr, "", rpr, jcEnumeration, hasBgColor, backgroudColor, true, null); } for (int i = 1; i < cols.length; i++) { createNormalCell(factory, secordTr, cols[i], rpr, jcEnumeration, hasBgColor, backgroudColor, true, null); } } } }
Example #17
Source File: Docx4j_合并单元格_S4_Test.java From docx4j-template with Apache License 2.0 | 5 votes |
/** * @Description: 获取所有的单元格 */ public List<Tc> getTrAllCell(Tr tr) { List<Object> objList = getAllElementFromObject(tr, Tc.class); List<Tc> tcList = new ArrayList<Tc>(); if (objList == null) { return tcList; } for (Object tcObj : objList) { if (tcObj instanceof Tc) { Tc objTc = (Tc) tcObj; tcList.add(objTc); } } return tcList; }
Example #18
Source File: Docx4j_合并单元格_S4_Test.java From docx4j-template with Apache License 2.0 | 5 votes |
/** * @Description:得到指定位置的表格 */ public Tc getTc(Tbl tbl, int row, int cell) { if (row < 0 || cell < 0) { return null; } List<Tr> trList = getTblAllTr(tbl); if (row >= trList.size()) { return null; } List<Tc> tcList = getTrAllCell(trList.get(row)); if (cell >= tcList.size()) { return null; } return tcList.get(cell); }
Example #19
Source File: Docx4j_合并单元格_S4_Test.java From docx4j-template with Apache License 2.0 | 5 votes |
/** * @Description: 跨列合并 */ public void mergeCellsHorizontal(Tbl tbl, int row, int fromCell, int toCell) { if (row < 0 || fromCell < 0 || toCell < 0) { return; } List<Tr> trList = getTblAllTr(tbl); if (row > trList.size()) { return; } Tr tr = trList.get(row); List<Tc> tcList = getTrAllCell(tr); for (int cellIndex = fromCell, len = Math .min(tcList.size() - 1, toCell); cellIndex <= len; cellIndex++) { Tc tc = tcList.get(cellIndex); TcPr tcPr = getTcPr(tc); HMerge hMerge = tcPr.getHMerge(); if (hMerge == null) { hMerge = new HMerge(); tcPr.setHMerge(hMerge); } if (cellIndex == fromCell) { hMerge.setVal("restart"); } else { hMerge.setVal("continue"); } } }
Example #20
Source File: Docx4j_合并单元格_S4_Test.java From docx4j-template with Apache License 2.0 | 5 votes |
public void mergeCellsHorizontalByGridSpan(Tbl tbl, int row, int fromCell, int toCell) { if (row < 0 || fromCell < 0 || toCell < 0) { return; } List<Tr> trList = getTblAllTr(tbl); if (row > trList.size()) { return; } Tr tr = trList.get(row); List<Tc> tcList = getTrAllCell(tr); for (int cellIndex = Math.min(tcList.size() - 1, toCell); cellIndex >= fromCell; cellIndex--) { Tc tc = tcList.get(cellIndex); TcPr tcPr = getTcPr(tc); if (cellIndex == fromCell) { GridSpan gridSpan = tcPr.getGridSpan(); if (gridSpan == null) { gridSpan = new GridSpan(); tcPr.setGridSpan(gridSpan); } gridSpan.setVal(BigInteger.valueOf(Math.min(tcList.size() - 1, toCell) - fromCell + 1)); } else { tr.getContent().remove(cellIndex); } } }
Example #21
Source File: Docx4j_替换模板.java From docx4j-template with Apache License 2.0 | 5 votes |
public static List<Tr> getTblAllTr(Tbl tbl) { List<Object> objList = getAllElementFromObject(tbl, Tr.class); List<Tr> trList = new ArrayList<Tr>(); if (objList == null) { return trList; } for (Object obj : objList) { if (obj instanceof Tr) { Tr tr = (Tr) obj; trList.add(tr); } } return trList; }
Example #22
Source File: Docx4j_替换模板.java From docx4j-template with Apache License 2.0 | 5 votes |
public static List<Tc> getTrAllCell(Tr tr) { List<Object> objList = getAllElementFromObject(tr, Tc.class); List<Tc> tcList = new ArrayList<Tc>(); if (objList == null) { return tcList; } for (Object tcObj : objList) { if (tcObj instanceof Tc) { Tc objTc = (Tc) tcObj; tcList.add(objTc); } } return tcList; }
Example #23
Source File: Docx4j_替换模板.java From docx4j-template with Apache License 2.0 | 5 votes |
public static Tc getTc(Tbl tbl, int row, int cell) { if (row < 0 || cell < 0) { return null; } List<Tr> trList = getTblAllTr(tbl); if (row >= trList.size()) { return null; } List<Tc> tcList = getTrAllCell(trList.get(row)); if (cell >= tcList.size()) { return null; } return tcList.get(cell); }
Example #24
Source File: Docx4j_替换模板.java From docx4j-template with Apache License 2.0 | 5 votes |
/** * 获取单元格字符串 */ private String[] getTcMarshalStr(Tr tr) { List<Object> tcObjList = tr.getContent(); String[] marshaArr = new String[7]; // 跳过层次 for (int i = 1, len = tcObjList.size(); i < len; i++) { marshaArr[i - 1] = XmlUtils.marshaltoString(tcObjList.get(i), true, false); } return marshaArr; }
Example #25
Source File: Docx4j_替换模板.java From docx4j-template with Apache License 2.0 | 5 votes |
/** * 替换tr数据 */ private void replaceTrSexData(Tr tr, String[] dataArr) throws Exception { List<Tc> tcList = getTrAllCell(tr); Tc tc = null; for (int i = 2, iLen = tcList.size(); i < iLen; i++) { tc = tcList.get(i); replaceTcContent(tc, dataArr[i - 2]); } }
Example #26
Source File: Docx4j_工具类_S3_Test.java From docx4j-template with Apache License 2.0 | 5 votes |
/** * @Description: 获取所有的单元格 */ public List<Tc> getTrAllCell(Tr tr) { List<Object> objList = getAllElementFromObject(tr, Tc.class); List<Tc> tcList = new ArrayList<Tc>(); if (objList == null) { return tcList; } for (Object tcObj : objList) { if (tcObj instanceof Tc) { Tc objTc = (Tc) tcObj; tcList.add(objTc); } } return tcList; }
Example #27
Source File: Docx4j_工具类_S3_Test.java From docx4j-template with Apache License 2.0 | 5 votes |
/** * @Description:隐藏行(只对表格中间的部分起作用,不包括首尾行) */ public void setTrHidden(Tr tr, boolean hidden) { List<Tc> tcList = getTrAllCell(tr); for (Tc tc : tcList) { setTcHidden(tc, hidden); } }
Example #28
Source File: Docx4J_简单例子.java From docx4j-template with Apache License 2.0 | 5 votes |
public void createNormalTableTest(WordprocessingMLPackage wordMLPackage, MainDocumentPart t, ObjectFactory factory) throws Exception { RPr titleRpr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA, true, false, false, false); RPr contentRpr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA, false, false, false, false); Tbl table = factory.createTbl(); addBorders(table, "2"); Tr titleRow = factory.createTr(); addTableCell(factory, wordMLPackage, titleRow, "序号", titleRpr, JcEnumeration.CENTER, true, "C6D9F1"); addTableCell(factory, wordMLPackage, titleRow, "姓甚", titleRpr, JcEnumeration.CENTER, true, "C6D9F1"); addTableCell(factory, wordMLPackage, titleRow, "名谁", titleRpr, JcEnumeration.CENTER, true, "C6D9F1"); addTableCell(factory, wordMLPackage, titleRow, "籍贯", titleRpr, JcEnumeration.CENTER, true, "C6D9F1"); addTableCell(factory, wordMLPackage, titleRow, "营生", titleRpr, JcEnumeration.CENTER, true, "C6D9F1"); table.getContent().add(titleRow); for (int i = 0; i < 10; i++) { Tr contentRow = factory.createTr(); addTableCell(factory, wordMLPackage, contentRow, i + "", contentRpr, JcEnumeration.CENTER, false, null); addTableCell(factory, wordMLPackage, contentRow, "无名氏", contentRpr, JcEnumeration.CENTER, false, null); addTableCell(factory, wordMLPackage, contentRow, "佚名", contentRpr, JcEnumeration.CENTER, false, null); addTableCell(factory, wordMLPackage, contentRow, "武林", contentRpr, JcEnumeration.CENTER, false, null); addTableCell(factory, wordMLPackage, contentRow, "吟诗赋曲", contentRpr, JcEnumeration.CENTER, false, null); table.getContent().add(contentRow); } setTableAlign(factory, table, JcEnumeration.CENTER); t.addObject(table); }
Example #29
Source File: TableWithStyledContent.java From docx4j-template with Apache License 2.0 | 5 votes |
/** * 本方法创建单元格, 添加样式后添加到表格行中 */ private static void addStyledTableCell(Tr tableRow, String content, boolean bold, String fontSize) { Tc tableCell = factory.createTc(); addStyling(tableCell, content, bold, fontSize); tableRow.getContent().add(tableCell); }
Example #30
Source File: ExpressionReplacementInTablesTest.java From docx-stamper with MIT License | 5 votes |
private void unresolvedExpressionsAreNotReplacedInNestedTable(WordprocessingMLPackage document) { Tbl table = (Tbl) ((JAXBElement) document.getMainDocumentPart().getContent().get(1)).getValue(); Tr row = (Tr) table.getContent().get(2); Tc cell = (Tc) ((JAXBElement) row.getContent().get(0)).getValue(); Tbl nestedTable = (Tbl) ((JAXBElement) cell.getContent().get(1)).getValue(); Tr nestedRow = (Tr) nestedTable.getContent().get(1); Tc nestedCell = (Tc) ((JAXBElement) nestedRow.getContent().get(1)).getValue(); P nameParagraph = (P) nestedCell.getContent().get(0); Assert.assertEquals("${foo}", new ParagraphWrapper(nameParagraph).getText()); }