net.sf.jett.util
Class SheetUtil

java.lang.Object
  extended by net.sf.jett.util.SheetUtil

public class SheetUtil
extends java.lang.Object

The SheetUtil utility class provides methods for Sheet, Row, and Cell manipulation.

Author:
Randy Gettman

Constructor Summary
SheetUtil()
           
 
Method Summary
static void clearBlock(org.apache.poi.ss.usermodel.Sheet sheet, Block block, WorkbookContext context)
          Blanks out all Cells found inside the given Block on the given Sheet.
static Block copyBlock(org.apache.poi.ss.usermodel.Sheet sheet, Block block, WorkbookContext context, int numBlocksAway)
          Copies an entire Block the given number of blocks away on the given Sheet.
static org.apache.poi.ss.usermodel.CellStyle createCellStyle(org.apache.poi.ss.usermodel.Workbook workbook, short alignment, short borderBottom, short borderLeft, short borderRight, short borderTop, java.lang.String dataFormat, boolean wrapText, org.apache.poi.ss.usermodel.Color fillBackgroundColor, org.apache.poi.ss.usermodel.Color fillForegroundColor, short fillPattern, short verticalAlignment, short indention, short rotationDegrees, org.apache.poi.ss.usermodel.Color bottomBorderColor, org.apache.poi.ss.usermodel.Color leftBorderColor, org.apache.poi.ss.usermodel.Color rightBorderColor, org.apache.poi.ss.usermodel.Color topBorderColor, boolean locked, boolean hidden)
          Creates a new CellStyle for the given Workbook, with the given attributes.
static org.apache.poi.ss.usermodel.Font createFont(org.apache.poi.ss.usermodel.Workbook workbook, short fontBoldweight, boolean fontItalic, org.apache.poi.ss.usermodel.Color fontColor, java.lang.String fontName, short fontHeightInPoints, byte fontUnderline, boolean fontStrikeout, int fontCharset, short fontTypeOffset)
          Creates a new Font for the given Workbook, with the given attributes.
static void deleteBlock(org.apache.poi.ss.usermodel.Sheet sheet, Block block, WorkbookContext context)
          Removes all Cells found inside the given Block on the given Sheet.
static java.lang.String getCellKey(org.apache.poi.ss.usermodel.Cell cell)
          Returns a String that can reference the given Cell.
static org.apache.poi.ss.usermodel.Color getColor(org.apache.poi.ss.usermodel.Workbook workbook, java.lang.String value)
          Determines the proper POI Color, given a string value that could be a color name, e.g.
static java.lang.String getColorHexString(org.apache.poi.ss.usermodel.Color color)
          Get the hex string that represents the Color.
static int getLastPopulatedColIndex(org.apache.poi.ss.usermodel.Sheet sheet)
          Determine the last populated column and return its 0-based index.
static Block getShiftEndingAncestor(Block block)
          Walk up the Block tree until a "shift ending" ancestor is found, or until the tree has been exhausted.
static Block getShiftEndingAncestor(Block block, int numVertCells, int numHorizCells)
          Walk up the Block tree until a "shift ending" ancestor is found, or until the tree has been exhausted.
static void groupColumns(org.apache.poi.ss.usermodel.Sheet sheet, int begin, int end, boolean collapse)
          Group all columns on the sheet between the "begin" and "end" indices, inclusive.
static void groupRows(org.apache.poi.ss.usermodel.Sheet sheet, int begin, int end, boolean collapse)
          Group all rows on the sheet between the "begin" and "end" indices, inclusive.
static boolean isCellBlank(org.apache.poi.ss.usermodel.Row r, int colNum)
          Determines whether the Cell in the given Row at the given index is blank: either it doesn't exist, or it exists and the cell type is blank.
static boolean isCellBlank(org.apache.poi.ss.usermodel.Sheet sheet, int rowNum, int colNum)
          Determines whether the Cell on the given Sheet at the given row and column indexes is blank: either it doesn't exist, or it exists and the cell type is blank.
static void removeBlock(org.apache.poi.ss.usermodel.Sheet sheet, Block block, WorkbookContext context)
          Removes the given Block of Cells from the given Sheet.
static java.lang.Object setCellValue(org.apache.poi.ss.usermodel.Cell cell, java.lang.Object value)
          Sets the cell value on the given Cell to the given value, regardless of data type.
