Completed
Last Updated: 15 May 2024 07:49 by ADMIN
Release 2024.2.514 (2024 Q2)
Steven
Created on: 06 Mar 2024 14:11
Category: PivotGrid
Type: Bug Report
1
RadPivotGrid: NullReference exception is thrown when exporting the control with FileExportMode.CreateOrOverrideSheet
In this case, other sheets have references to the one in which the RadPivotGrid is exported. Currently, the logic will remove and recreated the sheet which will break the references and lead to an exception.
1 comment
ADMIN
Dinko | Tech Support Engineer
Posted on: 06 Mar 2024 15:02

Hello Steven, 

Thank you for reporting this. 

To workaround this, we can clear the values in the cells instead of re-creating the sheet. To do that we need to interfere in the ReplaceWorksheet() method. We can create a custom class that derives from SpreadExportRenderer and override the ReplaceWorksheet() method. Inside the method, we can clear the cell's value.

public class CustomRenderer : SpreadExportRenderer
{
    public override bool ReplaceWorksheet(string sheetName)
    {
        bool result = false;
        if (string.IsNullOrWhiteSpace(sheetName))
        {
            this.AddWorksheet(sheetName);
            return result;
        }

        var workbookField = typeof(SpreadExportRenderer).GetField("workbook", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

        Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook = workbookField.GetValue(this) as Workbook;
        int index = workbook.Worksheets.IndexOf(sheetName);
        if (index >= 0)
        {
            result = true;
            workbook.Worksheets[index].Cells[0, 10000].ClearValue();// cell range can be modified.
        }

        var worksheetField = typeof(SpreadExportRenderer).GetField("worksheet", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        Worksheet worksheet = workbookField.GetValue(this) as Worksheet;

        if (result && index < workbook.Worksheets.Count)
        {
            // Sheet was not the last one.
                
            worksheet = workbook.Worksheets[index];
        }
        else
        {
            worksheet = workbook.Worksheets.Add();
        }
        worksheet.Name = sheetName;
        worksheetField.SetValue(this, worksheet);
        // this.worksheet.Name = sheetName;
        return result;
    }
}

This way the FileExportMode.CreateOrOverrideSheet option will work.

private void radButton1_Click(object sender, EventArgs e)
{
    PivotGridSpreadExport exporter = new PivotGridSpreadExport(this.radPivotGrid1);
    CustomRenderer renderer = new CustomRenderer();

    exporter.ShowGridLines = false;
    exporter.ExportVisualSettings = true;
    exporter.ExportSelectionOnly = true;
    exporter.FileExportMode = FileExportMode.CreateOrOverrideSheet;
    exporter.RunExport("../../../PivotData", renderer, "PivotData");
}

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.