Skip to content

Addressing a worksheet

Cell addressing in EPPlus works pretty much as it works in Excel.
Addressing cells is made in the indexer of the ExcelWorksheet.Cells property (the ExcelRange class). Here“s a few examples of how you can address ranges.

ws.Cells["B1"].Value = "This is cell B1"; // Sets the value of Cell B1
ws.Cells[1, 2].Value = "This is cell B1"; // Also sets the value of Cell B1

worksheet.Cells["A1:B3"].Style.NumberFormat.Format = "#,##0"; //Sets the number format for a range
worksheet.Cells[1,1,3,2].Style.NumberFormat.Format = "#,##0"; //Same as above,A1:B3

worksheet.Cells["A1:B3,D1:E57"].Style.NumberFormat.Format = "#,##0"; //Sets the number format for a range containing two addresses.
worksheet.Cells["A:B"].Style.Font.Bold = true; //Sets font-bold to true for column A & B
worksheet.Cells["1:1,A:A,C3"].Style.Font.Bold = true; //Sets font-bold to true for row 1,column A and cell C3
worksheet.Cells["A:XFD"].Style.Font.Name = "Arial"; //Sets font to Arial for all cells in a worksheet.
worksheet.Cells.Style.Font.Name = "Arial"; //This is equal to the above.

//Access row and column properties. These properties are available from EPPlus 5.8.
worksheet.Rows[1, 18].Height = 25;
worksheet.Cells["A1:A18"].EntireRow.Height = 25; //Access row properties for a range, in this case row 1-18

worksheet.Columns[2, 3].Width = 18;
worksheet.Cells["B1:C1"].EntireColumn.Width = 18; //Access column properties for a range, in this case column B:C
ws.Cells("B1").Value = "This is cell B1" ' Sets the value of Cell B1
ws.Cells(1, 2).Value = "This is cell B1" ' Also sets the value of Cell B1

worksheet.Cells("A1:B3").Style.NumberFormat.Format = "#,##0" 'Sets the number format for a range
worksheet.Cells(1, 1, 3, 2).Style.NumberFormat.Format = "#,##0" 'Same as above,A1:B3

worksheet.Cells("A1:B3,D1:E57").Style.NumberFormat.Format = "#,##0" 'Sets the number format for a range containing two addresses.
worksheet.Cells("A:B").Style.Font.Bold = True 'Sets font-bold to true for column A & B
worksheet.Cells("1:1,A:A,C3").Style.Font.Bold = True 'Sets font-bold to true for row 1,column A and cell C3
worksheet.Cells("A:XFD").Style.Font.Name = "Arial" 'Sets font to Arial for all cells in a worksheet.
worksheet.Cells.Style.Font.Name = "Arial" 'This is equal to the above.

'Access row and column properties. These properties are available from EPPlus 5.8.
worksheet.Rows(1, 18).Height = 25
worksheet.Cells("A1:A18").EntireRow.Height = 25 'Access row properties for a range, in this case row 1-18

worksheet.Columns(2, 3).Width = 18
worksheet.Cells("B1:C1").EntireColumn.Width = 18 'Access column properties for a range, in this case column B:C

Relative references in a sub-range

The EPPlus Cells indexer uses absolute addressing within the worksheet, so if you want to use relative addresses from an existing range object, use the Offset method.

var relRange1 = worksheet.Cells["A3:B4"].Offset(1, 1); //Returns a relative range to the range with 1 row and 1 column, B4:C5
var relRange2 = worksheet.Cells["C3:D4"].Offset(-1, -1); //Returns a relative range to the range with -1 row and -1 column, B2:C3;
var relRange3 = worksheet.Cells["C3:D4"].Offset(1, 1, 1, 1); //Returns a relative range to the range with 1 row and 1 column, but only the first cell, D4;
Dim relRange1 = worksheet.Cells("A3:B4").Offset(1, 1) 'Returns a relative range to the range with 1 row and 1 column, B4:C5
Dim relRange2 = worksheet.Cells("C3:D4").Offset(-1, -1) 'Returns a relative range to the range with -1 row and -1 column, B2:C3;
Dim relRange3 = worksheet.Cells("C3:D4").Offset(1, 1, 1, 1) 'Returns a relative range to the range with 1 row and 1 column, but only the first cell, D4;

See also