Winnovative Software Logo

 HTML to PDF Converter - Excel Library - ASP.NET Charts - RTF to PDF Converter
 PDF Merge and Split - PDF Security - PDF Viewers - PDF to Text - Images Extractor

Skip Navigation Links
Accessing Cells and Ranges with Winnovative Excel Library for .NET
This sample shows how to access the ranges and cells from a worksheet. The ranges and cells can be accessed using the A1 notation or can be referenced directly using the row and column indexes. Also it is possible to assign a name to a range and access that range by the assigned name.
To create the Excel workbook first select the format of the generated workbook and press the Create Workbook button. The Excel workbook will be created on the server and sent as an attachment to the browser. You will be prompted to open the generated workbook in an exernal viewer. The free Excel Viewer from Microsoft supports both the Excel 97-2003 (.xls) format and the new Excel 2007 (.xlsx) format.
Workbook Format:
Create Workbook Button Create Excel Workbook

protected void lnkBtnCreateWorkbook_Click(object sender, EventArgs e)
    // get the Excel workbook format
    ExcelWorkbookFormat workbookFormat = radioXlsFormat.Checked ? ExcelWorkbookFormat.Xls_2003 : 

    // create the workbook in the desired format with a single worksheet
    ExcelWorkbook workbook = new ExcelWorkbook(workbookFormat);

    // set the license key before saving the workbook
    workbook.LicenseKey = "RW51ZXZ0ZXVldGt1ZXZ0a3R3a3x8fHw=";

    // set workbook description properties
    workbook.DocumentProperties.Subject = "Accessing Ranges and Cells";
    workbook.DocumentProperties.Comments = 
                "Accessing Ranges and Cells using Winnovative Excel library for .NET";


    #region Add a style used for the cells in the worksheet title area

    ExcelCellStyle titleStyle = workbook.Styles.AddStyle("WorksheetTitleStyle");
    // center the text in the title area
    titleStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
    titleStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
    // set the title area borders
    titleStyle.Borders[ExcelCellBorderIndex.Bottom].Color = Color.Green;
    titleStyle.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Medium;
    titleStyle.Borders[ExcelCellBorderIndex.Top].Color = Color.Green;
    titleStyle.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Medium;
    titleStyle.Borders[ExcelCellBorderIndex.Left].Color = Color.Green;
    titleStyle.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Medium;
    titleStyle.Borders[ExcelCellBorderIndex.Right].Color = Color.Green;
    titleStyle.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Medium;
    if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
        // set the solid fill for the title area range with a custom color
        titleStyle.Fill.FillType = ExcelCellFillType.SolidFill;
        titleStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204);
        // set the gradient fill for the title area range with a custom color
        titleStyle.Fill.FillType = ExcelCellFillType.GradientFill;
        titleStyle.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204);
        titleStyle.Fill.GradientFillOptions.Color2 = Color.White;
    // set the title area font 
    titleStyle.Font.Size = 14;
    titleStyle.Font.Bold = true;
    titleStyle.Font.UnderlineType = ExcelCellUnderlineType.Single;


    #region Add a style used for text messages

    ExcelCellStyle textMessageStyle = workbook.Styles.AddStyle("TextMessageStyle");
    textMessageStyle.Font.Size = 12;
    textMessageStyle.Font.Bold = true;
    textMessageStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
    textMessageStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Left;
    textMessageStyle.Fill.FillType = ExcelCellFillType.SolidFill;
    textMessageStyle.Fill.SolidFillOptions.BackColor = Color.LightBlue;
    textMessageStyle.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Thin;
    textMessageStyle.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Thin;
    textMessageStyle.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Thin;
    textMessageStyle.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Thin;


    #region Add a style used for accessed cells and ranges

    ExcelCellStyle accessedRangeStyle = workbook.Styles.AddStyle("ΑccessedRangeStyle");
    accessedRangeStyle.Font.Size = 10;
    accessedRangeStyle.Font.Bold = true;
    accessedRangeStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
    accessedRangeStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Left;
    accessedRangeStyle.Fill.FillType = ExcelCellFillType.SolidFill;
    accessedRangeStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(204, 255, 204);
    accessedRangeStyle.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Thin;
    accessedRangeStyle.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Thin;
    accessedRangeStyle.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Thin;
    accessedRangeStyle.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Thin;



    // get the first worksheet in the workbook
    ExcelWorksheet worksheet = workbook.Worksheets[0];

    // set the default worksheet name
    worksheet.Name = "Access Ranges And Cells Demo";


    // set worksheet paper size and orientation, margins, header and footer
    worksheet.PageSetup.PaperSize = ExcelPagePaperSize.PaperA4;
    worksheet.PageSetup.Orientation = ExcelPageOrientation.Landscape;
    worksheet.PageSetup.LeftMargin = 1;
    worksheet.PageSetup.RightMargin = 1;
    worksheet.PageSetup.TopMargin = 1;
    worksheet.PageSetup.BottomMargin = 1;

    // add header and footer

    //display a logo image in the left part of the header
    string imagesPath = System.IO.Path.Combine(Server.MapPath("~"), @"Images");
    System.Drawing.Image logoImg = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "logo.jpg"));
    worksheet.PageSetup.LeftHeaderFormat = "&G";
    worksheet.PageSetup.LeftHeaderPicture = logoImg;
    // display worksheet name in the right part of the header
    worksheet.PageSetup.RightHeaderFormat = "&A";

    // add worksheet header and footer
    // display the page number in the center part of the footer
    worksheet.PageSetup.CenterFooterFormat = "&P";
    // display the workbook file name in the left part of the footer
    worksheet.PageSetup.LeftFooterFormat = "&F";
    // display the current date in the right part of the footer
    worksheet.PageSetup.RightFooterFormat = "&D";



    // merge the cells in the range to create the title area 
    // gets the merged range containing the top left cell of the range
    ExcelRange titleRange = worksheet["A2"].MergeArea;
    // set the text of title area
    worksheet["A2"].Text = "Accessing Cells and
        Ranges of Cells";

    // set a row height of 18 points for each row in the range
    titleRange.RowHeightInPoints = 18;
    // set the worksheet top title style
    titleRange.Style = titleStyle;



    //set column width
    worksheet["A1:G1"].ColumnWidthInChars = 14;

    worksheet["A5:G5"].Style = textMessageStyle;
    worksheet["A5:G5"].Value = "Access a single
        cell using the 'A1' notation:";

    // access a single cell using the A1 notation
    worksheet["A7"].Value = "sheet[A7]";
    worksheet["A7"].Style = accessedRangeStyle;

    worksheet["A9:G9"].Style = textMessageStyle;
    worksheet["A9:G9"].Value = "Access a range
        of cells using the 'A1' notation:";

    //access a range of cells using the A1 notation
    worksheet["A11:G12"].Value = "sheet[A11:G12]";
    worksheet["A11:G12"].Style = accessedRangeStyle;

    worksheet["A14:G14"].Style = textMessageStyle;
    worksheet["A14:G14"].Value = "Access a single
        cell using row and column index:";

    // access a cell by one-based row and column index
    worksheet[16, 1].Value = "sheet[16,1]";
    worksheet[16, 1].Style = accessedRangeStyle;

    worksheet["A18:G18"].Style = textMessageStyle;
    worksheet["A18:G18"].Value = "Access a range
        of cells using row and column index:";

    // access a range of cells by one-based row and column index
    worksheet[20, 1, 21, 7].Value = "sheet[20,1,21,7]";
    worksheet[20, 1, 21, 7].Style = accessedRangeStyle;

    worksheet["A23:G23"].Style = textMessageStyle;
    worksheet["A23:G23"].Value = \
                "Access a range of cells using a named range defined at worksheet
                    or workbook level:";

    // access a range of cells using a named range

    // a named range defined at worksheet level can be referenced only
        inside the worksheet where it is defined
    // a named range defined at workbook level can referenced by all the
        worksheets in the workbook

    // add a named range at worksheet level
    ExcelNamedRange worksheetNamedRange = worksheet.NamedRanges.AddNamedRange(worksheet["A25"], "MyWSNamedRange");
    worksheet["MyWSNamedRange"].Value = "Named
        Range in Worksheet";
    worksheet["MyWSNamedRange"].Style.Font.Color = Color.Green;
    worksheet["MyWSNamedRange"].Style.Font.Bold = true;

    // add a named range at wotkbook level
    ExcelNamedRange workbookNamedRange = workbook.NamedRanges.AddNamedRange(worksheet["A26"], "MyWBNamedRange");
    worksheet["MyWBNamedRange"].Value = "Named
        Range in Workbook";
    worksheet["MyWBNamedRange"].Style.Font.Color = Color.Green;
    worksheet["MyWBNamedRange"].Style.Font.Bold = true;

    // Get the used range of the worksheet

    worksheet["A28:G28"].Style = textMessageStyle;
    worksheet["A28:G28"].Value = "Get the Used
        Range of the worksheet:";

    // access a cell by one-based row and column index
    worksheet[30, 1].Style.Font.Color = Color.Green;
    worksheet[30, 1].Style.Font.Bold = true;
    worksheet[30, 1].Value = worksheet.UsedRange.Address;



    // Save the Excel document in the current HTTP response stream

    string outFileName = workbookFormat == ExcelWorkbookFormat.Xls_2003 ? "RangesAndCels.xls" : 

    System.Web.HttpResponse httpResponse = System.Web.HttpContext.Current.Response;

    // Prepare the HTTP response stream for saving the Excel document

    // Clear any data that might have been previously buffered in the
        output stream

    // Set output stream content type for Excel 97-2003 (.xls) or Excel
        2007 (.xlsx)
    if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
        httpResponse.ContentType = "Application/x-msexcel";
        httpResponse.ContentType = "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

    // Add the HTTP header to announce the Excel document either as an
        attachment or inline
    httpResponse.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", outFileName));

    // Save the workbook to the current HTTP response output stream
    // and close the workbook after save to release all the allocated
    catch (Exception ex)
        // report any error that might occur during save
        Session["ErrorMessage"] = ex.Message;
        // close the workbook and release the allocated resources

        #region Dispose the Image object
        if (logoImg != null)


    // End the response and finish the execution of this page