Class ExcelUtils
public final class ExcelUtils extends Object
- See Also:
Workbook,Sheet,Row,Cell,CellStyle,Font
-
Field Summary
Fields Modifier and Type Field Description static StringEXCEL_2007_EXTENSIONstatic StringEXCEL_97_EXTENSION -
Method Summary
Modifier and Type Method Description static voidautoResizeColumns(org.apache.poi.ss.usermodel.Sheet sheet, int numOfColumns)Adjusts width of columns to fit the contents.static booleanequalsCellStyle(org.apache.poi.ss.usermodel.CellStyle style, org.apache.poi.ss.usermodel.CellStyle other)Returns whether one cell style is equal to the other.static booleanequalsCellStyleAndFont(org.apache.poi.ss.usermodel.Workbook workbook, org.apache.poi.ss.usermodel.CellStyle style, org.apache.poi.ss.usermodel.Workbook otherWorkbook, org.apache.poi.ss.usermodel.CellStyle otherStyle)Returns whether one cell style/font is equal to the other.static booleanequalsFont(org.apache.poi.ss.usermodel.Font font, org.apache.poi.ss.usermodel.Font other)Returns whether one font is equal to the other.static List<org.apache.poi.ss.usermodel.CellStyle>getDeclaredCellStyles(org.apache.poi.ss.usermodel.Workbook workbook)Returns the declared cell styles.static org.apache.poi.ss.usermodel.FontgetFontFromCellStyle(org.apache.poi.ss.usermodel.Workbook workbook, org.apache.poi.ss.usermodel.CellStyle style)Returns a font on cell style in workbook.static intgetMaxColumns(org.apache.poi.ss.usermodel.Sheet sheet)Returns maximum number of columns in a spreadsheet.static intgetMaxColumns(org.apache.poi.ss.usermodel.Workbook workbook)Returns maximum number of columns in a spreadsheet.static intgetMaxRows(org.apache.poi.ss.usermodel.Sheet sheet)Returns maximum number of rows in a spreadsheet.static intgetMaxRows(org.apache.poi.ss.usermodel.Workbook workbook)Returns maximum number of rows in a spreadsheet.static intgetNumOfDeclaredCellStyles(org.apache.poi.ss.usermodel.Workbook workbook)Returns the number of declared cell styles.static intgetNumOfInitialCellStyles(org.apache.poi.ss.usermodel.Workbook workbook)Returns the number of initial cell styles.static longgetNumOfModels(File file)Returns the number of models in all sheets.static intgetNumOfModels(org.apache.poi.ss.usermodel.Sheet sheet)Returns the number of models in a sheet.static longgetNumOfModels(org.apache.poi.ss.usermodel.Workbook workbook)Returns the number of models in all sheets.static longgetNumOfRows(File file)Returns the number of rows in all sheets.static intgetNumOfRows(org.apache.poi.ss.usermodel.Sheet sheet)Returns the number of rows in a sheet.static longgetNumOfRows(org.apache.poi.ss.usermodel.Workbook workbook)Returns the number of rows in all sheets.static int[]getSheetRange(org.apache.poi.ss.usermodel.Workbook workbook)Returns range of the sheets.static List<org.apache.poi.ss.usermodel.Sheet>getSheets(org.apache.poi.ss.usermodel.Workbook workbook)Returns all sheets in a workbook.static org.apache.poi.ss.usermodel.WorkbookgetWorkbook(File file)Returns the instance ofWorkbookby reading file.static voidhideExtraColumns(org.apache.poi.ss.usermodel.Sheet sheet, int numOfColumns)Hides extraneous columns.static voidhideExtraRows(org.apache.poi.ss.usermodel.Sheet sheet, int numOfRows)Hides extraneous rows.static booleanisExcel97(org.apache.poi.ss.usermodel.Cell cell)Checks if spreadsheet's version is 97.static booleanisExcel97(org.apache.poi.ss.usermodel.Row row)Checks if spreadsheet's version is 97.static booleanisExcel97(org.apache.poi.ss.usermodel.Sheet sheet)Checks if spreadsheet's version is 97.static booleanisExcel97(org.apache.poi.ss.usermodel.Workbook workbook)Checks if spreadsheet's version is 97.static voidsetRangeAlias(org.apache.poi.ss.usermodel.Workbook workbook, String alias, String ref)Sets alias for range.static voidsetValidation(org.apache.poi.ss.usermodel.Sheet sheet, org.apache.poi.ss.usermodel.DataValidationHelper helper, String ref, String... values)Sets a validation to the cells on the reference.static org.apache.poi.ss.usermodel.CellStyletoCellStyle(org.apache.poi.ss.usermodel.Workbook workbook, com.github.javaxcel.styler.ExcelStyleConfig config)Converts configuration to cell style.static org.apache.poi.ss.usermodel.CellStyle[]toCellStyles(org.apache.poi.ss.usermodel.Workbook workbook, com.github.javaxcel.styler.ExcelStyleConfig... configs)Converts configurations to cell styles.static StringtoColumnRangeReference(org.apache.poi.ss.usermodel.Sheet sheet, int columnIndex)Converts a reference for column range address except first row.static StringtoRangeReference(org.apache.poi.ss.usermodel.Sheet sheet, int startColumnIndex, int startRowIndex, int endColumnIndex, int endRowIndex)Converts a reference for cell range address.static StringtoRangeReference(org.apache.poi.ss.usermodel.Sheet sheet, org.apache.poi.ss.usermodel.Cell startCell, org.apache.poi.ss.usermodel.Cell endCell)Converts a reference for cell range address.
-
Field Details
-
EXCEL_97_EXTENSION
- See Also:
- Constant Field Values
-
EXCEL_2007_EXTENSION
- See Also:
- Constant Field Values
-
-
Method Details
-
getWorkbook
Returns the instance ofWorkbookby reading file.- Parameters:
file- excel file- Returns:
- excel workbook instance
- Throws:
IllegalArgumentException- unless file extension is equal to 'xls' or 'xlsx'
-
getSheetRange
public static int[] getSheetRange(org.apache.poi.ss.usermodel.Workbook workbook)Returns range of the sheets.- Parameters:
workbook- excel workbook- Returns:
- range that from 0 to (the number of sheets - 1)
- See Also:
Workbook.getNumberOfSheets()
-
getSheets
public static List<org.apache.poi.ss.usermodel.Sheet> getSheets(org.apache.poi.ss.usermodel.Workbook workbook)Returns all sheets in a workbook.- Parameters:
workbook- excel workbook- Returns:
- all sheets
-
getNumOfRows
public static int getNumOfRows(org.apache.poi.ss.usermodel.Sheet sheet)Returns the number of rows in a sheet.- Parameters:
sheet- sheet- Returns:
- the number of rows
- Throws:
UnsupportedWorkbookException- if instance of sheet isSXSSFSheet
-
getNumOfRows
public static long getNumOfRows(org.apache.poi.ss.usermodel.Workbook workbook)Returns the number of rows in all sheets.- Parameters:
workbook- excel workbook- Returns:
- the number of rows
- Throws:
UnsupportedWorkbookException- if instance of workbook isSXSSFWorkbook
-
getNumOfRows
Returns the number of rows in all sheets.- Parameters:
file- excel file- Returns:
- the number of rows
-
getNumOfModels
public static int getNumOfModels(org.apache.poi.ss.usermodel.Sheet sheet)Returns the number of models in a sheet.This excludes header row. In other words, this returns the total number of rows minus 1.
- Parameters:
sheet- sheet- Returns:
- the number of models
- Throws:
UnsupportedWorkbookException- if instance of sheet isSXSSFSheet
-
getNumOfModels
public static long getNumOfModels(org.apache.poi.ss.usermodel.Workbook workbook)Returns the number of models in all sheets.This excludes header row. In other words, this returns the total number of rows minus number of all headers.
- Parameters:
workbook- excel workbook- Returns:
- the number of models
- Throws:
UnsupportedWorkbookException- if instance of workbook isSXSSFWorkbook
-
getNumOfModels
Returns the number of models in all sheets.This excludes header row. In other words, this returns the total number of rows minus number of all headers.
- Parameters:
file- excel file- Returns:
- the number of models
-
getMaxRows
public static int getMaxRows(org.apache.poi.ss.usermodel.Workbook workbook)Returns maximum number of rows in a spreadsheet.- Parameters:
workbook- excel workbook- Returns:
- maximum number of rows in a spreadsheet
-
getMaxRows
public static int getMaxRows(org.apache.poi.ss.usermodel.Sheet sheet)Returns maximum number of rows in a spreadsheet.- Parameters:
sheet- sheet- Returns:
- maximum number of rows in a spreadsheet
-
getMaxColumns
public static int getMaxColumns(org.apache.poi.ss.usermodel.Workbook workbook)Returns maximum number of columns in a spreadsheet.- Parameters:
workbook- excel workbook- Returns:
- maximum number of columns in a spreadsheet
-
getMaxColumns
public static int getMaxColumns(org.apache.poi.ss.usermodel.Sheet sheet)Returns maximum number of columns in a spreadsheet.- Parameters:
sheet- sheet- Returns:
- maximum number of columns in a spreadsheet
-
isExcel97
public static boolean isExcel97(org.apache.poi.ss.usermodel.Workbook workbook)Checks if spreadsheet's version is 97.- Parameters:
workbook- excel workbook- Returns:
- whether spreadsheet's version is 97 or not
-
isExcel97
public static boolean isExcel97(org.apache.poi.ss.usermodel.Sheet sheet)Checks if spreadsheet's version is 97.- Parameters:
sheet- excel sheet- Returns:
- whether spreadsheet's version is 97 or not
-
isExcel97
public static boolean isExcel97(org.apache.poi.ss.usermodel.Row row)Checks if spreadsheet's version is 97.- Parameters:
row- row- Returns:
- whether spreadsheet's version is 97 or not
-
isExcel97
public static boolean isExcel97(org.apache.poi.ss.usermodel.Cell cell)Checks if spreadsheet's version is 97.- Parameters:
cell- cell- Returns:
- whether spreadsheet's version is 97 or not
-
autoResizeColumns
public static void autoResizeColumns(org.apache.poi.ss.usermodel.Sheet sheet, int numOfColumns)Adjusts width of columns to fit the contents.This can be affected by font size and font family. If you want this process well, set up the same font family into all cells. This process will be perform in parallel, but if
SXSSFSheetis, in single.If instance of sheet is
SXSSFSheet, the columns may be inaccurately auto-resized compared toHSSFSheetandXSSFSheet.- Parameters:
sheet- excel sheetnumOfColumns- number of the columns that wanted to make fit contents.- See Also:
Sheet.autoSizeColumn(int)
-
hideExtraRows
public static void hideExtraRows(org.apache.poi.ss.usermodel.Sheet sheet, int numOfRows)Hides extraneous rows.This process will be performed in single-thread. If change this code to be in parallel, this will throw
ConcurrentModificationException.- Parameters:
sheet- excel sheetnumOfRows- number of the rows that have contents.- See Also:
Row.setZeroHeight(boolean)
-
hideExtraColumns
public static void hideExtraColumns(org.apache.poi.ss.usermodel.Sheet sheet, int numOfColumns)Hides extraneous columns.This process shouldn't be performed in parallel. If try it, this is about 46% slower when handled in parallel than when handled in sequential.
+------------+----------+ | sequential | parallel | +------------+----------+ | 15s | 22s | +------------+----------+- Parameters:
sheet- excel sheetnumOfColumns- number of the columns that have contents.- See Also:
Sheet.setColumnHidden(int, boolean)
-
toCellStyle
@Nullable public static org.apache.poi.ss.usermodel.CellStyle toCellStyle(org.apache.poi.ss.usermodel.Workbook workbook, com.github.javaxcel.styler.ExcelStyleConfig config)Converts configuration to cell style.- Parameters:
workbook- excel workbookconfig- configuration of cell style- Returns:
- cell style | null if instance type of config is
NoStyleConfig
-
toCellStyles
@Nullable public static org.apache.poi.ss.usermodel.CellStyle[] toCellStyles(org.apache.poi.ss.usermodel.Workbook workbook, com.github.javaxcel.styler.ExcelStyleConfig... configs)Converts configurations to cell styles.- Parameters:
workbook- excel workbookconfigs- configurations of cell style- Returns:
- cell styles | null if all instance types of configs are
NoStyleConfig - Throws:
IllegalArgumentException- if configs is null or its length is zero.
-
setRangeAlias
public static void setRangeAlias(org.apache.poi.ss.usermodel.Workbook workbook, String alias, String ref)Sets alias for range.Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("mySheet"); String ref = sheet.getSheetName() + "!$A$1:$A$2"; setRangeAlias(workbook, "MY_RANGE", ref);- Parameters:
workbook- excel workbookalias- alias for cell range addressref- reference for cell range address
-
toRangeReference
public static String toRangeReference(org.apache.poi.ss.usermodel.Sheet sheet, org.apache.poi.ss.usermodel.Cell startCell, org.apache.poi.ss.usermodel.Cell endCell)Converts a reference for cell range address.Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("mySheet"); Cell startCell = sheet.createRow(0).createCell(0); Cell endCell = sheet.createRow(1).createCell(0); toRangeReference(sheet, startCell, endCell); // mySheet!$A$1:$A$2- Parameters:
sheet- excel sheetstartCell- first cell in cell range addressendCell- last cell in cell range address- Returns:
- reference for cell range address
- Throws:
IllegalArgumentException- if end cell precedes start cell.
-
toRangeReference
public static String toRangeReference(org.apache.poi.ss.usermodel.Sheet sheet, int startColumnIndex, int startRowIndex, int endColumnIndex, int endRowIndex)Converts a reference for cell range address.Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("mySheet"); toRangeReference(sheet, 0, 0, 0, 1); // mySheet!$A$1:$A$2 toRangeReference(sheet, 2, 1, 5, 4); // mySheet!$C$2:$F$5- Parameters:
sheet- excel sheetstartColumnIndex- column index of first cell in cell range addressstartRowIndex- row index of first cell in cell range addressendColumnIndex- column index of end cell in cell range addressendRowIndex- row index of end cell in cell range address- Returns:
- reference for cell range address
- Throws:
IllegalArgumentException- if end cell's column/row index precedes start cell's
-
toColumnRangeReference
public static String toColumnRangeReference(org.apache.poi.ss.usermodel.Sheet sheet, int columnIndex)Converts a reference for column range address except first row.Workbook hssfWorkbook = new HSSFWorkbook(); Sheet hssfSheet = hssfWorkbook.createSheet("mySheet"); toRangeReference(hssfSheet, 0); // mySheet!$A$2:$A$65536 Workbook xssfWorkbook = new XSSFWorkbook(); Sheet xssfSheet = xssfWorkbook.createSheet("mySheet"); toRangeReference(xssfSheet, 2); // mySheet!$C$2:$A$1048576- Parameters:
sheet- excel sheetcolumnIndex- column index for cell range address- Returns:
- reference for column range address except first row
- Throws:
IllegalArgumentException- if column index is greater than max column index of the sheet
-
setValidation
public static void setValidation(org.apache.poi.ss.usermodel.Sheet sheet, org.apache.poi.ss.usermodel.DataValidationHelper helper, String ref, String... values)Sets a validation to the cells on the reference.Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("mySheet"); DataValidationHelper helper = sheet.getDataValidationHelper(); String ref = toRangeReference(sheet, 2); setValidation(sheet, helper, ref, "RED", "GREEN", "BLUE");- Parameters:
sheet- excel sheethelper- data validation helperref- reference for cell range addressvalues- constraint values- See Also:
Sheet.getDataValidationHelper()
-
getNumOfDeclaredCellStyles
public static int getNumOfDeclaredCellStyles(org.apache.poi.ss.usermodel.Workbook workbook)Returns the number of declared cell styles.- Parameters:
workbook- excel workbook- Returns:
- number of declared cell styles
-
getNumOfInitialCellStyles
public static int getNumOfInitialCellStyles(org.apache.poi.ss.usermodel.Workbook workbook)Returns the number of initial cell styles.- Parameters:
workbook- excel workbook- Returns:
- the number of initial cell styles
-
getDeclaredCellStyles
public static List<org.apache.poi.ss.usermodel.CellStyle> getDeclaredCellStyles(org.apache.poi.ss.usermodel.Workbook workbook)Returns the declared cell styles.- Parameters:
workbook- excel workbook- Returns:
- the declared cell styles
-
getFontFromCellStyle
public static org.apache.poi.ss.usermodel.Font getFontFromCellStyle(org.apache.poi.ss.usermodel.Workbook workbook, org.apache.poi.ss.usermodel.CellStyle style)Returns a font on cell style in workbook.- Parameters:
workbook- excel workbookstyle- cell style- Returns:
- font on cell style
-
equalsCellStyleAndFont
public static boolean equalsCellStyleAndFont(org.apache.poi.ss.usermodel.Workbook workbook, org.apache.poi.ss.usermodel.CellStyle style, org.apache.poi.ss.usermodel.Workbook otherWorkbook, org.apache.poi.ss.usermodel.CellStyle otherStyle)Returns whether one cell style/font is equal to the other.- Parameters:
workbook- excel workbookstyle- cell styleotherWorkbook- other excel workbookotherStyle- other cell style- Returns:
- whether one cell style/font is equal to the other
-
equalsCellStyle
public static boolean equalsCellStyle(org.apache.poi.ss.usermodel.CellStyle style, org.apache.poi.ss.usermodel.CellStyle other)Returns whether one cell style is equal to the other.- Parameters:
style- cell styleother- other cell style- Returns:
- whether one cell style is equal to the other
-
equalsFont
public static boolean equalsFont(org.apache.poi.ss.usermodel.Font font, org.apache.poi.ss.usermodel.Font other)Returns whether one font is equal to the other.- Parameters:
font- fontother- other font- Returns:
- whether one font is equal to the other
-