New to Telerik UI for Blazor? Download free 30-day trial

Iterate Spreadsheet Data and Get Cell Values

Environment

Product Spreadsheet for Blazor

Description

This KB article answers the following questions:

  • How to iterate Spreadsheet cells?
  • How to get the Spreadsheet cell values?
  • How to obtain the data that users entered in the Blazor Spreadsheet?

Solution

  1. Install the required Telerik Document Processing NuGet packages:
    • Telerik.Documents.Spreadsheet
    • Telerik.Documents.Spreadsheet.FormatProviders.OpenXml
  2. Set the @ref attribute of the Spreadsheet, so that you can call component methods.
  3. Use the ExportToExcelAsync() Spreadsheet method to get the Excel file as a byte[].
  4. Create a MemoryStream that contains the Excel file byte array.

    The steps below use the Telerik RadSpreadProcessing API and are outside the scope of Telerik UI for Blazor.

  5. Use an XlsxFormatProvider to import the Excel file as a Workbook.

  6. Get the desired Worksheet object from the workbook.
  7. Get the desired CellRange from the worksheet.
  8. Iterate the cell range.
  9. Get each cell value (ICellValue object), cell value type (CellValueType object), and cell format (CellValueFormat object) to know more about the cell content.
  10. Use ICellValue.RawValue or ICellValue.GetResultValueAsString(CellValueFormat cellFormat) to obtain the actual cell content.
  11. Excel stores dates as numbers. Use FormatHelper.ConvertDoubleToDateTime(Convert.ToDouble(ICellValue.RawValue)) to retrieve any DateTime values.

Iterate the Spreadsheet cells and get their values

@using Telerik.Windows.Documents.Spreadsheet.FormatProviders
@using Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx
@using Telerik.Windows.Documents.Spreadsheet.Formatting
@using Telerik.Windows.Documents.Spreadsheet.Formatting.FormatStrings
@using Telerik.Windows.Documents.Spreadsheet.Model

<p>
    <TelerikButton ThemeColor="@ThemeConstants.Button.ThemeColor.Primary"
                   OnClick="@OnButtonClick">Get Cell Values</TelerikButton>
    <TelerikLoader Visible="@LoaderVisible" />
</p>

<TelerikSpreadsheet @ref="@SpreadsheetRef"
                    Data="@SpreadsheetData"
                    Height="300px">
</TelerikSpreadsheet>

@if (SpreadsheetValues.Any())
{
    <h3>Cell Values</h3>
    <table class="spreadsheet-values">
        @foreach (KeyValuePair<int, List<object?>> row in SpreadsheetValues)
        {
            <tr>
                @foreach (var cell in row.Value)
                {
                    <td>@cell</td>
                }
            </tr>
        }
    </table>

    <h3>Cell Strings</h3>
    <table class="spreadsheet-values">
        @foreach (KeyValuePair<int, List<string>> row in SpreadsheetStrings)
        {
            <tr>
                @foreach (var cell in row.Value)
                {
                    <td>@cell</td>
                }
            </tr>
        }
    </table>
}

<style>
    .spreadsheet-values {
        margin: 1em;
    }

        .spreadsheet-values td {
            border: 1px solid #ccc;
            padding: .2em .4em;
        }
</style>

@code {
    private TelerikSpreadsheet? SpreadsheetRef { get; set; }
    private byte[]? SpreadsheetData { get; set; }

    private bool LoaderVisible { get; set; }

    private Dictionary<int, List<object?>> SpreadsheetValues { get; set; } = new();
    private Dictionary<int, List<string>> SpreadsheetStrings { get; set; } = new();

    private async Task OnButtonClick()
    {
        if (SpreadsheetRef == null)
        {
            return;
        }

        SpreadsheetValues = new();
        SpreadsheetStrings = new();

        LoaderVisible = true;

        // Refresh the UI: https://docs.telerik.com/blazor-ui/components/loader/overview#creating-blazor-loader
        await Task.Delay(1);

        byte[] excelFileToRead = await SpreadsheetRef.ExportToExcelAsync();

        using MemoryStream ms = new MemoryStream(excelFileToRead);

        IWorkbookFormatProvider formatProvider = new XlsxFormatProvider();
        Workbook workbook = formatProvider.Import(ms);
        Worksheet worksheet = workbook.Worksheets.First();

        CellRange cellRange = worksheet.UsedCellRange;

        for (int rowIndex = cellRange.FromIndex.RowIndex; rowIndex <= cellRange.ToIndex.RowIndex; rowIndex++)
        {
            var rowValues = new List<object?>();
            var rowStringValues = new List<string>();

            for (int columnIndex = cellRange.FromIndex.ColumnIndex; columnIndex <= cellRange.ToIndex.ColumnIndex; columnIndex++)
            {
                CellSelection cell = worksheet.Cells[rowIndex, columnIndex];
                ICellValue cellValue = cell.GetValue().Value;
                CellValueType valueType = cellValue.ValueType;
                CellValueFormat cellFormat = cell.GetFormat().Value;

                if (valueType == CellValueType.Number &&
                    cellFormat.FormatStringInfo.FormatType == FormatStringType.DateTime)
                {
                    // Excel stores dates as numbers.
                    // Excel date format strings differ from .NET format strings.
                    DateTime? actualDateTimeValue = FormatHelper.ConvertDoubleToDateTime(Convert.ToDouble(cellValue.RawValue));

                    rowValues.Add(actualDateTimeValue);
                    rowStringValues.Add(cellValue.GetResultValueAsString(cellFormat));
                }
                else if (valueType == CellValueType.Number)
                {
                    double actualNumericValue = Convert.ToDouble(cellValue.RawValue);

                    rowValues.Add(actualNumericValue);
                    rowStringValues.Add(actualNumericValue.ToString(cellFormat.FormatString));
                }
                else if (valueType == CellValueType.Boolean)
                {
                    bool actualBooleanValue = Convert.ToBoolean(cellValue.RawValue);

                    rowValues.Add(actualBooleanValue);
                    rowStringValues.Add(actualBooleanValue.ToString().ToLowerInvariant());
                }
                else if (valueType == CellValueType.Text || valueType == CellValueType.Empty)
                {
                    rowValues.Add(cellValue.RawValue);
                    rowStringValues.Add(cellValue.RawValue);
                }
            }

            SpreadsheetValues.Add(rowIndex, rowValues);
            SpreadsheetStrings.Add(rowIndex, rowStringValues);
        }

        LoaderVisible = false;
    }

    protected override void OnInitialized()
    {
        SpreadsheetData = Convert.FromBase64String(Base64ExcelSource);

        base.OnInitialized();
    }

    private const string Base64ExcelSource = "UEsDBAoAAAAAAFheG1kAAAAAAAAAAAAAAAAJAAAAZG9jUHJvcHMvUEsDBAoAAAAIAFheG1nNjhd0PQEAAIECAAARAAAAZG9jUHJvcHMvY29yZS54bWylkl9rwjAUxd+FfYeQ9zZtdepKW2EbwtiEwSobewvJVcuaPyTZqt9+adVimW97TM7J795zSLbYixr9gLGVkjmOwwgjkEzxSm5zvC6XwRwj66jktFYScnwAixfFzQhlTKdMGXg1SoNxFVjkUdKmTOd455xOCbFsB4La0DukFzfKCOr80WyJpuyLboEkUTQlAhzl1FHSAgPdE/EIoROVs56qv03dMTgjUIMA6SyJw5jg3uvACHv1QaeQAVhU7qDhqvss9ui9rXpj0zRhM+6sPkVMPlYv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