static java.lang.Object setCellValue(org.apache.poi.ss.usermodel.Cell cell, java.lang.Object value, org.apache.poi.ss.usermodel.RichTextString origRichString)
          Sets the cell value on the given Cell to the given value, regardless of data type.
static void setUpBlockForImplicitCollectionAccess(org.apache.poi.ss.usermodel.Sheet sheet, Block block, java.lang.String collExpr, java.lang.String itemName)
          Replace all occurrences of the given collection expression name with the given item name, in preparation for implicit collections processing loops.
static void shiftForBlock(org.apache.poi.ss.usermodel.Sheet sheet, Block block, WorkbookContext context, int numBlocksAway)
          Shifts Cells out of the way.
static void takePastEndAction(org.apache.poi.ss.usermodel.Sheet sheet, Block block, java.util.List<java.lang.String> pastEndRefs, PastEndAction pastEndAction)
          Takes the given PastEndAction on all Cells found inside the given Block on the given Sheet that contain any of the given expressions.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SheetUtil

public SheetUtil()
Method Detail

getLastPopulatedColIndex

public static int getLastPopulatedColIndex(org.apache.poi.ss.usermodel.Sheet sheet)
Determine the last populated column and return its 0-based index.

Parameters:
sheet - The Sheet on which to determine the last populated column.
Returns:
The 0-based index of the last populated column (-1 if the Sheet is empty).

setCellValue

public static java.lang.Object setCellValue(org.apache.poi.ss.usermodel.Cell cell,
                                            java.lang.Object value)
Sets the cell value on the given Cell to the given value, regardless of data type.

Parameters:
cell - The Cell on which to set the value.
value - The value.
Returns:
The actual value set in the Cell.

setCellValue

public static java.lang.Object setCellValue(org.apache.poi.ss.usermodel.Cell cell,
                                            java.lang.Object value,
                                            org.apache.poi.ss.usermodel.RichTextString origRichString)
Sets the cell value on the given Cell to the given value, regardless of data type.

Parameters:
cell - The Cell on which to set the value.
value - The value.
origRichString - The original RichTextString, to be used to set the CellStyle if the value isn't some kind of string (String or RichTextString).
Returns:
The actual value set in the Cell.

isCellBlank

public static boolean isCellBlank(org.apache.poi.ss.usermodel.Sheet sheet,
                                  int rowNum,
                                  int colNum)
Determines whether the Cell on the given Sheet at the given row and column indexes is blank: either it doesn't exist, or it exists and the cell type is blank. That is, whether the cell doesn't exist, is blank, or is empty.

Parameters:
sheet - The Sheet.
rowNum - The 0-based row index.
colNum - The 0-based column index.
Returns:
Whether the Cell is blank.

isCellBlank

public static boolean isCellBlank(org.apache.poi.ss.usermodel.Row r,
                                  int colNum)
Determines whether the Cell in the given Row at the given index is blank: either it doesn't exist, or it exists and the cell type is blank. That is, whether the cell doesn't exist, is blank, or is empty.

Parameters:
r - The Row in which to look for the Cell.
colNum - The 0-based column index.
Returns:
Whether the Cell is blank.

getCellKey

public static java.lang.String getCellKey(org.apache.poi.ss.usermodel.Cell cell)
Returns a String that can reference the given Cell.

Parameters:
cell - The Cell.
Returns:
A string in the format "sheet!A1".

deleteBlock

public static void deleteBlock(org.apache.poi.ss.usermodel.Sheet sheet,
                               Block block,
                               WorkbookContext context)
Removes all Cells found inside the given Block on the given Sheet.

Parameters:
sheet - The Sheet on which to delete a Block
block - The Block of Cells to delete.
context - The WorkbookContext.

clearBlock

public static void clearBlock(org.apache.poi.ss.usermodel.Sheet sheet,
                              Block block,
                              WorkbookContext context)
Blanks out all Cells found inside the given Block on the given Sheet.

Parameters:
sheet - The Sheet on which to clear a Block
block - The Block of Cells to clear.
context - The WorkbookContext.

takePastEndAction

public static void takePastEndAction(org.apache.poi.ss.usermodel.Sheet sheet,
                                     Block block,
                                     java.util.List<java.lang.String> pastEndRefs,
                                     PastEndAction pastEndAction)
