public class Range
extends java.lang.Object
implements java.lang.Iterable
Remarks
The Range class denotes a region of Excel spreadsheet. With this, you can format and set value of the range. And you can simply copy range of Excel too.Example
The following example shows how to create a range and set value the range of Excel.
//Instantiating a Workbook object
Workbook workbook = new Workbook();
// Get the first Worksheet Cells.
Cells cells = workbook.getWorksheets().get(0).getCells();
// Create a range (A1:D3).
Range range = cells.createRange("A1", "D3");
// Set value to the range.
range.setValue("Hello");
//Save the Excel file
workbook.save("book1.xlsm");
| Modifier and Type | Method and Description |
|---|---|
Hyperlink |
addHyperlink(java.lang.String address,
java.lang.String textToDisplay,
java.lang.String screenTip)
Adds a hyperlink to a specified cell or a range of cells.
|
void |
applyStyle(Style style,
StyleFlag flag)
Applies formats for a whole range.
|
void |
autoFill(Range target)
Automaticall fill the target range.
|
void |
autoFill(Range target,
int autoFillType)
Automaticall fill the target range.
|
void |
clear()
Clears this range.
|
void |
clearComments()
Clears the comments of this range.
|
void |
clearContents()
Clears the contents of this range.
|
void |
clearFormats()
Clears the formats of this range.
|
void |
clearHyperlinks(boolean clearFormat)
Only removes hyperlinks.
|
void |
copy(Range range)
Copies data (including formulas), formatting, drawing objects etc.
|
void |
copy(Range range,
PasteOptions options)
Copying the range with paste special options.
|
void |
copyData(Range range)
Copies cell data (including formulas) from a source range.
|
void |
copyStyle(Range range)
Copies style settings from a source range.
|
void |
copyValue(Range range)
Copies cell value from a source range.
|
Cell |
get(int rowOffset,
int columnOffset)
Gets
Cell object in this range. |
java.lang.String |
getAddress()
Gets address of the range.
|
Cell |
getCellOrNull(int rowOffset,
int columnOffset)
Gets
Cell object or null in this range. |
int |
getColumnCount()
Gets the count of columns in the range.
|
double |
getColumnWidth()
Sets or gets the column width of this range
|
Range |
getCurrentRegion()
Returns a Range object that represents the current region.
|
Range |
getEntireColumn()
Gets a Range object that represents the entire column (or columns) that contains the specified range.
|
Range |
getEntireRow()
Gets a Range object that represents the entire row (or rows) that contains the specified range.
|
int |
getFirstColumn()
Gets the index of the first column of the range.
|
int |
getFirstRow()
Gets the index of the first row of the range.
|
double |
getHeight()
Gets the width of a range in points.
|
Hyperlink[] |
getHyperlinks()
Gets all hyperlink in the range.
|
double |
getLeft()
Gets the distance, in points, from the left edge of column A to the left edge of the range.
|
java.lang.String |
getName()
Gets the name of the range.
|
Range |
getOffset(int rowOffset,
int columnOffset)
Gets
Range range by offset. |
java.lang.String |
getRefersTo()
Gets the range's refers to.
|
int |
getRowCount()
Gets the count of rows in the range.
|
double |
getRowHeight()
Sets or gets the height of rows in this range
|
double |
getTop()
Gets the distance, in points, from the top edge of row 1 to the top edge of the range.
|
java.lang.Object |
getValue()
Gets the value of the range.
|
double |
getWidth()
Gets the width of a range in points.
|
Worksheet |
getWorksheet()
Gets the
getWorksheet()object which contains this range. |
Range |
intersect(Range range)
Returns a
Range object that represents the rectangular intersection of two ranges. |
boolean |
isBlank()
Indicates whether the range contains values.
|
boolean |
isIntersect(Range range)
Indicates whether the range is intersect.
|
java.util.Iterator |
iterator()
Gets the enumerator for cells in this Range.
|
void |
merge()
Combines a range of cells into a single cell.
|
void |
moveTo(int destRow,
int destColumn)
Move the current range to the dest range.
|
void |
putValue(java.lang.String stringValue,
boolean isConverted,
boolean setStyle)
Puts a value into the range, if appropriate the value will be converted to other data type and cell's number format will be reset.
|
void |
setColumnWidth(double value)
Sets or gets the column width of this range
|
void |
setInsideBorders(int borderEdge,
int lineStyle,
CellsColor borderColor)
Set inside borders of the range.
|
void |
setName(java.lang.String value)
Sets the name of the range.
|
void |
setOutlineBorder(int borderEdge,
int borderStyle,
CellsColor borderColor)
Sets outline border around a range of cells.
|
void |
setOutlineBorder(int borderEdge,
int borderStyle,
Color borderColor)
Sets outline border around a range of cells.
|
void |
setOutlineBorders(int[] borderStyles,
Color[] borderColors)
Sets out line borders around a range of cells.
|
void |
setOutlineBorders(int borderStyle,
CellsColor borderColor)
Sets the outline borders around a range of cells with same border style and color.
|
void |
setOutlineBorders(int borderStyle,
Color borderColor)
Sets the outline borders around a range of cells with same border style and color.
|
void |
setRowHeight(double value)
Sets or gets the height of rows in this range
|
void |
setStyle(Style style)
Sets the style of the range.
|
void |
setStyle(Style style,
boolean explicitFlag)
Apply the cell style.
|
void |
setValue(java.lang.Object value)
Sets the value of the range.
|
byte[] |
toHtml(HtmlSaveOptions saveOptions)
Convert the range to html .
|
byte[] |
toImage(ImageOrPrintOptions options)
Converts the range to image.
|
java.lang.String |
toJson(JsonSaveOptions options)
Convert the range to JSON value.
|
java.lang.String |
toString()
Returns a string represents the current Range object.
|
void |
transpose()
Transpose (rotate) data from rows to columns or vice versa.
|
java.util.ArrayList |
union(Range range)
Deprecated.
Use Range.UnionRanges() instead.
|
Range[] |
unionRang(Range range)
Deprecated.
Use Range.UnionRanges() instead.
|
UnionRange |
unionRanges(Range[] ranges)
Returns the union result of two ranges.
|
void |
unMerge()
Unmerges merged cells of this range.
|
public void autoFill(Range target)
Example
//Instantiating a Workbook object
Workbook workbook = new Workbook();
// Get the first Worksheet Cells.
Cells cells = workbook.getWorksheets().get(0).getCells();
cells.get("A1").putValue(1);
cells.get("A2").putValue(2);
Range source = cells.createRange("A1:A2");
Range target = cells.createRange("A3:A10");
//fill 3,4,5....10 to the range A3:A10
source.autoFill(target);
//Save the Excel file
workbook.save("book1.xlsm");
target - the target range.public void autoFill(Range target, int autoFillType)
target - The targed range.autoFillType - AutoFillType. The auto fill type.public Range getCurrentRegion()
public Hyperlink[] getHyperlinks()
public Hyperlink addHyperlink(java.lang.String address, java.lang.String textToDisplay, java.lang.String screenTip)
address - Address of the hyperlink.textToDisplay - The text to be displayed for the specified hyperlink.screenTip - The screenTip text for the specified hyperlink.Hyperlink object.public java.util.Iterator iterator()
Remarks
When traversing elements by the returned Enumerator, the cells collection should not be modified(such as operations that will cause new Cell/Row be instantiated or existing Cell/Row be deleted). Otherwise the enumerator may not be able to traverse all cells correctly(some elements may be traversed repeatedly or skipped).Example
Workbook workbook = new Workbook("template.xlsx");
Cells cells = workbook.getWorksheets().get(0).getCells();
Iterator en = cells.createRange("B2:C3").iterator();
while (en.hasNext())
{
Cell cell = (Cell)en.next();
System.out.println(cell.getName() + ": " + cell.getValue());
}
iterator in interface java.lang.Iterablepublic boolean isIntersect(Range range)
Remarks
If the two ranges area not in the same worksheet ,return false.range - The range.public Range intersect(Range range)
Range object that represents the rectangular intersection of two ranges.
Remarks
If the two ranges are not intersected, returns null.Example
//Instantiating a Workbook object
Workbook workbook = new Workbook();
// Get the first Worksheet Cells.
Cells cells = workbook.getWorksheets().get(0).getCells();
Range range1 = cells.createRange("A1:A5");
Range range2 = cells.createRange("A3:A10");
//Get intersected range of the two ranges.
Range intersectRange = range1.intersect(range2);
//Save the Excel file
workbook.save("book1.xlsm");
range - The intersecting range.Range objectpublic Range[] unionRang(Range range)
Remarks
NOTE: This method is now obsolete. Instead, please use Range.UnionRanges() method. This method will be removed 12 months later since May 2024. Aspose apologizes for any inconvenience you may have experienced.range - The rangepublic UnionRange unionRanges(Range[] ranges)
ranges - The rangepublic java.util.ArrayList union(Range range)
Remarks
NOTE: This method is now obsolete. Instead, please use Range.UnionRanges() method. This method will be removed 12 months later since November 2023. Aspose apologizes for any inconvenience you may have experienced.range - The rangepublic int getRowCount()
public int getColumnCount()
public java.lang.String getName()
Remarks
Named range is supported. For example,range.Name = "Sheet1!MyRange";
public void setName(java.lang.String value)
Remarks
Named range is supported. For example,range.Name = "Sheet1!MyRange";
public java.lang.String getRefersTo()
public java.lang.String getAddress()
public boolean isBlank()
public double getLeft()
public double getTop()
public double getWidth()
public double getHeight()
public int getFirstRow()
public int getFirstColumn()
public void merge()
Remarks
Reference the merged cell via the address of the upper-left cell in the range.public void unMerge()
public void putValue(java.lang.String stringValue,
boolean isConverted,
boolean setStyle)
stringValue - Input valueisConverted - True: converted to other data type if appropriate.setStyle - True: set the number format to cell's style when converting to other data typepublic java.lang.Object getValue()
Remarks
If the range contains multiple cells, the returned/applied object should be Object[][].public void setValue(java.lang.Object value)
Remarks
If the range contains multiple cells, the returned/applied object should be Object[][].public void setStyle(Style style, boolean explicitFlag)
style - The cell style.explicitFlag - True, only overwriting formatting which is explicitly set.public void applyStyle(Style style, StyleFlag flag)
Remarks
Each cell in this range will contains aStyle object.
So this is a memory-consuming method. Please use it carefully.style - The style object which will be applied.flag - Flags which indicates applied formatting properties.public void setStyle(Style style)
style - The Style object.public void setOutlineBorders(int borderStyle,
CellsColor borderColor)
borderStyle - CellBorderType. Border style.borderColor - Border color.public void setOutlineBorders(int borderStyle,
Color borderColor)
borderStyle - CellBorderType. Border style.borderColor - Border color.public void setOutlineBorders(int[] borderStyles,
Color[] borderColors)
Remarks
Both the length of borderStyles and borderStyles must be 4. The order of borderStyles and borderStyles must be top,bottom,left,rightborderStyles - CellBorderType. Border styles.borderColors - Border colors.public void setOutlineBorder(int borderEdge,
int borderStyle,
CellsColor borderColor)
borderEdge - BorderType. Border edge.borderStyle - CellBorderType. Border style.borderColor - Border color.public void setOutlineBorder(int borderEdge,
int borderStyle,
Color borderColor)
borderEdge - BorderType. Border edge.borderStyle - CellBorderType. Border style.borderColor - Border color.public void setInsideBorders(int borderEdge,
int lineStyle,
CellsColor borderColor)
borderEdge - BorderType. Inside borde type, only can be BorderType.VERTICAL and BorderType.HORIZONTAL.lineStyle - CellBorderType. The border style.borderColor - The color of the border.public double getColumnWidth()
public void setColumnWidth(double value)
public double getRowHeight()
public void setRowHeight(double value)
public void moveTo(int destRow,
int destColumn)
destRow - The start row of the dest range.destColumn - The start column of the dest range.public void copyData(Range range)
range - Source Range object.public void copyValue(Range range)
range - Source Range object.public void copyStyle(Range range)
range - Source Range object.public void copy(Range range, PasteOptions options)
range - The source range.options - The paste special options.public void transpose()
public void copy(Range range)
Example
//Instantiating a Workbook object
Workbook workbook = new Workbook();
// Get the first Worksheet Cells.
Cells cells = workbook.getWorksheets().get(0).getCells();
Range range1 = cells.createRange("A1:A5");
Range range2 = cells.createRange("A6:A10");
//Copy the range.
range1.copy(range2);
//Save the Excel file
workbook.save("book1.xlsm");
range - Source Range object.public Cell get(int rowOffset, int columnOffset)
Cell object in this range.rowOffset - Row offset in this range, zero based.columnOffset - Column offset in this range, zero based.Cell object.public Cell getCellOrNull(int rowOffset, int columnOffset)
Cell object or null in this range.rowOffset - Row offset in this range, zero based.columnOffset - Column offset in this range, zero based.Cell object.public Range getOffset(int rowOffset, int columnOffset)
Range range by offset.rowOffset - Row offset in this range, zero based.columnOffset - Column offset in this range, zero based.public Range getEntireColumn()
public Range getEntireRow()
public Worksheet getWorksheet()
getWorksheet()object which contains this range.public java.lang.String toString()
toString in class java.lang.Objectpublic byte[] toImage(ImageOrPrintOptions options)
options - The options for converting this range to imagepublic java.lang.String toJson(JsonSaveOptions options)
options - The options of convertingpublic byte[] toHtml(HtmlSaveOptions saveOptions)
saveOptions - Options for coverting range to html.public void clear()
public void clearContents()
public void clearFormats()
public void clearComments()
public void clearHyperlinks(boolean clearFormat)
clearFormat - Indicates whether to clear the format of hyperlinks.See Also:
Aspose.Cells Documentation - the home page for the Aspose.Cells Product Documentation.
Aspose.Cells Support Forum - our preferred method of support.
We guarantee a prompt response to any inquiry!
© Aspose Pty Ltd 2003-2025. All Rights Reserved.