ExcelCell Referencing

A cell can be referenced in a variety of ways, by using either names or indexes. If using indexes, you can specify absolute or relative position.

Following sample demonstrates various cell referencing techniques that can be used in GemBox.Spreadsheet component.

Screenshot

Cell Referencing Screenshot

See the full code below, use RUN EXAMPLE to execute.


1using GemBox.Spreadsheet;
2using System;
3using System.IO;
4
5class Sample
6{
7    [STAThread]
8    static void Main(string[] args)
9    {
10        // If using Professional version, put your serial key below.
11        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
12
13        ExcelFile ef = new ExcelFile();
14        ExcelWorksheet ws = ef.Worksheets.Add("Cell Referencing");
15
16        ws.Cells[0].Value = "Cell referencing examples:";
17
18        ws.Cells["B2"].Value = "Cell B2.";
19        ws.Cells[6, 0].Value = "Cell in row 7 and column A.";
20
21        ws.Rows[2].Cells[0].Value = "Cell in row 3 and column A.";
22        ws.Rows["4"].Cells["B"].Value = "Cell in row 4 and column B.";
23
24        ws.Columns[2].Cells[4].Value = "Cell in column C and row 5.";
25        ws.Columns["AA"].Cells["6"].Value = "Cell in AA column and row 6.";
26
27        CellRange cr = ws.Rows[7].Cells;
28
29        cr[0].Value = cr.IndexingMode.ToString();
30        cr[3].Value = "D8";
31        cr["B"].Value = "B8";
32
33        cr = ws.Columns[7].Cells;
34
35        cr[0].Value = cr.IndexingMode.ToString();
36        cr[2].Value = "H3";
37        cr["5"].Value = "H5";
38
39        cr = ws.Cells.GetSubrange("I2", "L8");
40        cr.Style.Borders.SetBorders(MultipleBorders.Outside, SpreadsheetColor.FromArgb(0, 0, 128), LineStyle.Dashed);
41
42        cr["J7"].Value = cr.IndexingMode.ToString();
43        cr[0, 0].Value = "I2";
44        cr["J3"].Value = "J3";
45        cr[4].Value = "I3"; // Cell range width is 4 (I J K L).
46
47        // Autofit columns and some print options (for better look when exporting to pdf, xps and printing).
48        var columnCount = ws.CalculateMaxUsedColumns();
49        for (int i = 0; i < columnCount; i++)
50            ws.Columns[i].AutoFit();
51
52        ws.PrintOptions.PrintGridlines = true;
53        ws.PrintOptions.PrintHeadings = true;
54
55        ef.Save("Cell Referencing.xls");
56    }
57}
1Imports GemBox.Spreadsheet
2Imports System.IO
3
4Module Samples
5
6    Sub Main()
7
8        ' If using Professional version, put your serial key below.
9        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
10
11        Dim ef As ExcelFile = New ExcelFile
12        Dim ws As ExcelWorksheet = ef.Worksheets.Add("Cell Referencing")
13
14        ws.Cells(0).Value = "Cell referencing examples:"
15
16        ws.Cells("B2").Value = "Cell B2."
17        ws.Cells(6, 0).Value = "Cell in row 7 and column A."
18
19        ws.Rows(2).Cells(0).Value = "Cell in row 3 and column A."
20        ws.Rows("4").Cells("B").Value = "Cell in row 4 and column B."
21
22        ws.Columns(2).Cells(4).Value = "Cell in column C and row 5."
23        ws.Columns("AA").Cells("6").Value = "Cell in AA column and row 6."
24
25        Dim cr As CellRange = ws.Rows(7).Cells
26
27        cr(0).Value = cr.IndexingMode.ToString()
28        cr(3).Value = "D8"
29        cr("B").Value = "B8"
30
31        cr = ws.Columns(7).Cells
32
33        cr(0).Value = cr.IndexingMode.ToString()
34        cr(2).Value = "H3"
35        cr("5").Value = "H5"
36
37        cr = ws.Cells.GetSubrange("I2", "L8")
38        cr.Style.Borders.SetBorders(MultipleBorders.Outside, SpreadsheetColor.FromArgb(0, 0, 128), LineStyle.Dashed)
39
40        cr("J7").Value = cr.IndexingMode.ToString()
41        cr(0, 0).Value = "I2"
42        cr("J3").Value = "J3"
43        cr(4).Value = "I3" ' Cell range width is 4 (I J K L).
44
45        ' Autofit columns and some print options (for better look when exporting to pdf, xps and printing).
46        Dim columnCount = ws.CalculateMaxUsedColumns()
47        For i As Integer = 0 To columnCount - 1
48            ws.Columns(i).AutoFit()
49        Next
50        ws.PrintOptions.PrintGridlines = True
51        ws.PrintOptions.PrintHeadings = True
52
53        ef.Save("Cell Referencing.xls")
54
55    End Sub
56
57End Module

Check next sample or find out more about GemBox.Spreadsheet and GemBox Software.