Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

There are three ways to address cells, areas, and ranges using ExcelApplication:

Anchor
A1
A1

Excel's A1 Reference Style

A1-style references are easy to read, intuitive, and match the default reference style seen in Microsoft Excel.

Example

Code Block
c#
c#

ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Create();
Worksheet ws = wb.Worksheets[0];
ws.Cells["A3"].Value = "Jan";
ws.Cells["B3"].Value = "Feb";
ws.Cells["C3"].Value = "Mar";
Area a = ws.CreateArea("=A1:G10");

...

The reference style used in the generated workbook (when it is opened in Excel) is determined by the value of the property Workbook.UseRCFormulaNotation. If it is false (the default value), Excel will use A1 references. If UseRCFormulaNotation is set to true, Excel will use R1C1-style references.

Anchor
R1C1
R1C1

...

Relative R1C1 references enable you to reuse a single formula in multiple rows or columns. The following example copies one formula to three different columns to calculate the sum of those columns.

Code Block
c#
c#

ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Create();
wb.UseRCFormulaNotation = true;
Worksheet ws = wb.Worksheets[0];
String formulaString = "=SUM(R[-12]C:R[-1]C)";
Cell columnTotal;
for (int i = 0;i < 3; i++)
{
     columnTotal = ws.Cells[13, i + 1];
     columnTotal.Formula = formulaString;
}

...

The following specifies cell A1 by 0-based row and column numbers: The first parameter of the Cell property is the row index, the second is the column index.

Code Block
c#
c#

sheet1.Cells[2, 0].Value = "SoftArtisans OfficeWriter";

Using row and column numbers makes it easy to iterate over cells:for(int iRow = 3; iRow <= 23; iRow++)

Code Block
c#
c#

for(int iCol = 0; iCol <= 2; iCol++)
     sheet1.Cells[iRow, iCol].Value = iRow + iCol;