Document Solutions for Excel, .NET Edition | Document Solutions
Features / Worksheet / Range Operations / Access a Range
In This Topic
    Access a Range
    In This Topic

    Range refers to an array of cells defined in a spreadsheet.

    DsExcel allows users to define a range and then access the rows and columns within the range to perform certain tasks like formatting of cells, merging of cells, insertion or deletion of cells along with other useful operations.

    Refer to the following example code in order to access a range using different methods.

    C#
    Copy Code
    //Use index to access cell A1.
    worksheet.Range[0, 0].Interior.Color = Color.LightGreen;
    
    //Use index to access range A1:B2
    worksheet.Range[0, 0, 2, 2].Value = 5;
    
    //Use string to access range.
    worksheet.Range["A2"].Interior.Color = Color.LightYellow;
    worksheet.Range["C3:D4"].Interior.Color = Color.Tomato;
    worksheet.Range["A5:B7, C3, H5:N6"].Value = 2;
    
    //Use index to access rows
    worksheet.Rows[2].Interior.Color = Color.LightSalmon;
    
    //Use string to access rows
    worksheet.Range["4:4"].Interior.Color = Color.LightSkyBlue;
    
    //Use index to access columns
    worksheet.Columns[2].Interior.Color = Color.LightSalmon;
    
    //Use string to access columns
    worksheet.Range["D:D"].Interior.Color = Color.LightSkyBlue;
    
    //Use Cells to access range.
    worksheet.Cells[5].Interior.Color = Color.LightBlue;
    worksheet.Cells[5, 5].Interior.Color = Color.LightYellow;
    
    //Access all rows in worksheet
    var allRows = worksheet.Rows.ToString();
    
    //Access all columns in worksheet
    var allColumns = worksheet.Columns.ToString();
    
    //Access the entire sheet range
    var entireSheet = worksheet.Cells.ToString();
    
    Note: DsExcel also supports defined names, which can be used to name the range. For more information, see Defined Names.