Referencing Excel cell and range in Java

An Excel cell or range of cells can be referenced by using either names or indexes. If using indexes, you can specify absolute or relative position.

The following example demonstrates various techniques for referencing ExcelCell and CellRange objects in Java, which can be used in GemBox.Spreadsheet for Java library.

Screenshot of Excel cell and range referencing with GemBox.Spreadsheet for Java
Excel cell and range referencing with GemBox.Spreadsheet for Java
import com.gembox.spreadsheet.*;

class Program {

    public static void main(String[] args) throws java.io.IOException {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.setLicense("FREE-LIMITED-KEY");

        ExcelFile workbook = new ExcelFile();
        ExcelWorksheet worksheet = workbook.addWorksheet("Cell Referencing");

        worksheet.getCell(0, 0).setValue("Cell referencing examples:");

        worksheet.getCell("B2").setValue("Cell B2.");
        worksheet.getCell(6, 0).setValue("Cell in row 7 and column A.");

        worksheet.getRow(2).getCell(0).setValue("Cell in row 3 and column A.");
        worksheet.getRow("4").getCell("B").setValue("Cell in row 4 and column B.");

        worksheet.getColumn(2).getCell(4).setValue("Cell in column C and row 5.");
        worksheet.getColumn("AA").getCell("6").setValue("Cell in AA column and row 6.");

        // Referencing Excel row's cell range.
        CellRange cellRange = worksheet.getRow(7).getCells();

        cellRange.get(0).setValue(cellRange.getIndexingMode().toString());
        cellRange.get(3).setValue("D8");
        cellRange.get("B").setValue("B8");

        // Referencing Excel column's cell range.
        cellRange = worksheet.getColumn(7).getCells();

        cellRange.get(0).setValue(cellRange.getIndexingMode().toString());
        cellRange.get(2).setValue("H3");
        cellRange.get("5").setValue("H5");

        // Referencing arbitrary Excel cell range.
        cellRange = worksheet.getCells().getSubrange("I2", "L8");
        cellRange.getStyle().getBorders().setBorders(MultipleBorders.outside(), SpreadsheetColor.fromArgb(0, 0, 128), LineStyle.DASHED);

        cellRange.get("J7").setValue(cellRange.getIndexingMode().toString());
        cellRange.get(0, 0).setValue("I2");
        cellRange.get("J3").setValue("J3");
        cellRange.get(4).setValue("I3"); // Cell range width is 4 (I J K L).

        // Set column widths and some print options
        double[] columnWidths = new double[] { 175, 174, 174, 24, Double.NaN, Double.NaN, Double.NaN, 54, 19, 81 };
        for (int i = 0; i < columnWidths.length; i++)
            if (!Double.isNaN(columnWidths[i]))
                worksheet.getColumn(i).setWidth(columnWidths[i], LengthUnit.PIXEL);

        worksheet.getPrintOptions().setPrintGridlines(true);
        worksheet.getPrintOptions().setPrintHeadings(true);

        workbook.save("Cell Referencing.%OutputFileType%");
    }
}

Check next example or download examples from GitHub.