Named Ranges

Creating a Named Range

Using Microsoft Excel

The following steps describe how to name a cell or range of cells using Microsoft Excel. This method applies to Microsoft Office Excel 2003, Microsoft Excel 97, 2000, and 2002.

  1. Select the cell, range of cells that you want to name.
  2. Click the Name Box at the left end of the formula bar.
  3. Type the name for the cells.
  4. Press ENTER.

Using Aspose.Cells

Here, we use the Aspose.Cells API to do the task.

Aspose.Cells provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class contains a WorksheetCollection that allows access to each worksheet in an Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides a Cells collection.

It is possible to create a named range by calling the overloaded createRange method of the Cells collection. A typical version of the createRange method takes the following parameters:

  • Name of the upper-left cell, the name of the top-left cell in the range.
  • Name of the lower right cell, the name of the bottom right cell in the range.

When the createRange method is called, it returns the newly created named range as an instance of Range class.

The following example shows how to create a named range of cells that extends over B4:G14.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(CreateNamedRangeofCells.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
// Accessing the first worksheet in the Excel file
Worksheet sheet = worksheets.get(0);
Cells cells = sheet.getCells();
// Creating a named range
Range namedRange = cells.createRange("B4", "G14");
namedRange.setName("TestRange");
// Saving the modified Excel file in default (that is Excel 2000) format
workbook.save(dataDir + "CNROfCells_out.xls");
// Print message
System.out.println("Process completed successfully");

Accessing All Named Ranges in a Spreadsheet

Call the getNamedRanges method of the WorksheetCollection to get all named ranges in a spreadsheet. The getNamedRanges method returns an array of all named ranges in the WorksheetCollection.

The following example shows how to access all the named ranges in a workbook.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AccessAllNamedRanges.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
// Accessing the first worksheet in the Excel file
Worksheet sheet = worksheets.get(0);
Cells cells = sheet.getCells();
// Getting all named ranges
Range[] namedRanges = worksheets.getNamedRanges();
// Print message
System.out.println("Number of Named Ranges : " + namedRanges.length);

Access a Specific Named Range

Call the WorksheetCollection collection’s getRangeByName method to get a specified range by name. A typical getRangeByName method takes the name of the named range and returns the specified named range as an instance of the Range class.

The following example shows how to access a specified range by its name.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AccessSpecificNamedRange.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
// Accessing the first worksheet in the Excel file
Worksheet sheet = worksheets.get(0);
Cells cells = sheet.getCells();
// Getting the specified named range
Range namedRange = worksheets.getRangeByName("TestRange");
// Print message
System.out.println("Named Range : " + namedRange.getRefersTo());

Identify Cells in a Named Range

Using Aspose.Cells, you can insert data into the individual cells of a range. Suppose, you have a named range of cells.i.e., A1:C4. So the matrix would make 4 * 3 = 12 cells and the individual range cells are arranged sequentially. Aspose.Cells provides you some useful Properties of Range class to access the individual cells in the range. You may use the following methods to identify the cells in the range:

  • getFirstRow returns the index of the first row in the named range.
  • getFirstColumn returns the index of the first column in the named range.

The following example shows how to input some values into the cells of a specified range.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(IdentifyCellsinNamedRange.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
// Accessing the first worksheet in the Excel file
Worksheet sheet = worksheets.get(0);
Cells cells = sheet.getCells();
// Getting the specified named range
Range range = worksheets.getRangeByName("TestRange");
// Identify range cells.
System.out.println("First Row : " + range.getFirstRow());
System.out.println("First Column : " + range.getFirstColumn());
System.out.println("Row Count : " + range.getRowCount());
System.out.println("Column Count : " + range.getColumnCount());

Input Data into the Cells in the Named Range

Using Aspose.Cells, you can insert data into the individual cells of a range. Suppose, you have a named range of cells i.e., H1:J4. So the matrix would make 4 * 3 = 12 cells and the individual range cells are arranged sequentially. Aspose.Cells provides you some useful Properties of Range class to access the individual cells in the range. You may use the following properties to identify the cells in the range:

  • getFirstRow returns the index of the first row in the named range.
  • getFirstColumn returns the index of the first column in the named range.

The following example shows how to input some values into the cells of a specified range.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(InputDataInCellsInRange.class) + "data/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.getWorksheets().get(0);
// Create a range of cells and specify its name based on H1:J4.
Range range = worksheet1.getCells().createRange("H1:J4");
range.setName("MyRange");
// Input some data into cells in the range.
range.get(0, 0).setValue("USA");
range.get(0, 1).setValue("SA");
range.get(0, 2).setValue("Israel");
range.get(1, 0).setValue("UK");
range.get(1, 1).setValue("AUS");
range.get(1, 2).setValue("Canada");
range.get(2, 0).setValue("France");
range.get(2, 1).setValue("India");
range.get(2, 2).setValue("Egypt");
range.get(3, 0).setValue("China");
range.get(3, 1).setValue("Philipine");
range.get(3, 2).setValue("Brazil");
// Save the excel file.
workbook.save(dataDir + "IDICInRange_out.xls");
// Print message
System.out.println("Process completed successfully");

Format Ranges…Setting Background Color and Font Attributes to a Named Range

To apply formatting, define a Style object to specify style settings and apply it to the Range object.

The following example shows how to set solid fill color (shading color) with font settings to a range.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(FormatRanges1.class) + "data/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the book.
Worksheet WS = workbook.getWorksheets().get(0);
// Create a named range of cells.
com.aspose.cells.Range range = WS.getCells().createRange(1, 1, 1, 17);
range.setName("MyRange");
// Declare a style object.
Style stl;
// Create the style object with respect to the style of a cell.
stl = WS.getCells().get(1, 1).getStyle();
// Specify some Font settings.
stl.getFont().setName("Arial");
stl.getFont().setBold(true);
// Set the font text color
stl.getFont().setColor(Color.getRed());
// To Set the fill color of the range, you may use ForegroundColor with
// solid Pattern setting.
stl.setBackgroundColor(Color.getYellow());
stl.setPattern(BackgroundType.SOLID);
// Apply the style to the range.
for (int r = 1; r < 2; r++) {
for (int c = 1; c < 18; c++) {
WS.getCells().get(r, c).setStyle(stl);
}
}
// Save the excel file.
workbook.save(dataDir + "FormatRanges1_out.xls");
// Print message
System.out.println("Process completed successfully");

Format Ranges…Adding Borders to a Named Range

It is possible to add borders to a range of cells instead of just a single cell. The Range object provides a setOutlineBorders method that takes the following parameters to add a border to the range of cells:

  • borderStyle: the type of border, selected from the CellBorderType enumeration.
  • borderColor: the line color of the border, selected from the Color enumeration.

The following example shows how to set an outline border to a range.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(FormatRanges2.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
// Obtaining the reference of the newly added worksheet
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.setValue("Hello World From Aspose");
// Creating a range of cells starting from "A1" cell to 3rd column in a
// row
Range range = worksheet.getCells().createRange("A1:C1");
range.setName("MyRange");
// Adding a thick outline border with the blue line
range.setOutlineBorders(CellBorderType.THICK, Color.getBlue());
// Saving the Excel file
workbook.save(dataDir + "FormatRanges2_out.xls");
// Print message
System.out.println("Process completed successfully");

The following output would be generated after executing the above code:

todo:image_alt_text

Apply style to cells in a Range

Sometimes, you want to create apply a style to the cells in a Range. For this, you may iterate over the cells in the range and use the Cell.setStyle method to apply the style to the cell.

The following example shows how to apply styles to cells in a Range.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ConvertCellsAddresstoRangeorCellArea.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the newly added worksheet
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.setValue("Hello World!");
// Creating a range of cells based on cells Address.
Range range = worksheet.getCells().createRange("A1:F10");
// Specify a Style object for borders.
Style style = cell.getStyle();
// Setting the line style of the top border
style.setBorder(BorderType.TOP_BORDER, CellBorderType.THICK, Color.getBlack());
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.THICK, Color.getBlack());
style.setBorder(BorderType.LEFT_BORDER, CellBorderType.THICK, Color.getBlack());
style.setBorder(BorderType.RIGHT_BORDER, CellBorderType.THICK, Color.getBlack());
Iterator cellArray = range.iterator();
while (cellArray.hasNext()) {
Cell temp = (Cell) cellArray.next();
// Saving the modified style to the cell.
temp.setStyle(style);
}
// Saving the Excel file
workbook.save(dataDir + "CCAToROrCArea_out.xls");

Remove a Named Range

Aspose.Cells provides the NameCollection.RemoveAt() method to erase the name of the range. To clear the contents of the range, use Cells.ClearRange() method. The following example shows how to remove a named range with its contents.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(RemoveANamedRange.class) + "data/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get all the worksheets in the book.
WorksheetCollection worksheets = workbook.getWorksheets();
// Get the first worksheet in the worksheets collection.
Worksheet worksheet = workbook.getWorksheets().get(0);
// Create a range of cells.
Range range1 = worksheet.getCells().createRange("E12", "I12");
// Name the range.
range1.setName("MyRange");
// Set the outline border to the range.
range1.setOutlineBorder(BorderType.TOP_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128));
range1.setOutlineBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128));
range1.setOutlineBorder(BorderType.LEFT_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128));
range1.setOutlineBorder(BorderType.RIGHT_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128));
// Input some data with some formattings into
// a few cells in the range.
range1.get(0, 0).setValue("Test");
range1.get(0, 4).setValue("123");
// Create another range of cells.
Range range2 = worksheet.getCells().createRange("B3", "F3");
// Name the range.
range2.setName("testrange");
// Copy the first range into second range.
range2.copy(range1);
// Remove the previous named range (range1) with its contents.
worksheet.getCells().clearRange(11, 4, 11, 8);
worksheets.getNames().removeAt(0);
// Save the excel file.
workbook.save(dataDir + "RANRange_out.xls");
// Print message
System.out.println("Process completed successfully");

borderColors