/** * Helper for {@link #updateColumnWidths(Row)}. * * @param cell the cell to compute the best fit width on * @param pair the column width pair to update * @since 3.14beta1 */ private void updateColumnWidth(final Cell cell, final ColumnWidthPair pair) { final double unmergedWidth = SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, false); final double mergedWidth = SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, true); pair.setMaxColumnWidths(unmergedWidth, mergedWidth); } }
/** * Compute width of a single cell in a row * Convenience method for {@link #getCellWidth} * * @param row the row that contains the cell of interest * @param column the column number of the cell whose width is to be calculated * @param defaultCharWidth the width of a single character * @param formatter formatter used to prepare the text to be measured * @param useMergedCells whether to use merged cells * @return the width in pixels or -1 if cell is empty */ private static double getColumnWidthForRow( Row row, int column, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells) { if( row == null ) { return -1; } Cell cell = row.getCell(column); if (cell == null) { return -1; } return getCellWidth(cell, defaultCharWidth, formatter, useMergedCells); }
width = getCellWidth(defaultCharWidth, colspan, style, width, str); copyAttributes(font, str, 0, txt.length()); width = getCellWidth(defaultCharWidth, colspan, style, width, str);
/** * Helper for {@link #updateColumnWidths(Row)}. * * @param cell the cell to compute the best fit width on * @param pair the column width pair to update * @since 3.14beta1 */ private void updateColumnWidth(final Cell cell, final ColumnWidthPair pair) { final double unmergedWidth = SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, false); final double mergedWidth = SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, true); pair.setMaxColumnWidths(unmergedWidth, mergedWidth); } }
/** * Compute width of a single cell in a row * Convenience method for {@link #getCellWidth} * * @param row the row that contains the cell of interest * @param column the column number of the cell whose width is to be calculated * @param defaultCharWidth the width of a single character * @param formatter formatter used to prepare the text to be measured * @param useMergedCells whether to use merged cells * @return the width in pixels or -1 if cell is empty */ private static double getColumnWidthForRow( Row row, int column, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells) { if( row == null ) { return -1; } Cell cell = row.getCell(column); if (cell == null) { return -1; } return getCellWidth(cell, defaultCharWidth, formatter, useMergedCells); }
/** * Compute width of a column and return the result * * @param sheet the sheet to calculate * @param column 0-based index of the column * @param useMergedCells whether to use merged cells * @return the width in pixels */ public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells){ AttributedString str; TextLayout layout; Workbook wb = sheet.getWorkbook(); DataFormatter formatter = new DataFormatter(); Font defaultFont = wb.getFontAt((short) 0); str = new AttributedString(String.valueOf(defaultChar)); copyAttributes(defaultFont, str, 0, 1); layout = new TextLayout(str.getIterator(), fontRenderContext); int defaultCharWidth = (int)layout.getAdvance(); double width = -1; for (Row row : sheet) { Cell cell = row.getCell(column); if (cell == null) { continue; } double cellWidth = getCellWidth(cell, defaultCharWidth, formatter, useMergedCells); width = Math.max(width, cellWidth); } return width; }
double cellWidth = getCellWidth(cell, defaultCharWidth, formatter, useMergedCells); width = Math.max(width, cellWidth);
double cellWidth = getCellWidth(cell, defaultCharWidth, formatter, useMergedCells); width = Math.max(width, cellWidth);
/** * Compute width of a column and return the result * * @param sheet the sheet to calculate * @param column 0-based index of the column * @param useMergedCells whether to use merged cells * @return the width in pixels */ public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells){ AttributedString str; TextLayout layout; Workbook wb = sheet.getWorkbook(); DataFormatter formatter = new DataFormatter(); Font defaultFont = wb.getFontAt((short) 0); str = new AttributedString(String.valueOf(defaultChar)); copyAttributes(defaultFont, str, 0, 1); layout = new TextLayout(str.getIterator(), fontRenderContext); int defaultCharWidth = (int)layout.getAdvance(); double width = -1; for (Row row : sheet) { Cell cell = row.getCell(column); if (cell == null) { continue; } double cellWidth = getCellWidth(cell, defaultCharWidth, formatter, useMergedCells); width = Math.max(width, cellWidth); } return width; }
width = getCellWidth(defaultCharWidth, colspan, style, width, str); copyAttributes(font, str, 0, txt.length()); width = getCellWidth(defaultCharWidth, colspan, style, width, str);