- java.lang.Object
-
- org.dhatim.fastexcel.Worksheet
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class Worksheet extends Object implements Closeable
A worksheet is a set of cells.
-
-
Field Summary
Fields Modifier and Type Field Description static doubleDEFAULT_COL_WIDTHDefault column width in Excel.static intMAX_COL_WIDTHMaximum column width in Excel.static intMAX_COLSMaximum number of columns in Excel.static doubleMAX_ROW_HEIGHTMaximum row height in Excel.static intMAX_ROWSMaximum number of rows in Excel.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddNamedRange(Range range, String name)Add the given range to this sheet's list of named ranges under the provided name.voidbottomMargin(float margin)Set bottom margin.voidclose()voidcomment(int r, int c, String comment)Assign a note/comment to a cell.voidfinish()Finish the construction of this worksheet.voidfirstPageNumber(int pageNumber)voidfitToHeight(Short fitToHeight)voidfitToWidth(Short fitToWidth)voidflush()Write all the rows currently in memory to the workbook's output stream.voidfooter(String text, Position position)Set footer text.voidfooter(String text, Position position, int fontSize)Set footer text with specified font size.voidfooter(String text, Position position, String fontName, int fontSize)Set footer text with specified font and size.voidfooterMargin(float margin)Set footer margin.voidformula(int r, int c, String expression)Set the cell formula at the given coordinates.voidfreezePane(int nLeftColumns, int nTopRows)Set freeze pane (rows and columns that remain when scrolling).RangegetAutoFilterRange()Get cell range that autofilter is applied to.StringgetName()Get worksheet name.Map<String,Range>getNamedRanges()Get a list of named ranges.org.dhatim.fastexcel.RepeatColRangegetRepeatingCols()Get repeating cols defined for the print setup.org.dhatim.fastexcel.RepeatRowRangegetRepeatingRows()Get repeating rows defined for the print setup.VisibilityStategetVisibilityState()WorkbookgetWorkbook()Get parent workbook.voidgroupCols(int from, int to)voidgroupRows(int from, int to)voidheader(String text, Position position)Set header text with specified font and size.voidheader(String text, Position position, int fontSize)Set header text with specified font size.voidheader(String text, Position position, String fontName, int fontSize)Set header text.voidheaderMargin(float margin)Set header margin.voidhideColumn(int column)Hide the given column.voidhideGridLines()Hide grid lines.voidhideRow(int row)Hide the given row.voidhyperlink(int r, int c, HyperLink hyperLink)voidinlineString(int r, int c, String value)Set the cell value at the given coordinates.voidkeepInActiveTab()Keep this sheet in active tab.voidleftMargin(float margin)Set left margin.voidpageOrientation(String orientation)Set the page orientation.voidpageScale(int scale)voidpaperSize(PaperSize size)Set the paper size.voidprintInBlackAndWhite()voidprintInColor()voidprotect(String password)Protects the sheet with a password.voidprotect(String password, Set<SheetProtectionOption> options)Protects the sheet with a password.voidprotect(String password, SheetProtectionOption... options)Protects the sheet with a password.Rangerange(int top, int left, int bottom, int right)Create a new range of cells.voidremoveAutoFilter()Removes auto filter from sheet.voidrepeatCols(int col)voidrepeatCols(int startCol, int endCol)voidrepeatRows(int row)voidrepeatRows(int startRow, int endRow)voidrightMargin(float margin)Set right margin.voidrightToLeft()Display the worksheet from right to leftvoidrowHeight(int r, double height)Specify the custom row height for a rowvoidrowSumsBelow(boolean rowSumsBelow)voidrowSumsRight(boolean rowSumsRight)voidsetAutoFilter(int rowNumber, int leftCellNumber, int rightCellNumber)Applies autofilter automatically based on provided header cellsvoidsetAutoFilter(int topRowNumber, int leftCellNumber, int bottomRowNumber, int rightCellNumber)Applies autofilter specifically to the given cell rangevoidsetAutoPageBreaks(Boolean autoPageBreaks)voidsetFitToPage(Boolean fitToPage)voidsetTabColor(String rgbColor)voidsetVisibilityState(VisibilityState visibilityState)Sets the visibility state of the sheetvoidsetZoom(int zoomPercent)Set sheet view zoom level in percent.voidshowColumn(int column)Show the given column.voidshowRow(int row)Show the given row.ColumnStyleSetterstyle(int c)Get a new style setter for a column.StyleSetterstyle(int r, int c)Get a new style setter for a cell.voidtopMargin(float margin)Set top margin.voidunfreeze()Unfreeze any frozen rows, or columns.Objectvalue(int r, int c)Get the cell value (or formula) at the given coordinates.voidvalue(int r, int c, Boolean value)Set the cell value at the given coordinates.voidvalue(int r, int c, Number value)Set the cell value at the given coordinates.voidvalue(int r, int c, String value)Set the cell value at the given coordinates.voidvalue(int r, int c, LocalDate value)Set the cell value at the given coordinates.voidvalue(int r, int c, LocalDateTime value)Set the cell value at the given coordinates.voidvalue(int r, int c, ZonedDateTime value)Set the cell value at the given coordinates.voidvalue(int r, int c, Date value)Set the cell value at the given coordinates.voidwidth(int c, double width)Specify the width for the given column.
-
-
-
Field Detail
-
MAX_ROWS
public static final int MAX_ROWS
Maximum number of rows in Excel.- See Also:
- Constant Field Values
-
MAX_COLS
public static final int MAX_COLS
Maximum number of columns in Excel.- See Also:
- Constant Field Values
-
MAX_COL_WIDTH
public static final int MAX_COL_WIDTH
Maximum column width in Excel.- See Also:
- Constant Field Values
-
DEFAULT_COL_WIDTH
public static final double DEFAULT_COL_WIDTH
Default column width in Excel.- See Also:
- Constant Field Values
-
MAX_ROW_HEIGHT
public static final double MAX_ROW_HEIGHT
Maximum row height in Excel.- See Also:
- Constant Field Values
-
-
Method Detail
-
getName
public String getName()
Get worksheet name.- Returns:
- Worksheet name.
-
getRepeatingRows
public org.dhatim.fastexcel.RepeatRowRange getRepeatingRows()
Get repeating rows defined for the print setup.- Returns:
- List representing a range of rows to be repeated on each page when printing.
-
getAutoFilterRange
public Range getAutoFilterRange()
Get cell range that autofilter is applied to.- Returns:
- Range of cells that autofilter is set to (null if autofilter is not set).
-
getRepeatingCols
public org.dhatim.fastexcel.RepeatColRange getRepeatingCols()
Get repeating cols defined for the print setup.- Returns:
- List representing a range of columns to be repeated on each page when printing.
-
getNamedRanges
public Map<String,Range> getNamedRanges()
Get a list of named ranges.- Returns:
- Map containing named range entries where keys are the names and values are cell ranges.
-
getWorkbook
public Workbook getWorkbook()
Get parent workbook.- Returns:
- Parent workbook.
-
setVisibilityState
public void setVisibilityState(VisibilityState visibilityState)
Sets the visibility state of the sheetThis is done by setting the
stateattribute in the workbook.xml.- Parameters:
visibilityState- New visibility state for this sheet.
-
getVisibilityState
public VisibilityState getVisibilityState()
-
hideRow
public void hideRow(int row)
Hide the given row.- Parameters:
row- Zero-based row number
-
showRow
public void showRow(int row)
Show the given row.- Parameters:
row- Zero-based row number
-
hideColumn
public void hideColumn(int column)
Hide the given column.- Parameters:
column- Zero-based column number
-
showColumn
public void showColumn(int column)
Show the given column.- Parameters:
column- Zero-based column number
-
keepInActiveTab
public void keepInActiveTab()
Keep this sheet in active tab.
-
protect
public void protect(String password)
Protects the sheet with a password. This method protects all the defaultSheetProtectionOptions and 'sheet'. (Note that this is not very secure and only meant for discouraging changes.)- Parameters:
password- The password to use.
-
protect
public void protect(String password, SheetProtectionOption... options)
Protects the sheet with a password. (Note that this is not very secure and only meant for discouraging changes.)- Parameters:
password- The password to use.options- An array of all theSheetProtectionOptions to protect.
-
protect
public void protect(String password, Set<SheetProtectionOption> options)
Protects the sheet with a password. (Note that this is not very secure and only meant for discouraging changes.)- Parameters:
password- The password to use.options- ASetof all theSheetProtectionOptions to protect.
-
setAutoFilter
public void setAutoFilter(int topRowNumber, int leftCellNumber, int bottomRowNumber, int rightCellNumber)Applies autofilter specifically to the given cell range- Parameters:
topRowNumber- The first row (header) where filter will be initializedleftCellNumber- Left cell number where filter will be initializedbottomRowNumber- The last row (containing values) that will be includedrightCellNumber- Right cell number where filter will be initialized
-
setAutoFilter
public void setAutoFilter(int rowNumber, int leftCellNumber, int rightCellNumber)Applies autofilter automatically based on provided header cells- Parameters:
rowNumber- Row numberleftCellNumber- Left cell number where filter will be initializedrightCellNumber- Right cell number where filter will be initialized
-
removeAutoFilter
public void removeAutoFilter()
Removes auto filter from sheet. Does nothing if there wasn't any filter
-
width
public void width(int c, double width)Specify the width for the given column. Will autoSize by default.The maximum column width in excel is 255. The colum width in excel is the number of characters that can be displayed with the standard font (first font in the workbook).
Note: The xml spec specifies additional padding for each cell (Section 3.3.1.12 of the OOXML spec) which will result in slightly less characters being displayed then what is given here.
- Parameters:
c- Zero-based column numberwidth- The width of the column in character widths
-
rowHeight
public void rowHeight(int r, double height)Specify the custom row height for a rowThe maximum value for row height is 409.5
- Parameters:
r- Zero-based row numberheight- New row height
-
value
public void value(int r, int c, String value)Set the cell value at the given coordinates.- Parameters:
r- Zero-based row number.c- Zero-based column number.value- Cell value.
-
value
public void value(int r, int c, Number value)Set the cell value at the given coordinates.- Parameters:
r- Zero-based row number.c- Zero-based column number.value- Cell value.
-
value
public void value(int r, int c, Boolean value)Set the cell value at the given coordinates.- Parameters:
r- Zero-based row number.c- Zero-based column number.value- Cell value.
-
value
public void value(int r, int c, Date value)Set the cell value at the given coordinates.- Parameters:
r- Zero-based row number.c- Zero-based column number.value- Cell value. Note Excel timestamps do not carry any timezone information;Datevalues are converted to an Excel serial number with the system timezone. If you need a specific timezone, prefer passing aZonedDateTime.
-
value
public void value(int r, int c, LocalDateTime value)Set the cell value at the given coordinates.- Parameters:
r- Zero-based row number.c- Zero-based column number.value- Cell value. Note Excel timestamps do not carry any timezone information;Datevalues are converted to an Excel serial number with the system timezone. If you need a specific timezone, prefer passing aZonedDateTime.
-
value
public void value(int r, int c, LocalDate value)Set the cell value at the given coordinates.- Parameters:
r- Zero-based row number.c- Zero-based column number.value- Cell value. Note Excel timestamps do not carry any timezone information;Datevalues are converted to an Excel serial number with the system timezone. If you need a specific timezone, prefer passing aZonedDateTime.
-
value
public void value(int r, int c, ZonedDateTime value)Set the cell value at the given coordinates.- Parameters:
r- Zero-based row number.c- Zero-based column number.value- Cell value.
-
value
public Object value(int r, int c)
Get the cell value (or formula) at the given coordinates.- Parameters:
r- Zero-based row number.c- Zero-based column number.- Returns:
- Cell value (or
Formula).
-
hyperlink
public void hyperlink(int r, int c, HyperLink hyperLink)
-
formula
public void formula(int r, int c, String expression)Set the cell formula at the given coordinates.- Parameters:
r- Zero-based row number.c- Zero-based column number.expression- Cell formula expression.
-
inlineString
public void inlineString(int r, int c, String value)Set the cell value at the given coordinates.- Parameters:
r- Zero-based row number.c- Zero-based column number.value- Cell value.
-
style
public StyleSetter style(int r, int c)
Get a new style setter for a cell.- Parameters:
r- Zero-based row number.c- Zero-based column number.- Returns:
- Newly created style setter.
-
style
public ColumnStyleSetter style(int c)
Get a new style setter for a column.- Parameters:
c- Zero-based column number.- Returns:
- Newly created style setter.
-
range
public Range range(int top, int left, int bottom, int right)
Create a new range of cells. Note coordinates are reordered if necessary to make suretop<=bottomandleft<=right.- Parameters:
top- Top row.left- Left column.bottom- Bottom row.right- Right column.- Returns:
- Newly created range.
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
finish
public void finish() throws IOExceptionFinish the construction of this worksheet. This creates the worksheet file on the workbook's output stream. Rows and cells in this worksheet are then destroyed.- Throws:
IOException- If an I/O error occurs.
-
flush
public void flush() throws IOExceptionWrite all the rows currently in memory to the workbook's output stream. Call this method periodically when working with huge data sets. After callingflush(), all the rows created so far become inaccessible.
Notes:
- Throws:
IOException- If an I/O error occurs.
-
comment
public void comment(int r, int c, String comment)Assign a note/comment to a cell. The comment popup will be twice the size of the cell and will be initially hidden.Comments are stored in memory till call to
close()(or the old fashion wayfinish()) - callingflush()does not write them to output stream.- Parameters:
r- Zero-based row number.c- Zero-based column number.comment- Note text
-
hideGridLines
public void hideGridLines()
Hide grid lines.
-
rightToLeft
public void rightToLeft()
Display the worksheet from right to left
-
setZoom
public void setZoom(int zoomPercent)
Set sheet view zoom level in percent. Default is 100 (100%).- Parameters:
zoomPercent- - zoom level from 10 to 400
-
setAutoPageBreaks
public void setAutoPageBreaks(Boolean autoPageBreaks)
-
setFitToPage
public void setFitToPage(Boolean fitToPage)
-
freezePane
public void freezePane(int nLeftColumns, int nTopRows)Set freeze pane (rows and columns that remain when scrolling).- Parameters:
nLeftColumns- - number of columns from the left that will remain frozennTopRows- - number of rows from the top that will remain frozen
-
unfreeze
public void unfreeze()
Unfreeze any frozen rows, or columns.
-
headerMargin
public void headerMargin(float margin)
Set header margin.- Parameters:
margin- - header margin in inches
-
footerMargin
public void footerMargin(float margin)
Set footer margin.- Parameters:
margin- - footer page margin in inches
-
topMargin
public void topMargin(float margin)
Set top margin.- Parameters:
margin- - top page margin in inches
-
bottomMargin
public void bottomMargin(float margin)
Set bottom margin.- Parameters:
margin- - bottom page margin in inches
-
leftMargin
public void leftMargin(float margin)
Set left margin.- Parameters:
margin- - left page margin in inches
-
rightMargin
public void rightMargin(float margin)
Set right margin.- Parameters:
margin- - right page margin in inches
-
pageOrientation
public void pageOrientation(String orientation)
Set the page orientation.- Parameters:
orientation- New page orientation for this worksheet
-
paperSize
public void paperSize(PaperSize size)
Set the paper size.- Parameters:
size- New paper size for this worksheet
-
pageScale
public void pageScale(int scale)
- Parameters:
scale- = scaling factor for the print setup (between 1 and 100)
-
firstPageNumber
public void firstPageNumber(int pageNumber)
- Parameters:
pageNumber- - first page number (default: 0)
-
fitToHeight
public void fitToHeight(Short fitToHeight)
-
fitToWidth
public void fitToWidth(Short fitToWidth)
-
printInBlackAndWhite
public void printInBlackAndWhite()
-
printInColor
public void printInColor()
-
repeatRows
public void repeatRows(int startRow, int endRow)
-
repeatRows
public void repeatRows(int row)
-
repeatCols
public void repeatCols(int startCol, int endCol)
-
repeatCols
public void repeatCols(int col)
-
footer
public void footer(String text, Position position)
Set footer text.- Parameters:
text- - text input form or custom textposition- - Position.LEFT/RIGHT/CENTER enum
-
footer
public void footer(String text, Position position, int fontSize)
Set footer text with specified font size.- Parameters:
text- - text input form or custom textposition- - Position.LEFT/RIGHT/CENTER enumfontSize- - integer describing font size
-
footer
public void footer(String text, Position position, String fontName, int fontSize)
Set footer text with specified font and size.- Parameters:
text- - text input form or custom textposition- - Position.LEFT/RIGHT/CENTER enumfontName- - font name (e.g., "Arial")fontSize- - integer describing font size
-
header
public void header(String text, Position position, String fontName, int fontSize)
Set header text.- Parameters:
text- - text input form or custom textposition- - Position.LEFT/RIGHT/CENTER enumfontName- - font name (e.g., "Arial")fontSize- - integer describing font size
-
header
public void header(String text, Position position, int fontSize)
Set header text with specified font size.- Parameters:
text- - text input form or custom textposition- - Position.LEFT/RIGHT/CENTER enumfontSize- - integer describing font size
-
header
public void header(String text, Position position)
Set header text with specified font and size.- Parameters:
text- - text input form or custom textposition- - Position.LEFT/RIGHT/CENTER enum
-
addNamedRange
public void addNamedRange(Range range, String name)
Add the given range to this sheet's list of named ranges under the provided name. It will be visible when this sheet is open in the cell range dropdown menu under the specified name.- Parameters:
range- Range of cells that needs to be named.name- String representing the given cell range's name.
-
groupCols
public void groupCols(int from, int to)
-
groupRows
public void groupRows(int from, int to)
-
rowSumsBelow
public void rowSumsBelow(boolean rowSumsBelow)
-
rowSumsRight
public void rowSumsRight(boolean rowSumsRight)
-
setTabColor
public void setTabColor(String rgbColor)
- Parameters:
rgbColor- FFF381E0
-
-