copyAttributes(font, str, 0, txt.length()); width = getCellWidth(defaultCharWidth, colspan, style, width, str); String txt = sval + defaultChar; AttributedString str = new AttributedString(txt); copyAttributes(font, str, 0, txt.length()); width = getCellWidth(defaultCharWidth, colspan, style, width, str);
/** * Adjusts the column width to fit the contents.<p> * * This process can be relatively slow on large sheets, so this should * normally only be called once per column, at the end of your * processing.<p> * * You can specify whether the content of merged cells should be considered or ignored. * Default is to ignore merged cells. * * @param column the column index * @param useMergedCells whether to use the contents of merged cells when calculating the width of the column */ @Override public void autoSizeColumn(int column, boolean useMergedCells) { double width = SheetUtil.getColumnWidth(this, column, useMergedCells); if (width != -1) { width *= 256; int maxColumnWidth = 255 * 256; // The maximum column width for an individual cell is 255 characters if (width > maxColumnWidth) { width = maxColumnWidth; } setColumnWidth(column, (int) (width)); } }
/** * Compute width of a column based on a subset of the rows 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 * @param firstRow 0-based index of the first row to consider (inclusive) * @param lastRow 0-based index of the last row to consider (inclusive) * @return the width in pixels or -1 if cell is empty */ public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells, int firstRow, int lastRow){ DataFormatter formatter = new DataFormatter(); int defaultCharWidth = getDefaultCharWidth(sheet.getWorkbook()); double width = -1; for (int rowIdx = firstRow; rowIdx <= lastRow; ++rowIdx) { Row row = sheet.getRow(rowIdx); if( row != null ) { double cellWidth = getColumnWidthForRow(row, column, defaultCharWidth, formatter, useMergedCells); width = Math.max(width, cellWidth); } } return width; }
/** * 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); } }
for (int i = 0 ; i < sheet.getNumMergedRegions(); i++) { CellRangeAddress region = sheet.getMergedRegion(i); if (containsCell(region, row.getRowNum(), column)) { if (!useMergedCells) { copyAttributes(font, str, 0, txt.length()); String txt = sval + defaultChar; str = new AttributedString(txt); copyAttributes(font, str, 0, txt.length());
/** * Check if the Fonts are installed correctly so that Java can compute the size of * columns. * * If a Cell uses a Font which is not available on the operating system then Java may * fail to return useful Font metrics and thus lead to an auto-computed size of 0. * * This method allows to check if computing the sizes for a given Font will succeed or not. * * @param font The Font that is used in the Cell * @return true if computing the size for this Font will succeed, false otherwise */ public static boolean canComputeColumnWidth(Font font) { // not sure what is the best value sample-here, only "1" did not work on some platforms... AttributedString str = new AttributedString("1w"); copyAttributes(font, str, 0, "1w".length()); TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext); return (layout.getBounds().getWidth() > 0); }
/** * AutoSizeColumnTracker constructor. Holds no reference to <code>sheet</code> * * @param sheet the sheet associated with this auto-size column tracker * @since 3.14beta1 */ public AutoSizeColumnTracker(final Sheet sheet) { // If sheet needs to be saved, use a java.lang.ref.WeakReference to avoid garbage collector gridlock. defaultCharWidth = SheetUtil.getDefaultCharWidth(sheet.getWorkbook()); }
final Cell c = getCell(sheet, rowIx, colIx); if (c != null) return c;
if (!SheetUtil.containsCell(merged, startRow-1, 0) && !SheetUtil.containsCell(merged, endRow+1, 0)){ merged.setFirstRow(merged.getFirstRow()+n); merged.setLastRow(merged.getLastRow()+n);
/** * 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); }
for (int i = 0 ; i < sheet.getNumMergedRegions(); i++) { CellRangeAddress region = sheet.getMergedRegion(i); if (containsCell(region, row.getRowNum(), column)) { if (!useMergedCells) { copyAttributes(font, str, 0, txt.length()); String txt = sval + defaultChar; str = new AttributedString(txt); copyAttributes(font, str, 0, txt.length());
/** * Get default character width using the Workbook's default font * * @param wb the workbook to get the default character width from * @return default character width in pixels */ @Internal public static int getDefaultCharWidth(final Workbook wb) { Font defaultFont = wb.getFontAt( 0); AttributedString str = new AttributedString(String.valueOf(defaultChar)); copyAttributes(defaultFont, str, 0, 1); TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext); return (int) layout.getAdvance(); }
/** * AutoSizeColumnTracker constructor. Holds no reference to <code>sheet</code> * * @param sheet the sheet associated with this auto-size column tracker * @since 3.14beta1 */ public AutoSizeColumnTracker(final Sheet sheet) { // If sheet needs to be saved, use a java.lang.ref.WeakReference to avoid garbage collector gridlock. defaultCharWidth = SheetUtil.getDefaultCharWidth(sheet.getWorkbook()); }
final Cell cell = SheetUtil.getCell(workbook.getSheet(cellRef.getSheetName()), cellRef.getRow(), cellRef.getCol());
if (!SheetUtil.containsCell(merged, startRow - 1, 0) && !SheetUtil.containsCell(merged, endRow + 1, 0)) { merged.setFirstRow(merged.getFirstRow() + n); merged.setLastRow(merged.getLastRow() + n);
/** * 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; }
/** * 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 or -1 if all cells are empty */ public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells) { return getColumnWidth(sheet, column, useMergedCells, sheet.getFirstRowNum(), sheet.getLastRowNum()); }
/** * 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); } }
/** * Check if the Fonts are installed correctly so that Java can compute the size of * columns. * * If a Cell uses a Font which is not available on the operating system then Java may * fail to return useful Font metrics and thus lead to an auto-computed size of 0. * * This method allows to check if computing the sizes for a given Font will succeed or not. * * @param font The Font that is used in the Cell * @return true if computing the size for this Font will succeed, false otherwise */ public static boolean canComputeColumnWidth(Font font) { // not sure what is the best value sample-here, only "1" did not work on some platforms... AttributedString str = new AttributedString("1w"); copyAttributes(font, str, 0, "1w".length()); TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext); return (layout.getBounds().getWidth() > 0); }
/** * Compute width of a column based on a subset of the rows 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 * @param firstRow 0-based index of the first row to consider (inclusive) * @param lastRow 0-based index of the last row to consider (inclusive) * @return the width in pixels or -1 if cell is empty */ public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells, int firstRow, int lastRow){ DataFormatter formatter = new DataFormatter(); int defaultCharWidth = getDefaultCharWidth(sheet.getWorkbook()); double width = -1; for (int rowIdx = firstRow; rowIdx <= lastRow; ++rowIdx) { Row row = sheet.getRow(rowIdx); if( row != null ) { double cellWidth = getColumnWidthForRow(row, column, defaultCharWidth, formatter, useMergedCells); width = Math.max(width, cellWidth); } } return width; }