Takes the given PastEndAction on all Cells found inside the given Block on the given Sheet that contain any of the given expressions.

Parameters:
sheet - The Sheet on which to delete a Block
block - The Block of Cells to delete.
pastEndRefs - A List of strings identifying which expressions represent collection access beyond the end of the collection.
pastEndAction - An enumerated value representing the action to take on such a cell/expression that references collection access beyond the end of the collection.
See Also:
PastEndAction

removeBlock

public static void removeBlock(org.apache.poi.ss.usermodel.Sheet sheet,
                               Block block,
                               WorkbookContext context)
Removes the given Block of Cells from the given Sheet.

Parameters:
sheet - The Sheet on which to remove the block.
block - The Block to remove.
context - The WorkbookContext.

getShiftEndingAncestor

public static Block getShiftEndingAncestor(Block block)
Walk up the Block tree until a "shift ending" ancestor is found, or until the tree has been exhausted. The "shift ending" ancestor is defined as an ancestor Block that is either a different direction than the original Block or is larger than the original Block along the other direction (that is, larger in height for Horizontal blocks, or larger in width for Vertical blocks).

Parameters:
block - The Block to search for ancestors.
Returns:
The closest "shift ending" ancestor Block.

getShiftEndingAncestor

public static Block getShiftEndingAncestor(Block block,
                                           int numVertCells,
                                           int numHorizCells)
Walk up the Block tree until a "shift ending" ancestor is found, or until the tree has been exhausted. Optionally, grow/shrink parent blocks encountered until the "shift ending" ancestor is found. (The "shift ending" ancestor is not grown/shrunk). The "shift ending" ancestor is defined as an ancestor Block that is either a different direction than the original Block or is larger than the original Block along the other direction (that is, larger in height for Horizontal blocks, or larger in width for Vertical blocks).

Parameters:
block - The Block to search for ancestors.
numVertCells - The number of cells to grow each parent vertically until the "shift ending" ancestor is found, or shrink if numCells is negative.
numHorizCells - The number of cells to grow each parent horizontally until the "shift ending" ancestor is found, or shrink if numCells is negative.
Returns:
The closest "shift ending" ancestor Block.

shiftForBlock

public static void shiftForBlock(org.apache.poi.ss.usermodel.Sheet sheet,
                                 Block block,
                                 WorkbookContext context,
                                 int numBlocksAway)
Shifts Cells out of the way.

