/** * 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()); }
/** * 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; }
/** * 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()); }
/** * 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; }