ahmed salah

ahmed salah

  • 1.2k
  • 547
  • 66.2k

How to export data from ridzen datat grid to excel after filter?

Jul 22 2023 3:53 PM

I work on asp.net core blazer server side .I face issue I can't export ridzen data grid to excel after filter data on it .

so when page load list of Data source of ridzen data grid 10 so after this write any filter to any column then become 4 so when press

button export to excel it export 10 rows but correct must be  4 rows

so How to export ridzen data grid to excel after filter ridzen datat grid

private async Task ExportToExcel()
{
     ExceldatabaseData = databaseData.Select(p => new CopyExcelDatabaseClass
        {
            DBID = p.dbid,
            DBName = p.databaseName,
            ServerID = p.serverID,
            ServerName = p.serverName,
            Severity = p.serverity,
            BackupProcedure = p.backUpProcedure,
            Remarks = p.remarks,
            OwnerFileNo = p.ownerfilenumber,
            OwnerFileName = p.ownerfilename,
            IsActive = p.isActive

        }).ToList();
    ExcelPackage.LicenseContext = LicenseContext.Commercial;
    var stream = new MemoryStream();
    using (var package = new ExcelPackage(stream))
    {
        var worksheet = package.Workbook.Worksheets.Add("DatabaseList");
        //worksheet.Cells.LoadFromCollection(databaseData, true);
        worksheet.Cells.LoadFromCollection(ExceldatabaseData, true);
        package.Save();
    }
    var buffer = stream.ToArray();
    var fileName = $"Database_{DateTime.Now:yyyyMMddHHmmss}.xlsx";
    await JS.InvokeAsync<object>("saveAsFile", fileName, Convert.ToBase64String(buffer));
}
public class CopyExcelDatabaseClass
{
    [DisplayName("DB ID")]
    public int DBID { get; set; }

    [DisplayName("DB Name")]
    public string DBName { get; set; }

    [DisplayName("Server ID")]
    public int ServerID { get; set; }
    
    [DisplayName("Server Name")]
    public string ServerName { get; set; }
    
    public string Severity { get; set; }
    [DisplayName("Backup Procedure")]
    public string BackupProcedure { get; set; }
    public string Remarks { get; set; }

    [DisplayName("Owner File No")]
    public string OwnerFileNo { get; set; }
    [DisplayName("Owner File Name")]
    public string OwnerFileName { get; set; }

    
    [DisplayName("Is Active")]
    public bool IsActive { get; set; }

}
private async Task RefreshList()
{
    var request = "Database/GetAllDatabaseDetails";
    var response = await client.SendAsync(request);
    using var responsestream = await response.Content.ReadAsStreamAsync();
    databaseData = await JsonSerializer.DeserializeAsync<List<DatabaseClass>>(responsestream);
}

private IEnumerable<DatabaseClass> databaseData = Array.Empty<DatabaseClass>();
 
protected override async Task OnInitializedAsync()
{
    await RefreshList();
}

javascript function 

window.saveAsFile = (filename, bytesBase64) => {
    const link = document.createElement('a');
    link.download = filename;
link = 'data:application/octet-stream;base64,' + bytesBase64;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
 
};

 


Answers (1)