Parameters:
sheet - The Sheet on which to shift.
block - The Block whose copies will occupy the Cells that will move to make way for the copies.
context - The WorkbookContext.
numBlocksAway - The number of blocks (widths or lengths, depending on the case of block that defines the area of Cells to shift.

copyBlock

public static Block copyBlock(org.apache.poi.ss.usermodel.Sheet sheet,
                              Block block,
                              WorkbookContext context,
                              int numBlocksAway)
Copies an entire Block the given number of blocks away on the given Sheet.

Parameters:
sheet - The Sheet on which to copy.
block - The Block to copy.
context - The WorkbookContext.
numBlocksAway - The number of blocks (widths or lengths, depending on the direction of block), away to copy.
Returns:
The newly copied Block.

setUpBlockForImplicitCollectionAccess

public static void setUpBlockForImplicitCollectionAccess(org.apache.poi.ss.usermodel.Sheet sheet,
                                                         Block block,
                                                         java.lang.String collExpr,
                                                         java.lang.String itemName)
Replace all occurrences of the given collection expression name with the given item name, in preparation for implicit collections processing loops.

Parameters:
sheet - The Sheet on which the Block lies.
block - The Block in which to perform the replacement.
collExpr - The collection expression string to replace.
itemName - The item name that replaces the collection expression.

groupRows

public static void groupRows(org.apache.poi.ss.usermodel.Sheet sheet,
                             int begin,
                             int end,
                             boolean collapse)
Group all rows on the sheet between the "begin" and "end" indices, inclusive. Optionally collapse the rows.

Parameters:
sheet - The Sheet on which to group the rows.
begin - The 0-based index of the start row of the group.
end - The 0-based index of the end row of the group.
collapse - Whether to collapse the group.
Since:
0.2.0

groupColumns

public static void groupColumns(org.apache.poi.ss.usermodel.Sheet sheet,
                                int begin,
                                int end,
                                boolean collapse)
Group all columns on the sheet between the "begin" and "end" indices, inclusive. Optionally collapse the columns.

Parameters:
sheet - The Sheet on which to group the columns.
begin - The 0-based index of the start column of the group.
end - The 0-based index of the end column of the group.
collapse - Whether to collapse the group.
Since:
0.2.0

getColorHexString

public static java.lang.String getColorHexString(org.apache.poi.ss.usermodel.Color color)
Get the hex string that represents the Color.

Parameters:
color - A POI Color.
Returns:
The hex string that represents the Color.
Since:
0.5.0

createCellStyle

public static org.apache.poi.ss.usermodel.CellStyle createCellStyle(org.apache.poi.ss.usermodel.Workbook workbook,
                                                                    short alignment,
                                                                    short borderBottom,
                                                                    short borderLeft,
                                                                    short borderRight,
                                                                    short borderTop,
                                                                    java.lang.String dataFormat,
                                                                    boolean wrapText,
                                                                    org.apache.poi.ss.usermodel.Color fillBackgroundColor,
                                                                    org.apache.poi.ss.usermodel.Color fillForegroundColor,
                                                                    short fillPattern,
                                                                    short verticalAlignment,
                                                                    short indention,
                                                                    short rotationDegrees,
                                                                    org.apache.poi.ss.usermodel.Color bottomBorderColor,
                                                                    org.apache.poi.ss.usermodel.Color leftBorderColor,
                                                                    org.apache.poi.ss.usermodel.Color rightBorderColor,
                                                                    org.apache.poi.ss.usermodel.Color topBorderColor,
                                                                    boolean locked,
                                                                    boolean hidden)
Creates a new CellStyle for the given Workbook, with the given attributes. Moved from StyleTag here for 0.5.0.

Parameters:
workbook - A Workbook.
alignment - A short alignment constant.
borderBottom - A short border type constant.
borderLeft - A short border type constant.
borderRight - A short border type constant.
borderTop - A short border type constant.
dataFormat - A data format string.
wrapText - Whether text is wrapped.
fillBackgroundColor - A background Color.
fillForegroundColor - A foreground Color.
fillPattern - A short pattern constant.
verticalAlignment - A short vertical alignment constant.
indention - A short number of indent characters.
rotationDegrees - A short degrees rotation of text.
bottomBorderColor - A border Color object.
leftBorderColor - A border Color object.
rightBorderColor - A border Color object.
topBorderColor - A border Color object.
locked - Whether the cell is locked.
hidden - Whether the cell is hidden.
Returns:
A new CellStyle.

createFont

public static org.apache.poi.ss.usermodel.Font createFont(org.apache.poi.ss.usermodel.Workbook workbook,
                                                          short fontBoldweight,
                                                          boolean fontItalic,
                                                          org.apache.poi.ss.usermodel.Color fontColor,
                                                          java.lang.String fontName,
                                                          short fontHeightInPoints,
                                                          byte fontUnderline,
                                                          boolean fontStrikeout,
                                                          int fontCharset,
                                                          short fontTypeOffset)
Creates a new Font for the given Workbook, with the given attributes. Moved from StyleTag here for 0.5.0.

Parameters:
workbook - A Workbook.
fontBoldweight - A short boldweight constant.
fontItalic - Whether the text is italic.
fontColor - A color Color opbject.
fontName - A font name.
fontHeightInPoints - A short font height in points.
fontUnderline - A byte underline constant.
fontStrikeout - Whether the font is strikeout.
fontCharset - An int charset constant.
fontTypeOffset - A short type offset constant.
Returns:
A new Font.

getColor

public static org.apache.poi.ss.usermodel.Color getColor(org.apache.poi.ss.usermodel.Workbook workbook,
                                                         java.lang.String value)
Determines the proper POI Color, given a string value that could be a color name, e.g. "aqua", or a hex string, e.g. "#FFCCCC".

Parameters:
workbook - A Workbook, used only to determine whether to create an HSSFColor or an XSSFColor.
value - The color value, which could be one of the 48 pre-defined color names, or a hex value of the format "#RRGGBB".
Returns:
A Color, or null if an invalid color name was given.


Copyright © 2012-2013 Jett Team. All Rights Reserved.