Referencing ExcelCell and CellRange in C# and VB.NET

An Excel 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 techniques for referencing ExcelCell and CellRange objects in C# and VB.NET, which can be used in GemBox.Spreadsheet component.

Screenshot

Excel Cell Range 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        // Referencing Excel row's cell range.
28        CellRange cr = ws.Rows[7].Cells;
29
30        cr[0].Value = cr.IndexingMode.ToString();
31        cr[3].Value = "D8";
32        cr["B"].Value = "B8";
33
34        // Referencing Excel column's cell range.
35        cr = ws.Columns[7].Cells;
36
37        cr[0].Value = cr.IndexingMode.ToString();
38        cr[2].Value = "H3";
39        cr["5"].Value = "H5";
40
41        // Referencing arbitrary Excel cell range.
42        cr = ws.Cells.GetSubrange("I2", "L8");
43        cr.Style.Borders.SetBorders(MultipleBorders.Outside, SpreadsheetColor.FromArgb(0, 0, 128), LineStyle.Dashed);
44
45        cr["J7"].Value = cr.IndexingMode.ToString();
46        cr[0, 0].Value = "I2";
47        cr["J3"].Value = "J3";
48        cr[4].Value = "I3"; // Cell range width is 4 (I J K L).
49
50        // Autofit columns and some print options (for better look when exporting to pdf, xps and printing).
51        var columnCount = ws.CalculateMaxUsedColumns();
52        for (int i = 0; i < columnCount; i++)
53            ws.Columns[i].AutoFit();
54
55        ws.PrintOptions.PrintGridlines = true;
56        ws.PrintOptions.PrintHeadings = true;
57
58        ef.Save("Cell Referencing.xls");
59    }
60}
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        ' Referencing Excel row's cell range.
26        Dim cr As CellRange = ws.Rows(7).Cells
27
28        cr(0).Value = cr.IndexingMode.ToString()
29        cr(3).Value = "D8"
30        cr("B").Value = "B8"
31
32        ' Referencing Excel column's cell range.
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        ' Referencing arbitrary Excel cell range.
40        cr = ws.Cells.GetSubrange("I2", "L8")
41        cr.Style.Borders.SetBorders(MultipleBorders.Outside, SpreadsheetColor.FromArgb(0, 0, 128), LineStyle.Dashed)
42
43        cr("J7").Value = cr.IndexingMode.ToString()
44        cr(0, 0).Value = "I2"
45        cr("J3").Value = "J3"
46        cr(4).Value = "I3" ' Cell range width is 4 (I J K L).
47
48        ' Autofit columns and some print options (for better look when exporting to pdf, xps and printing).
49        Dim columnCount = ws.CalculateMaxUsedColumns()
50        For i As Integer = 0 To columnCount - 1
51            ws.Columns(i).AutoFit()
52        Next
53        ws.PrintOptions.PrintGridlines = True
54        ws.PrintOptions.PrintHeadings = True
55
56        ef.Save("Cell Referencing.xls")
57
58    End Sub
59
60End Module

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