addNewGridCol.setW(BigInteger.valueOf((long) (w / 2.54 * 1440)));
/** * <w:gridCol list should be filtered to ignore negative value. * <p> * Ex : <w:gridCol w:w="-54" /> should be ignored. See https://code.google.com/p/xdocreport/issues/detail?id=315 * </p> * * @param grid * @return */ private static List<CTTblGridCol> getGridColList( CTTblGrid grid ) { List<CTTblGridCol> newCols = new ArrayList<CTTblGridCol>(); List<CTTblGridCol> cols = grid.getGridColList(); for ( CTTblGridCol col : cols ) { if ( col.getW().floatValue() >= 0 ) { newCols.add( col ); } } return newCols; }
/** * <w:gridCol list should be filtered to ignore negative value. * <p> * Ex : <w:gridCol w:w="-54" /> should be ignored. See https://code.google.com/p/xdocreport/issues/detail?id=315 * </p> * * @param grid * @return */ private static List<CTTblGridCol> getGridColList( CTTblGrid grid ) { List<CTTblGridCol> newCols = new ArrayList<CTTblGridCol>(); List<CTTblGridCol> cols = grid.getGridColList(); for ( CTTblGridCol col : cols ) { if ( col.getW().floatValue() >= 0 ) { newCols.add( col ); } } return newCols; }
public static float[] computeColWidths( CTTbl table ) { CTTblGrid grid = table.getTblGrid(); List<CTTblGridCol> cols = getGridColList( grid ); int nbColumns = cols.size(); float[] colWidths = new float[nbColumns]; float colWidth = -1; int nbColumnsToIgnoreBefore = 0; for ( int i = nbColumnsToIgnoreBefore; i < colWidths.length; i++ ) { CTTblGridCol tblGridCol = cols.get( i ); colWidth = tblGridCol.getW().floatValue(); colWidths[i] = dxa2points( colWidth ); } return colWidths; }
/** * 表格设置宽度,每列平均分布 * * @param table * @param widthCM * @param cols */ public static void widthTable(XWPFTable table, float widthCM, int cols) { int width = (int)(widthCM/2.54*1440); CTTblPr tblPr = table.getCTTbl().getTblPr(); if (null == tblPr) { tblPr = table.getCTTbl().addNewTblPr(); } CTTblWidth tblW = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW(); tblW.setType(0 == width ? STTblWidth.AUTO : STTblWidth.DXA); tblW.setW(BigInteger.valueOf(width)); if (0 != width) { CTTblGrid tblGrid = table.getCTTbl().getTblGrid(); if (null == tblGrid) { tblGrid = table.getCTTbl().addNewTblGrid(); } for (int j = 0; j < cols; j++) { CTTblGridCol addNewGridCol = tblGrid.addNewGridCol(); addNewGridCol.setW(BigInteger.valueOf(width / cols)); } } }
public static float[] computeColWidths( CTTbl table ) { CTTblGrid grid = table.getTblGrid(); List<CTTblGridCol> cols = getGridColList( grid ); int nbColumns = cols.size(); float[] colWidths = new float[nbColumns]; float colWidth = -1; int nbColumnsToIgnoreBefore = 0; for ( int i = nbColumnsToIgnoreBefore; i < colWidths.length; i++ ) { CTTblGridCol tblGridCol = cols.get( i ); colWidth = tblGridCol.getW().floatValue(); colWidths[i] = dxa2points( colWidth ); } return colWidths; }
for (int i = 0; i < 3; i++) { CTTblGridCol gridCol = grid.addNewGridCol(); gridCol.setW(w);
colWidth = tblGridCol.getW().floatValue(); colWidths[i] = dxa2points( colWidth );
colWidth = tblGridCol.getW().floatValue(); colWidths[i] = dxa2points( colWidth );
colWidth = tblGridCol.getW().floatValue(); colWidths[i] = dxa2points( colWidth );