Export to PDF and Excel on Android 11

Dear Syncfusion support,


I am trying to export data from SfDataGrid to pdf and excel. I have followed your documentation and the code executes without error, it just does not save the file to the device. On your sample app 'Syncfusion Components for Xamarin.Forms' downloaded from the Google Play store, it also does not seem to work. Any guidance for this would be greatly appreciated.

Here is some sample code:

Code behind:

private void ExportExcel()

        {

            DataGridExcelExportingController excelExport = new DataGridExcelExportingController();

            var excelEngine = excelExport.ExportToExcel(this.dataGrid);

            var workbook = excelEngine.Excel.Workbooks[0];

            MemoryStream stream = new MemoryStream();

            workbook.SaveAs(stream);

            workbook.Close();

            excelEngine.Dispose();


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

        }

Interface in shared project:

public interface ISave

    {

        void Save(string filename, string contentType, MemoryStream stream);

    }

Android renderer in Android project:

internal class SaveAndroid : ISave

    {

        public void Save(string filename, string contentType, MemoryStream stream)

        {

            string exception = string.Empty;

            string root = null;

            if (Android.OS.Environment.IsExternalStorageEmulated)

            {

                root = Android.OS.Environment.ExternalStorageDirectory.ToString();

            }

            else

                root = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);


            Java.IO.File myDir = new Java.IO.File(root + "/Forward");

            myDir.Mkdir();

            Java.IO.File file = new Java.IO.File(myDir, filename);


            if (file.Exists()) file.Delete();


            try

            {

                FileOutputStream outs = new FileOutputStream(file);

                outs.Write(stream.ToArray());

                outs.Flush();

                outs.Close();

            }

            catch (Exception e)

            {

                exception = e.ToString();

            }

        }

    }

This line in your documentation is deprecated ->

root = Android.OS.Environment.ExternalStorageDirectory.ToString();


6 Replies 1 reply marked as answer

SV Suja Venkatesan Syncfusion Team November 17, 2021 02:45 PM UTC

Hi Jano, 
 
Thank you for contacting Syncfusion support. 
 
Currently we are validating the reported issue in our end. We will update the further details on or before November 19,2021. We appreciate your patience until then. 
 
Regards, 
Suja.


SV Suja Venkatesan Syncfusion Team November 19, 2021 09:49 AM UTC

Hi Jano, 

Thanks for your patience. 

We would like to let you know that Android.OS.Environment.ExternalStorageDirectory is deprecated in Android 11. You can resolve the reported issue by using GetExternalFilesDir() method instead to getting the external file directory from Application context as like below given code snippet. 

Code Snippet: SaveAndroid.cs  
if (Android.OS.Environment.IsExternalStorageEmulated) 
{ 
  root = Android.App.Application.Context.GetExternalFilesDir 
        (Android.OS.Environment.DirectoryDownloads).AbsolutePath; 
 
] 

We have attached a runnable sample for your reference in the following link. 


Please have look at this sample and let us know if you have any concern in this. 

Regards, 
Suja.  



JA Jano replied to Suja Venkatesan November 22, 2021 05:57 AM UTC

Hi Suja,


Could you please let me know where I can find the package 'Syncfusion.Xamarin.Data'? I am not able to find it in nuget package manager. I had to remove that package from the solution you provided to build the application. That might be the reason that the exporting of the SfDataGrid still does not work. I tested on API level 29 as well (Android 10.0), but still no luck. Please see the attached screen recording


Attachment: SVID_20211122_074358_1_d3290542.zip


SV Suja Venkatesan Syncfusion Team November 23, 2021 02:19 PM UTC

Hi Jano, 

Sorry for the inconvenience caused. 

We have attached a runnable sample without 'Syncfusion.Xamarin.Data' package and video illustration of the sample in the following link. We would like to let you know that you can find the exported pdf and excel file in the Folder(“…\Syncfusion\Datagrid.pdf” ) in FileExplorer 


Please have look at this sample and let us know if you have any concern in it. 

Regards, 
Suja.  


Marked as answer

JA Jano replied to Suja Venkatesan November 24, 2021 05:09 AM UTC

Hi Suja,


Thank you for your response and the sample project. The sample project resolves the issue I was facing.



SV Suja Venkatesan Syncfusion Team November 24, 2021 08:49 AM UTC

Hi Jano,  
  
Thank you for the update.  
  
We are glad that our solution meets your requirement. Please let us know if you need further assistance. As always, we are happy to help you out.  

Regards, 
Suja. 


Loader.
Up arrow icon