Class SheetUtil


  • public class SheetUtil
    extends Object
    Helper methods for when working with Usermodel sheets
    • Field Detail

      • DEFAULT_CHAR_WIDTH

        public static final int DEFAULT_CHAR_WIDTH
        Which default char-width to use if the font-system is unavailable.
        See Also:
        Constant Field Values
    • Constructor Detail

      • SheetUtil

        public SheetUtil()
    • Method Detail

      • getCellWidth

        public static double getCellWidth​(Cell cell,
                                          int defaultCharWidth,
                                          DataFormatter formatter,
                                          boolean useMergedCells)
        Deprecated.
        since POI 5.2.5, it is better to pass defaultCharWidth as a float
        Compute width of a single cell
        Parameters:
        cell - the cell whose width is to be calculated
        defaultCharWidth - the width of a single character
        formatter - formatter used to prepare the text to be measured
        useMergedCells - whether to use merged cells
        Returns:
        the width in pixels or -1 if cell is empty
      • getCellWidth

        public static double getCellWidth​(Cell cell,
                                          float defaultCharWidth,
                                          DataFormatter formatter,
                                          boolean useMergedCells)
        Compute width of a single cell
        Parameters:
        cell - the cell whose width is to be calculated
        defaultCharWidth - the width of a single character
        formatter - formatter used to prepare the text to be measured
        useMergedCells - whether to use merged cells
        Returns:
        the width in pixels or -1 if cell is empty
        Since:
        POI 5.2.5
      • getCellWidth

        public static double getCellWidth​(Cell cell,
                                          int defaultCharWidth,
                                          DataFormatter formatter,
                                          boolean useMergedCells,
                                          List<CellRangeAddress> mergedRegions)
        Deprecated.
        since POI 5.2.5, it is better to pass defaultCharWidth as a float
        Compute width of a single cell This method receives the list of merged regions as querying it from the cell/sheet is time-consuming and thus caching the list across cells speeds up certain operations considerably.
        Parameters:
        cell - the cell whose width is to be calculated
        defaultCharWidth - the width of a single character
        formatter - formatter used to prepare the text to be measured
        useMergedCells - whether to use merged cells
        mergedRegions - The list of merged regions as received via cell.getSheet().getMergedRegions()
        Returns:
        the width in pixels or -1 if cell is empty
      • getCellWidth

        public static double getCellWidth​(Cell cell,
                                          float defaultCharWidth,
                                          DataFormatter formatter,
                                          boolean useMergedCells,
                                          List<CellRangeAddress> mergedRegions)
        Compute width of a single cell This method receives the list of merged regions as querying it from the cell/sheet is time-consuming and thus caching the list across cells speeds up certain operations considerably.
        Parameters:
        cell - the cell whose width is to be calculated
        defaultCharWidth - the width of a single character
        formatter - formatter used to prepare the text to be measured
        useMergedCells - whether to use merged cells
        mergedRegions - The list of merged regions as received via cell.getSheet().getMergedRegions()
        Returns:
        the width in pixels or -1 if cell is empty
        Since:
        POI 5.2.5
      • getColumnWidth

        public static double getColumnWidth​(Sheet sheet,
                                            int column,
                                            boolean useMergedCells)
        Compute width of a column and return the result. Note that this fall can fail if you do not have the right fonts installed in your OS.
        Parameters:
        sheet - the sheet to calculate
        column - 0-based index of the column
        useMergedCells - whether to use merged cells
        Returns:
        the width in pixels or -1 if all cells are empty
      • getColumnWidth

        public static double getColumnWidth​(Sheet sheet,
                                            int column,
                                            boolean useMergedCells,
                                            int firstRow,
                                            int lastRow)
        Compute width of a column based on a subset of the rows and return the result. Note that this fall can fail if you do not have the right fonts installed in your OS.
        Parameters:
        sheet - the sheet to calculate
        column - 0-based index of the column
        useMergedCells - whether to use merged cells
        firstRow - 0-based index of the first row to consider (inclusive)
        lastRow - 0-based index of the last row to consider (inclusive)
        Returns:
        the width in pixels or -1 if cell is empty
      • getDefaultCharWidth

        @Internal
        public static int getDefaultCharWidth​(Workbook wb)
        Deprecated.
        since POI 5.2.5, it is recommended to switch to getDefaultCharWidthAsFloat(Workbook).
        Get default character width using the Workbook's default font. Note that this can fail if your OS does not have the right fonts installed.
        Parameters:
        wb - the workbook to get the default character width from
        Returns:
        default character width in pixels
      • getDefaultCharWidthAsFloat

        @Internal
        public static float getDefaultCharWidthAsFloat​(Workbook wb)
        Get default character width using the Workbook's default font. Note that this can fail if your OS does not have the right fonts installed.
        Parameters:
        wb - the workbook to get the default character width from
        Returns:
        default character width in pixels (as a float)
        Since:
        POI 5.2.5
      • canComputeColumnWidth

        public static boolean canComputeColumnWidth​(Font font)
        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.
        Parameters:
        font - The Font that is used in the Cell
        Returns:
        true if computing the size for this Font will succeed, false otherwise
      • getCell

        public static Cell getCell​(Sheet sheet,
                                   int rowIx,
                                   int colIx)
        Return the cell, without taking account of merged regions.

        Use getCellWithMerges(Sheet, int, int) if you want the top left cell from merged regions instead when the reference is a merged cell.

        Use this where you want to know if the given cell is explicitly defined or not.

        Parameters:
        sheet - The workbook sheet to look at.
        rowIx - The 0-based index of the row.
        colIx - The 0-based index of the cell.
        Returns:
        cell at the given location, or null if not defined
        Throws:
        NullPointerException - if sheet is null
      • getCellWithMerges

        public static Cell getCellWithMerges​(Sheet sheet,
                                             int rowIx,
                                             int colIx)
        Return the cell, taking account of merged regions. Allows you to find the cell who's contents are shown in a given position in the sheet.

        If the cell at the given co-ordinates is a merged cell, this will return the primary (top-left) most cell of the merged region.

        If the cell at the given co-ordinates is not in a merged region, then will return the cell itself.

        If there is no cell defined at the given co-ordinates, will return null.

        Parameters:
        sheet - The workbook sheet to look at.
        rowIx - The 0-based index of the row.
        colIx - The 0-based index of the cell.
        Returns:
        cell at the given location, its base merged cell, or null if not defined
        Throws:
        NullPointerException - if sheet is null