Unplanned
Last Updated: 30 May 2023 09:57 by ADMIN
Randy
Created on: 21 Feb 2023 16:57
Category: Grid
Type: Feature Request
3
Ensure empty Grid cells are treated as blank cells in the exported Excel file

Currently, the empty cells in the exported Excel file contain zero-length strings. As a result, the ISBLANK() function returns false for them while the cells essentially do not have content.

Please allow the empty Grid cells to be treated as blank in the exported Excel file.

===

ADMIN EDIT

===

For the time being, you may extend the formula to also check whether the length of the cell content is 0. For that purpose, you may use the LEN() function as suggested here:  https://learn.microsoft.com/en-us/office/troubleshoot/excel/isblank-function-return-false#workaround.

2 comments
ADMIN
Nadezhda Tacheva
Posted on: 30 May 2023 09:57

Hi Randy,

Thank you for listing this scenario as well! Our development team will take it into consideration when working on the item.

A possible option I can suggest for the time being is to modify the exported file as needed to meet the specific requirements of the business case.

You can do that by getting the file in the OnAfterExport event and changing it using the Document Processing libraries that are available with your license. Similar to the approach listed here: https://docs.telerik.com/blazor-ui/knowledge-base/grid-custom-cell-formatting-with-radspreadprocessing.

For your specific case, you may loop through the cells, check if the length of their values is 0 and clear them if so. Here is a sample (install the Telerik.Documents.Spreadsheet.FormatProviders.Xls package to test it): 

@using Telerik.Windows.Documents.Spreadsheet.FormatProviders
@using Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx
@using Telerik.Windows.Documents.Spreadsheet.FormatProviders.TextBased.Csv
@using Telerik.Windows.Documents.Spreadsheet.Model

<TelerikGrid Data="@GridData" Pageable="true">
    <GridToolBarTemplate>
        <GridCommandButton Command="ExcelExport" Icon="@FontIcon.FileExcel">Export to Excel</GridCommandButton>
        <label class="k-checkbox-label"><TelerikCheckBox @bind-Value="@ExportAllPages" />Export All Pages</label>
    </GridToolBarTemplate>

    <GridExport>
        <GridExcelExport FileName="telerik-grid-export"
                         AllPages="@ExportAllPages"
                         OnAfterExport="@OnExcelAfterExport" />
    </GridExport>

    <GridColumns>
        <GridColumn Field="@nameof(SampleData.ProductId)" Title="ID" Width="100px" />
        <GridColumn Field="@nameof(SampleData.ProductName)" Title="Product Name" Width="300px" />
        <GridColumn Field="@nameof(SampleData.UnitsInStock)" Title="In stock" Width="100px" />
        <GridColumn Field="@nameof(SampleData.Price)" Title="Unit Price" Width="200px" />
        <GridColumn Field="@nameof(SampleData.Discontinued)" Title="Discontinued" Width="100px" />
        <GridColumn Field="@nameof(SampleData.FirstReleaseDate)" Title="Release Date" Width="300px" />
    </GridColumns>
</TelerikGrid>

@code {
    private async Task OnExcelAfterExport(GridAfterExcelExportEventArgs args)
    {
        //args.Stream is finalized. The Import() method of the XlsxFormatProvider requires a readable stream, so you should copy the stream bytes to a new MemoryStream instance which will be used for the import.
        var bytes = args.Stream.ToArray();

        var excelStream = new MemoryStream(bytes);

        //create a format provider instance to call the import
        XlsxFormatProvider formatProvider = new XlsxFormatProvider();

        //import the stream to a workbook
        Workbook workbook = formatProvider.Import(excelStream);

        //loop through the cells
        CellRange usedCellRange = workbook.Worksheets[0].UsedCellRange;

        for (int rowIndex = usedCellRange.FromIndex.RowIndex; rowIndex <= usedCellRange.ToIndex.RowIndex; rowIndex++)
        {
            for (int columnIndex = usedCellRange.FromIndex.ColumnIndex; columnIndex <= usedCellRange.ToIndex.ColumnIndex; columnIndex++)
            {
                CellSelection cell = workbook.Worksheets[0].Cells[rowIndex, columnIndex];

                //get the cell value
                RangePropertyValue<ICellValue> rangeValue = cell.GetValue();
                ICellValue value = rangeValue.Value;

                //if the value length is 0, clear it
                if (value.RawValue.Length == 0)
                {
                    cell.ClearValue();
                }
            }
        }

        //save the modified workbook in a MemoryStream
        MemoryStream modifiedExport = new MemoryStream();

        formatProvider.Export(workbook, modifiedExport);

        //pass the modified stream to the event arguments
        args.Stream = modifiedExport;
    }

    List<SampleData> GridData { get; set; }

    bool ExportAllPages { get; set; }

    protected override void OnInitialized()
    {
        GridData = Enumerable.Range(1, 100).Select(x => new SampleData
            {
                ProductId = x,
                ProductName = $"Product {x}",
                UnitsInStock = x % 4 == 0? null : x * 2,
                Price = 3.14159m * x,
                Discontinued = x % 4 == 0,
                FirstReleaseDate = DateTime.Now.AddDays(-x)
            }).ToList();
    }

    public class SampleData
    {
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public int? UnitsInStock { get; set; }
        public decimal Price { get; set; }
        public bool Discontinued { get; set; }
        public DateTime FirstReleaseDate { get; set; }
    }
}

Regards,
Nadezhda Tacheva
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources!
Randy
Posted on: 23 May 2023 16:06
This is not only a problem for the use of formulas, but just highlighting a range of cells to get a quick count is inaccurate because Excel sees the "blank" cells as having content so it includes them in the Count. This is very problematic for our users as they try to get quick counts of columns that contain a Date, for example.