Exporting to Excel - Datagrid

Hello,
I have the following code:

private void ExportAllTraitsToExcel()
{
var vm = this.BindingContext as BullListViewModel;
SfDataGrid grid = new SfDataGrid();
grid.ItemsSource = vm.DisplayBulls;
grid.AutoGenerateColumns = true;
grid.AutoGenerateColumnsMode = AutoGenerateColumnsMode.ResetAll;
DataGridExcelExportingController excelExport = new DataGridExcelExportingController();
var excelEngine = excelExport.ExportToExcel(grid);
var workbook = excelEngine.Excel.Workbooks[0];
MemoryStream stream = new MemoryStream();
workbook.SaveAs(stream);
workbook.Close();
excelEngine.Dispose();
Xamarin.Forms.DependencyService.Get().Save("BullExportData_FilteredTraits.xlsx", "application/msexcel", stream);
}
When I look at the exported excel file there isn't any columns in the file. It seems like the columns aren't auto generating. Please advise.
Thank you,
Dylan

3 Replies

JA Jayaraman Ayyanar Syncfusion Team June 4, 2018 02:37 AM UTC

Hi  Dylan, 
 
Thanks for contacting Syncfusion support. 
 
We have checked your reported query for the Exporting to Excel in the DataGrid. We have tested the same with the provided code sample and we have noticed that  you are creating instance for DataGrid in both Xaml and code behind will generate a empty excel file due to the newly created instance in the button click of the ExportToExcel. You need to create instance for DataGrid either in XAML or code behind. We have added the comments in the sample to direct you in the right way to use the Exporting feature. We have also modified the sample to the working condition with the provided code snippets. Refer the below code example to achieve you requirement. 
 
 
Code logics 
   private async void ExPortToExcel(object sender, EventArgs e) 
        { 
            this.BindingContext = new OrderInfoRepository(); 
          //  grid = new SfDataGrid(); // If you need to create instance either in xaml or here. Do not create in the both locations. 
            grid.ItemsSource = ((this.BindingContext) as OrderInfoRepository).OrderInfoCollection; 
            grid.AutoGenerateColumns = true; 
            grid.AutoGenerateColumnsMode = AutoGenerateColumnsMode.ResetAll; 
            await Task.Delay(500); 
            DataGridExcelExportingController excelExport = new DataGridExcelExportingController(); 
 
            DataGridExcelExportingOption option = new DataGridExcelExportingOption(); 
 
            var excelEngine = excelExport.ExportToExcel(this.grid); 
            var workbook = excelEngine.Excel.Workbooks[0]; 
            MemoryStream stream = new MemoryStream(); 
            workbook.SaveAs(stream); 
            workbook.Close(); 
            excelEngine.Dispose(); 
 
            if (Device.RuntimePlatform == Device.UWP) 
                Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("DataGrid.xlsx", "application/msexcel", stream); 
            else 
                Xamarin.Forms.DependencyService.Get<ISave>().Save("DataGrid.xlsx", "application/msexcel", stream); 
 
        } 
 
 
We have prepared a sample for your reqiurement and you can download the same from the below link. 
 
For more details about the Exporting feature you can refer the help documentation in the below link. 
 
Regards, 
Jayaraman. 



DW Dylan Wegner June 4, 2018 12:28 PM UTC

Thanks for replying, Jayaraman. 

I looked at your suggestions and I decided to create a new page to do the export and I am still not getting anything in the excel file. The entire view is below. 

Here is the xaml: 
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:viewmodel="clr-namespace:Genex.DairySireSearch.ViewModels.Bull"
             xmlns:datagrid="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms"
             x:Class="Genex.DairySireSearch.Views.Bull.ExportAllBullsExcelView">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Exporting All Bulls to Excel" />
            <datagrid:SfDataGrid x:Name="grid"></datagrid:SfDataGrid>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Here is the code behind: 
using Genex.DairySireSearch.DendencyServices;
using Genex.DairySireSearch.Models;
using Skyline.Forms;
using Syncfusion.SfDataGrid.XForms;
using Syncfusion.SfDataGrid.XForms.Exporting;
using System.IO;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Genex.DairySireSearch.Views.Bull
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ExportAllBullsExcelView : ContentPage
    {
        private FastObservableCollection<BullListDisplayModel> bullsToExport;

        public ExportAllBullsExcelView(FastObservableCollection<BullListDisplayModel> bullsToExport)
        {
            InitializeComponent();
            this.bullsToExport = bullsToExport;
        }

        protected async override void OnAppearing()
        {
            ExportAllTraitsToExcel();
            await AppNavigation.PopAsync();
        }

        private async void ExportAllTraitsToExcel()
        {
            grid = new SfDataGrid();
            grid.ItemsSource = bullsToExport;
            grid.AutoGenerateColumns = true;
            grid.AutoGenerateColumnsMode = AutoGenerateColumnsMode.ResetAll;
            await Task.Delay(500);

            DataGridExcelExportingController excelExport = new DataGridExcelExportingController();
            DataGridExcelExportingOption option = new DataGridExcelExportingOption();

            var excelEngine = excelExport.ExportToExcel(this.grid);
            var workbook = excelEngine.Excel.Workbooks[0];
            MemoryStream stream = new MemoryStream();
            workbook.SaveAs(stream);
            workbook.Close();
            excelEngine.Dispose();

            Xamarin.Forms.DependencyService.Get<ISave>().Save("BullExportData_FilteredTraits.xlsx", "application/msexcel", stream);
        }
    }
}


DS Divakar Subramaniam Syncfusion Team June 5, 2018 08:44 AM UTC

Hi Dylan, 
 
Thanks for the update. 
 
In your code, you have initialized the grid one more time in ExportAllTraitsToExcel() method which is not actually correct way because you have already created instance for the grid in XAML code (with the name as “grid”). Please refer the below code, 
  
private async void ExportAllTraitsToExcel() 
{ 
    grid = new SfDataGrid(); //You should not create new instance for grid once again which is already loaded in XAML. So remove that code. 
    grid.ItemsSource = bullsToExport; 
    grid.AutoGenerateColumns = true; 
    grid.AutoGenerateColumnsMode = AutoGenerateColumnsMode.ResetAll; 
    await Task.Delay(500); 
 
    DataGridExcelExportingController excelExport = new DataGridExcelExportingController(); 
    DataGridExcelExportingOption option = new DataGridExcelExportingOption(); 
 
    var excelEngine = excelExport.ExportToExcel(this.grid); 
    var workbook = excelEngine.Excel.Workbooks[0]; 
    MemoryStream stream = new MemoryStream(); 
    workbook.SaveAs(stream); 
    workbook.Close(); 
    excelEngine.Dispose(); 
 
    Xamarin.Forms.DependencyService.Get<ISave>().Save("BullExportData_FilteredTraits.xlsx", "application/msexcel", stream); 
} 
 
 
If you create a new instance like this, then this will be considered as the new grid which is not the actual grid you have loaded in the XAML. This is the reason, the grid is not exported in the excel with the data. So, please remove the instance creation from your code. Also, you can refer the sample we have provided on our previous update in which we have actually removed the unwanted instance creation.  
 
Please let us know if you need any other assistance. 
 
Regards, 
Divakar. 


Loader.
Up arrow icon